//题目17:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //分析:将字符串存入字符数组,用for分别检索英文字母、空格、数字和其它字符 //char型可以转成Int类型,通过ASCII表就可以得出数字的范围时48~57;字母的范围是65~90;97~122;空格是32 void main(){ char str[30] = "adfa-123 12 asdf‘sad13"; int num = 0; int ch = 0; int nul = 0; int other = 0; int temp = 0; for (int i = 0; i < 30; i++) { if (str[i]==‘\0‘) { break; } else{ temp = (int)str[i]; if (temp>47 && temp<58) { num++; } else if ((temp>64 && temp<91) || (temp>96 && temp < 123)){ ch++; } else if (temp==32) { nul++; } else{ other++; } } } printf("\n数字的个数%d,字母的个数%d,空格的个数%d,其他字符的个数%d。",num,ch,nul,other); system("pause"); }
//题目18:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时 //共有5个数相加),几个数相加有键盘控制。 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<math.h> //分析:键盘输入数字,决定相加的个数 // int getnum(int num){ int a = 2; int res = 0; for (int i = num; i >-1; i--) { res += a*(int)(pow(10, i)); } return res; } void main(){ int num = 0; scanf("%d",&num); int s = 0; //方法1 /*for (int i = 0; i <num; i++) { s += getnum(i); }*/ //方法2 int count = 0; int a = 2; int tn = 0; while (count < num){ //每次实现加的那个数的值,a永远是200..0,tn是2222,两者相加 就变成正确的数 tn = tn + a; s += tn; a = a * 10; count++; } printf("\n%d",s); system("pause"); }
原文:http://www.cnblogs.com/zhanggaofeng/p/5149917.html