1 闰年判断
2 完数判断 ‘完数’指一个数恰好等于它的因子之和,如 6=1+2+3。
#include<stdio.h> int main() { int i, sum=0, n; scanf("%d", &n); for (i = 1; i < n; i++) { if (n % i == 0) sum += i; } if (sum == n) { printf("%d是完数\n", n); } return 0; }
3 水仙花数 指一个三位数等于它每一位的立次方之和
#include<stdio.h> int main() { int i, g, s, b,sum=0; //g s b 分别代表个位,十位,百位 for (i = 100; i < 1000; i++) { g = i % 10; s = (i / 10) % 10; b = i / 100; sum = g * g * g + s * s * s + b * b * b; if (i == sum) printf("%d ", i); } return 0; }
4 斐波那契数
#include<stdio.h> int main() { int arr[100] = { 1,1 }; int i = 0, n; scanf("%d", &n); for (i = 2; i < n; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } for(i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
5 求1!+2!+3!+4!...n!
#include<stdio.h> int main() { int n, i, j; double x = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { x = x + 1.0 / i; } printf("%.2lf", x); return 0; }
6求x=1+1/1+1/2+1/3+……..+1/n
#include<stdio.h> int main() { int i, sum = 0, x = 1, n; scanf("%d", &n); for (i = 1; i <= n; i++) { x *= i; sum += x; } printf("%d", sum); return 0; }
7 输入年月日,判断这是这一年的第几天
#include<stdio.h> int main() { int year, month, days, sum=0; scanf("%d %d %d", &year, &month, &days); if ((year % 4 == 0 && year % 100!= 0) || (year % 400 == 0)) //判断闰年 if (month > 2) { sum = 1; } switch (month) { case 1:sum = sum+ 0; break; case 2:sum = sum+ 31; break; case 3:sum = sum + 31 + 28; break; case 4:sum = sum + 31 + 28 + 31; break; case 5:sum = sum + 31 + 28 + 31 + 30; break; case 6:sum = sum + 31 + 28 + 31 + 30 + 31; break; case 7:sum = sum + 31 + 28 + 31 + 30 + 31 + 30; break; case 8:sum = sum + 31 + 28 + 31 + 30 + 31 + 30 + 31; break; case 9:sum = sum + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10:sum = sum + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30; break; case 11:sum = sum + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12:sum = sum + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; default: break; } sum = sum + days; printf("%d\n", sum); return 0; }
8 输入两个正整数m和n,求其最大公约数和最小公倍数
(m与n最小公倍数)=m*n/最大公约数 #include<stdio.h> int main() { int n, m, t = 1, x; scanf("%d%d", &n, &m); if (m > n) { t = m; m = n; n = t; } t = n % m; x = n * m; if (t == 0) { printf("最大公约数是:%d\n最小公倍数是:%d", m, n); } else { // 找m与n的最大公约数 while (n % m != 0) { t = n % m; n = m; m = t; } printf("最大公约数是:%d\n最小公倍数是:%d", t, x / t); } return 0; }
9 有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
#include<stdio.h> int main() { int i, j, k; for (i = 0; i <= 4; i++) { for (j = 0; j <= 4; j++) { for (k = 0; k <= 4; k++) { if (i != j && j != k && i != k)//判断i,j,k是否都不相等 printf("%d,%d,%d\n", i, j, k); } } } return 0; }
10 打印图形--菱形
#include<stdio.h> int main() { int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { //先输出菱形的上半 for (j = 1; j <= n-i; j++) { //控制每一行的空格数 printf(" "); } for (j = 0; j < 2 * i - 1; j++) { printf("*"); } printf("\n"); } for (i = n-1; i >0; i--) { //菱形的下半就是将上半反过来 for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 0; j < 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }