Initial commit: C language learning code
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !定义并初始化变量
|
||||
int a = 0; // 用户输入的第一个整数,作为数列的基础数字
|
||||
int n = 0; // 用户输入的第二个整数,表示数列的项数
|
||||
|
||||
// !获取用户输入
|
||||
printf("请输入两个整数a和n:");
|
||||
scanf("%d %d", &a, &n); // 读取用户输入的两个整数
|
||||
|
||||
int sum = 0; // 用于存储数列的和
|
||||
int b = a; // 保存a的初始值,用于后续计算
|
||||
int count = 1; // 循环计数器,从1开始到n结束
|
||||
|
||||
// !计算数列的和
|
||||
while (count <= n) // 循环n次
|
||||
{
|
||||
sum = sum + a; // 将当前项a加到总和sum中
|
||||
a = a * 10 + b; // 生成下一项:将当前项乘以10再加上初始值b
|
||||
count++; // 计数器递增
|
||||
}
|
||||
|
||||
// !输出结果
|
||||
printf("数列之和为%d\n", sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int N;
|
||||
printf("请输入项数N: ");
|
||||
scanf("%d", &N);
|
||||
double a = 2.0, b = 1.0, sum = 0.0;
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
sum += a / b;
|
||||
double next_a = a + b; // 计算下一项的分子
|
||||
double next_b = a; // 计算下一项的分母
|
||||
a = next_a; // 更新分子
|
||||
b = next_b; // 更新分母
|
||||
}
|
||||
printf("%.2f\n", sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n=9;
|
||||
printf("请输入一个正整数n:");
|
||||
scanf("%d", &n);
|
||||
|
||||
if (n < 1 || n > 9)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
for (int j = 1; j <= i; j++)
|
||||
{
|
||||
printf("%d*%d=%d ", i, j, i * j);
|
||||
if (i == j)
|
||||
{
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c = '1'; // '1'是字符常量,不是整数,注意:" "也是字符常量
|
||||
char d = 1; // 1是整数常量,不是字符常量
|
||||
printf("%c\n", c); // %c是字符输出
|
||||
printf("%d\n", d); // %d是整数输出
|
||||
|
||||
char ch;
|
||||
scanf("%c", &ch); // scanf("%c", &c)是字符输入
|
||||
printf("ch=%d\n", ch); // %d是整数输出
|
||||
printf("ch='%c'\n", ch);// %c是字符输出
|
||||
|
||||
// !char是一种数据类型,它是用数字编码表示的字符集,比如ASCII码,Unicode码等,也就是说,char类型可以表示一个字符,也可以表示一个整数,具体表示什么,取决于它的编码方式(即输出printf的是%d还是%c)
|
||||
|
||||
// 例如
|
||||
char ch1 = 'a';
|
||||
ch1++;
|
||||
printf("'%c'\n", ch1); //输出'b'
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int num_weishu(int num) // 判断数字位数
|
||||
{
|
||||
int n = 0;
|
||||
num = abs(num);
|
||||
if (num == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
while (num > 0)
|
||||
{
|
||||
num /= 10;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
int m = 0;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
|
||||
a = num_weishu(x);
|
||||
b = (int)(pow(10, a - 1) + 0.1); // pow函数是次方运算的函数名,+0.1是为了避免浮点数误差
|
||||
printf("这个数字的位数是%d。\n", a);
|
||||
|
||||
if (a > 9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (x < 0) // 处理负号
|
||||
{
|
||||
printf("-");
|
||||
}
|
||||
while (b > 0)
|
||||
{
|
||||
m = x / b;
|
||||
switch (m)
|
||||
{
|
||||
case 0:
|
||||
printf("ling ");
|
||||
break;
|
||||
case 1:
|
||||
printf("yi ");
|
||||
break;
|
||||
case 2:
|
||||
printf("er ");
|
||||
break;
|
||||
case 3:
|
||||
printf("san ");
|
||||
break;
|
||||
case 4:
|
||||
printf("si ");
|
||||
break;
|
||||
case 5:
|
||||
printf("wu ");
|
||||
break;
|
||||
case 6:
|
||||
printf("liu ");
|
||||
break;
|
||||
case 7:
|
||||
printf("qi ");
|
||||
break;
|
||||
case 8:
|
||||
printf("ba ");
|
||||
break;
|
||||
case 9:
|
||||
printf("jiu ");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (b > 9)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
x = x % b;
|
||||
b = b / 10;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned char c = 127;
|
||||
int i = 255;
|
||||
|
||||
c = c + 1;
|
||||
|
||||
printf("c=%d,i=%d", c ,i);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c = 127;
|
||||
int i = 255;
|
||||
|
||||
c = c + 1;
|
||||
|
||||
printf("c=%d,i=%d", c ,i);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 0;
|
||||
int b = 1;
|
||||
|
||||
while (++a>0)
|
||||
{
|
||||
;
|
||||
}
|
||||
printf("int的最大值是:%d\n", a-1);
|
||||
|
||||
while ((a=a/10)!=0)
|
||||
{
|
||||
b++;
|
||||
}
|
||||
printf("int的位数是:%d\n", b);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 0;
|
||||
int b = 1;
|
||||
|
||||
while (++a>0)
|
||||
{
|
||||
;
|
||||
}
|
||||
printf("int的最大值是:%d\n", a-1);
|
||||
|
||||
while ((a=a/10)!=0)
|
||||
{
|
||||
b++;
|
||||
}
|
||||
printf("int的位数是:%d\n", b);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
/*
|
||||
条件运算符:?:
|
||||
条件运算符的优先级低于关系运算符,高于算术运算符
|
||||
条件运算符的运算对象和求值结果都是int类型
|
||||
条件运算符的运算对象可以是任意能转换成int类型的值
|
||||
*/
|
||||
int count = 0;
|
||||
scanf("%d",&count);
|
||||
count=(count>20)?count-10:-count+10;
|
||||
/*
|
||||
等价于
|
||||
if (count>20)
|
||||
count=count-10;
|
||||
else
|
||||
count=-count+10;
|
||||
*/
|
||||
|
||||
/*
|
||||
逗号运算符:,:
|
||||
逗号用来连接两个表达式,并以其右边的表达式的值做为他的结果
|
||||
逗号运算符的优先级在所有运算符中是最低的,所以他的两边的运算对象会先求值
|
||||
逗号的组合关系是从左向右的,所以左边的运算对象会先求值,而右边的表达式的值就会留下来作为整个逗号表达式的值
|
||||
*/
|
||||
int i;
|
||||
int j;
|
||||
i = 3 + 4, 5 + 6;
|
||||
printf("%d\n", i);
|
||||
// 结果是7,因为逗号运算符的优先级最低,所以i=3+4,5+6;这个表达式等价于i=(3+4),(5+6);
|
||||
|
||||
// 例如
|
||||
for (i = 0, j = 10; i < j;i++,j--)
|
||||
{
|
||||
printf("%d %d\n", i, j);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int power(int a, int x) //a的x次方运算
|
||||
{
|
||||
if (x < 0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int sum = 1;
|
||||
for (int i = 0; i < x; i++)
|
||||
{
|
||||
sum *= a;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("请输入一个正整数n(3 ≤ n ≤ 7):");
|
||||
scanf("%d", &n);
|
||||
|
||||
if (n < 3 || n > 7)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
int lower = power(10, n - 1); //n位数的最小值
|
||||
int upper = power(10, n) - 1; //n位数的最大值
|
||||
|
||||
for (int num = lower; num <= upper;num++) //缩小范围,减少运算量
|
||||
{
|
||||
int sum=0;
|
||||
int m = num;
|
||||
while(m>0)
|
||||
{
|
||||
int a = m % 10;
|
||||
sum += power(a,n);
|
||||
m /= 10;
|
||||
}
|
||||
if (num==sum)
|
||||
{
|
||||
printf("%d\n", sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !浮点数的范围
|
||||
printf("%f\n", 12.0/0.0); //12/0是没有定义的,即只有浮点数有无穷大和无穷小
|
||||
printf("%f\n", -12.0/0.0);
|
||||
printf("%f\n", 0.0/0.0);
|
||||
//inf是无穷大,nan是无穷小
|
||||
|
||||
// !浮点数的精度
|
||||
float a, b, c;
|
||||
a = 1.345f; //带小数点的默认是double类型,所以要加f来指定是float类型
|
||||
b = 1.123f;
|
||||
c = a + b;
|
||||
if (c== 2.468)
|
||||
{
|
||||
printf("c = 2.468\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("不相等! c=%.10f,或%f\n", c, c);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
double ff = 1234.56789;
|
||||
printf("科学计数法的输出:ff = %e\n", ff); //%e是科学计数法的输出
|
||||
printf("科学计数法的输出:ff = %E\n", ff); //%E是科学计数法的输出(底数是大写的)
|
||||
printf("普通格式的输出:ff = %f\n", ff); //%f是普通格式的输出
|
||||
|
||||
double fff = 1E-10;
|
||||
printf("科学计数法的输出:ff = %e\n", fff); //%e是科学计数法的输出
|
||||
printf("科学计数法的输出:ff = %E\n", fff); //%E是科学计数法的输出(底数是大写的)
|
||||
printf("普通格式的输出:ff = %f\n", fff); //%f是普通格式的输出
|
||||
printf("普通格式的输出:ff = %.16f\n", fff); //%.16f是普通格式的保留16位小数的输出
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main() //自己输入数字,自己猜
|
||||
{
|
||||
int a, b, c, d;
|
||||
printf("请输入一个设计的整数和设计轮数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
while(c!=a)
|
||||
{
|
||||
c = 0;
|
||||
d = 1;
|
||||
d++;
|
||||
printf("请输入一个的整数:");
|
||||
scanf("%d", &c);
|
||||
if(c>a)
|
||||
{
|
||||
printf("大了!\n");
|
||||
}
|
||||
else if(c<a)
|
||||
{
|
||||
printf("小了!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
d++;
|
||||
if (d <= b)
|
||||
{
|
||||
printf("你只猜了%d次,就猜对了!\n", d);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("你一共猜了%d次,才猜对!\n", d);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !自类型转换
|
||||
/* 对于printf函数,任何小于int的整型值都会自动转换成int类型
|
||||
float会自动转换成double类型
|
||||
但是scanf函数不会自动转换类型,要输入short类型,必须输入%hd
|
||||
*/
|
||||
|
||||
// !强制类型转换
|
||||
/* 要将一个量强制转换成另一个类型,需要使用强制类型转换运算符(通常是较小的类型)
|
||||
需要:(type)expression,大的变量赋值给小的变量时,旧的值不会改变
|
||||
例如:(int)10.2
|
||||
(short)32
|
||||
强制类型转换的优先级比四则运算高
|
||||
*/
|
||||
printf("%d\n", (short)32768); //结果为-32768
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int gcd(int x, int y) // 求最大公约数
|
||||
{
|
||||
int c;
|
||||
while (y > 0)
|
||||
{
|
||||
c = x % y;
|
||||
x = y;
|
||||
y = c;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int m;
|
||||
int n;
|
||||
printf("请输入分式m/n:");
|
||||
scanf("%d/%d", &m, &n);
|
||||
|
||||
int a, b;
|
||||
int num_gcd = gcd(m, n);
|
||||
a = m / num_gcd;
|
||||
b = n / num_gcd;
|
||||
printf("%d/%d的化简分式是%d/%d\n", m, n, a, b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int l;
|
||||
printf("请输入一个正整数:");
|
||||
scanf("%d", &n);
|
||||
printf("一行有几个整数:");
|
||||
scanf("%d", &l);
|
||||
|
||||
if (n<=0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
int i, j, k;
|
||||
int m = 0;
|
||||
i = n;
|
||||
while(i<=n+3)
|
||||
{
|
||||
j = n;
|
||||
while(j<=n+3)
|
||||
{
|
||||
k = n;
|
||||
while(k<=n+3)
|
||||
{
|
||||
if (i!=j && j!=k && i!=k)
|
||||
{
|
||||
m++;
|
||||
printf("%d%d%d", i, j, k);
|
||||
if (m==l)
|
||||
{
|
||||
printf("\n");
|
||||
m = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
}
|
||||
k++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int is_sushu(int x) //判断一个数是否是素数
|
||||
{
|
||||
if (x <= 1)
|
||||
return 0;
|
||||
for (int a = 2; a <= sqrt(x); a++)
|
||||
{
|
||||
if (x % a == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int num_sushuhe(int x, int y) //计算区间[x,y]内素数的和
|
||||
{
|
||||
int sum = 0;
|
||||
for (int e = x; e <= y; e++)
|
||||
{
|
||||
if (is_sushu(e))
|
||||
{
|
||||
sum += e;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int m, n;
|
||||
printf("请输入两个正整数m和n:");
|
||||
scanf("%d %d", &m, &n);
|
||||
|
||||
if (n > m) // 确保m <= n
|
||||
{
|
||||
int temp = m;
|
||||
m = n;
|
||||
n = temp;
|
||||
}
|
||||
|
||||
if (n < 1 || m < 1)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("素数列表: ");
|
||||
for (int x = n; x <= m; x++)
|
||||
{
|
||||
if (is_sushu(x))
|
||||
{
|
||||
printf("%d ", x);
|
||||
}
|
||||
}
|
||||
printf("\n素数和: %d\n", num_sushuhe(n, m));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// 逃逸字符:表示无法直接输入的字符(如换行、制表符)或特殊符号(如双引号、反斜杠本身)
|
||||
// 避免语法冲突,例如在字符串中嵌套双引号
|
||||
printf("输入\"5 7\"表示5英尺7英寸");
|
||||
|
||||
// \b回退一格
|
||||
printf("123\b\n456\n");
|
||||
|
||||
// \t制表符,到下一个表格位(t是tab的缩写,相当于键盘上的tab键)
|
||||
printf("12\t456\n");
|
||||
|
||||
// \n换行
|
||||
printf("123\n456\n");
|
||||
|
||||
// \r回车
|
||||
printf("123\r456\n");
|
||||
|
||||
// \"双引号(")
|
||||
printf("123\"456\n");
|
||||
|
||||
// \'单引号(')
|
||||
printf("123\'456\n");
|
||||
|
||||
// \\反斜杠(\)
|
||||
printf("123\\456\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !#include <stdbool.h>之后,bool类型就可用,即true和false
|
||||
// 逻辑运算符:&&(与)、||(或)、!(非)
|
||||
// 逻辑运算符的优先级低于关系运算符,高于算术运算符
|
||||
// 逻辑运算符的运算对象和求值结果都是bool类型
|
||||
// 逻辑运算符的运算对象可以是任意能转换成bool类型的值
|
||||
|
||||
bool b = 6 > 5; // b为true
|
||||
bool t = true;
|
||||
t = 2;
|
||||
printf("%d\n", t); // bool类型只能以%d输出,即结果为1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user