题目描述
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个整数n(n<10^100),统计并输出n中2 的个数。
输入
见sample
输出
见sample
样例输入
3
-21902
2
345543
样例输出
count=2
count=1
count=0
提示
来源
【想法】
既然大学了,想法就不要局限。谁规定题目里说是数字就要用int的,如果还是高中的数字/10 &%10时间就超时了
换成char字符串来,有一点地址的想法却不多
#include<stdio.h>
#include<string.h>
int main()
{
char w[10000];
int n,m,num,i;
scanf("%d",&n);
getchar();
while(n > 0){
gets(w);//scanf("%s",w)等价也可以加&,w:首地址位置
m=strlen(w);num=0;//strlen用来返回字符串字符个数,num用来记录2的个数
for(i=0;i < m;i++){
if(w[i]=='-') continue;//由于是字符串就要考虑'-'
if(w[i]=='2') num++;
}
printf("count=%d",num);
if(n > 1) printf("\n");//好像有些题目最后输出也可以有“\n”
n--;
}
return 0;
}