Initial commit: C language learning code
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int n=0;
|
||||
int one,two,five;
|
||||
|
||||
printf("请输入钱的数额:");
|
||||
scanf("%d", &x);
|
||||
|
||||
for(one=0;one<=x*10;one++)
|
||||
{
|
||||
for(two=0;two<=x*5;two++)
|
||||
{
|
||||
for(five=0;five<=x*2;five++)
|
||||
{
|
||||
if(one*1 + two*2 + five*5 == x*10)
|
||||
{
|
||||
n +=1;
|
||||
printf("%d元的钱可以用%d个1角、%d个2角和%d个5角换\n", x, one, two, five);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("一共有%d的可能性", n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int one,two,five;
|
||||
|
||||
printf("请输入钱的数额:");
|
||||
scanf("%d", &x);
|
||||
|
||||
for(one=0;one<=x*10;one++)
|
||||
{
|
||||
for(two=0;two<=x*5;two++)
|
||||
{
|
||||
for(five=0;five<=x*2;five++)
|
||||
{
|
||||
if(one*1 + two*2 + five*5 == x*10)
|
||||
{
|
||||
printf("%d元的钱可以用%d个1角、%d个2角和%d个5角换\n", x, one, two, five);
|
||||
goto out; //多层嵌套的循环的内层直接结束跳到最外层的out:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out:
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
int a=1;
|
||||
int b=1;
|
||||
|
||||
while(b<=n)
|
||||
{
|
||||
a=a*b;
|
||||
b=b+1;
|
||||
}
|
||||
printf("n!=%d\n", a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
int a=1;
|
||||
int b=n;
|
||||
|
||||
for(n=n;n>1;n-- )
|
||||
{
|
||||
a=a*n;
|
||||
}
|
||||
printf("%d!=%d\n", b, a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
int a=1;
|
||||
int b=n;
|
||||
|
||||
for(n=n;n>1;n-- )
|
||||
{
|
||||
a=a*n;
|
||||
}
|
||||
printf("%d!=%d\n", b, a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int x;
|
||||
double f=0.0; //f不是整数,是小数,用double表示
|
||||
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
for (x=1 ; x<=n; x++)
|
||||
{
|
||||
f = f+(1.0/x); //1是整数,不是小数,用1.0表示小数的运算
|
||||
}
|
||||
|
||||
printf("f(%d)=%f\n", n, f); //f不是整数,是小数,用%f表示浮点数
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int m;
|
||||
int x;
|
||||
double f=0.0; //f不是整数,是小数,用double表示
|
||||
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
for (x=1 ; x<=n; x++)
|
||||
{
|
||||
m = 1;
|
||||
if (x%2==0)
|
||||
{
|
||||
m = -1;
|
||||
}
|
||||
f = f+(1.0/x)*m;
|
||||
}
|
||||
|
||||
printf("f(%d)=%f\n", n, f); //f不是整数,是小数,用%f表示浮点数
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int m=1;
|
||||
int x;
|
||||
double f=0.0; //f不是整数,是小数,用double表示
|
||||
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
for (x=1 ; x<=n; x++)
|
||||
{
|
||||
f = f+(1.0/x)*m;
|
||||
m = -1;
|
||||
}
|
||||
|
||||
printf("f(%d)=%f\n", n, f); //f不是整数,是小数,用%f表示浮点数
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int gcd(int a,int b)
|
||||
{
|
||||
int c;
|
||||
while(c>0)
|
||||
{
|
||||
c = a % b;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
int lcm(int a, int b)
|
||||
{
|
||||
return (a / gcd(a, b)) * b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
|
||||
if (a<0 || b<0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d和%d的最大公约数是:%d\n", a, b, gcd(a, b));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int num_small(int a,int b)
|
||||
{
|
||||
int n = a;
|
||||
if (a>b)
|
||||
{
|
||||
n = b;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int gcd=0;
|
||||
int a;
|
||||
int b;
|
||||
int n;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
|
||||
if (a<0 || b<0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = num_small(a,b);
|
||||
|
||||
for (gcd=n;gcd>=1;gcd--)
|
||||
{
|
||||
if (a%gcd==0 && b%gcd==0)
|
||||
{
|
||||
printf("最大公约数是:%d\n", gcd);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int gcd(int a,int b) //最大公约数
|
||||
{
|
||||
int gcd = a;
|
||||
if (a>b)
|
||||
{
|
||||
gcd = b;
|
||||
}
|
||||
for (;gcd>=1;gcd--)
|
||||
{
|
||||
if (a%gcd==0 && b%gcd==0)
|
||||
{
|
||||
return gcd;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
|
||||
if (a<0 || b<0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d和%d的最大公约数是:%d\n", a, b, gcd(a, b));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int a=0;
|
||||
int n=0;
|
||||
int m=0;
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &x);
|
||||
|
||||
do{
|
||||
a = x%10;
|
||||
n = n*10+a;
|
||||
x = x/10;
|
||||
} while (x>0);
|
||||
|
||||
do{
|
||||
m = n%10;
|
||||
printf("%d", m);
|
||||
if (n>=10)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
n = n/10;
|
||||
} while (n>0);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.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 m=0;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
int a = num_weishu(x);
|
||||
|
||||
if(a>9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (a>0)
|
||||
{
|
||||
m = x/a;
|
||||
printf("%d", m);
|
||||
x = x%a;
|
||||
a = a/10;
|
||||
if (a>1)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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, sign = 1;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
|
||||
if (x < 0) // 处理负数符号
|
||||
{
|
||||
sign = -1;
|
||||
x = -x; // 转换为正数处理
|
||||
}
|
||||
|
||||
int a = num_weishu(x);
|
||||
if (a > 9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int b=1;
|
||||
while (a>0)
|
||||
{
|
||||
int m = x/a; // 第一个数字应用符号
|
||||
if (b)
|
||||
{
|
||||
printf("%d", sign*m);
|
||||
b = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d", m);
|
||||
}
|
||||
x %= a;
|
||||
a /= 10;
|
||||
if (a > 0) printf(" "); //后续数字后加空格
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#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;
|
||||
printf("%d", m);
|
||||
if (b>9)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
x = x%b;
|
||||
b = b/10;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
int a;
|
||||
int b=1;
|
||||
|
||||
for(a=2;a<n;a++)
|
||||
{
|
||||
if(n%a==0)
|
||||
{
|
||||
b=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(b==1)
|
||||
{
|
||||
printf("%d是素数", n);
|
||||
}
|
||||
else if (b==0)
|
||||
{
|
||||
printf("%d不是素数", n);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int x=0;
|
||||
printf("输入数字:");
|
||||
scanf("%d", &x);
|
||||
|
||||
for(n=2;n<=x;n++)
|
||||
{
|
||||
int a;
|
||||
int b=1;
|
||||
|
||||
for(a=2;a<n;a++)
|
||||
{
|
||||
if(n%a==0)
|
||||
{
|
||||
b=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(b==1)
|
||||
{
|
||||
printf("%d\n", n);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
// !素数判断的简化版本
|
||||
int sushu(int x)
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
if (x <= 1)
|
||||
{
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
int i;
|
||||
if (x == 2 || (x % 2 == 0 && x != 2)) // 排除偶数
|
||||
{
|
||||
ret = 0;
|
||||
}
|
||||
for (i = 3; i < (int)sqrt(x)+1; i += 2)
|
||||
{
|
||||
if (x % i == 0)
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
printf("输入数字:");
|
||||
scanf("%d", &x);
|
||||
|
||||
if (sushu(x))
|
||||
{
|
||||
printf("%d是素数\n", x);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d不是素数\n", x);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user