1.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
#include<stdio.h> #include<string.h> int main() { int letter = 0, space = 0, number = 0, other = 0, i; char ch[100]; gets_s(ch); for (i = 0;i<strlen(ch); i++) { if (ch[i] >= 'A'&&ch[i] <= 'Z' || ch[i] >= 'a'&&ch[i] <= 'z') letter++; else if (ch[i] == ' ') space++; else if (ch[i] <= '9'&&ch[i] >= '0') number++; else other++; } printf("letter=%d\nspace=%d\nnumber=%d\nother=%d\n", letter, space, number, other); }
2.求Σ(k=1~100)k+Σ(k=1~50)k^2+Σ(k=1~10)1/k
#include<stdio.h> int main() { int k,a=0,b=0,c=0; for (k = 1; k <= 100; k++) a = a + k; for (k = 1; k <= 50; k++) b = b + k*k; for (k = 1; k <= 10;k++) c = c + 1 / k; printf("%d\n", a + b + c); return 0; }
3.输出所有的“水仙花”数
#include<stdio.h> int main() { int n,i,j,k; for (n = 100; n <= 999; n++) { i = n / 100; j = (n - i * 100) / 10; k = n % 10; if (n == i*i*i + j*j*j + k*k*k) printf("%d ", n); } printf("\n"); return 0; }