关于for的使用!!
1、输入一行字符,统计并输出其中的英文字母、数字字符和其他字符的个数!!2、s=1!+(1!+2!)+(1!+2!+3!)+…+(1!+2!+3!…n!) n有键盘输入
#include < stdio.h > #include < stdlib.h > #include < ctype.h > void main ( ) { char ch; int sum1 = 0; int sum2 = 0; int sum3 = 0; int sum4 = 0; while((ch=getchar())!='\n') { if ( isdigit ( ch ) != 0 ) sum1++; else if ( isspace ( ch ) != 0 ) sum2++; else if ( islower ( ch ) != 0 || isupper ( ch ) != 0 ) sum3++; else sum4++; } printf ( "%5d%5d%5d%5d", sum1, sum2, sum3, sum4 ); system ( "pause" ); }下题晚上来,上自习去了
#include <stdio.h> long long fac(long long ); int main(void) { long long n, s, i; printf("Enter n: \n"); scanf("%lld",&n); for (i = 1, s = 0; i <= n; i++) { s = s + fac(i) * (n - i + 1); } printf("result = %lld\n",s); return 0; } long long fac(long long i) { long long int f; if (i == 0 || i == 1) { f = 1; } else { f = fac(i - 1) * i; } return f; }
#include < stdio.h > void main ( ) { char c; int letters=0,space=0,numbers=0,others=0; printf("please input some characters\n"); while((c=getchar())!='\n') { if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++; else if(c==' ') space++; else if(c>='0'&&c<='9') numbers++; else others++; } printf("All characters: \nchar=%d\nspace=%d\nnubers=%d\nothers=%d\n",letters,space,numbers,others); }