Description 给你两个数a和b,你的任务是计算出1在a和b之间出现的次数,比如说,如果a=1024,b=1032,那么a和b之间的数就是: 1024 1025 1026 1027 1028 1029 1030 1031 1032 则有10个1出现在这些数中。 Input 输入不会超过500行。每一行有两个数a和b,a和b的范围是0 < a, b < 100000000。输入两个0时程序结束,两个0不作为输入样例。 Ouput 对于每一对输入的a和b,输出一个数,代表1出现的个数。 Sample Input 1 10 44 497 346 542 1199 1748 1496 1403 1004 503 1714 190 1317 854 1976 494 1001 1960 0 0 Sample Output 2 185 40 666 113 105 1133 512 1375 1256
#include<stdio.h>
#include<stdlib.h>
const int N=1;
int count(int x){
int cnt=0;
for(int i=0;i<=x;i++){
int t=i;
while(t)
{
if(t%10==N)
cnt++;
t/=10;
}
}
return cnt;
}
int main(){
int a,b,result;
printf("请输入两个数:\n");
scanf("%d %d",&a,&b);
result=count(b)-count(a-1);
printf("%d\n",result);
system("pause");
}
这只是一种解法。