Posted on 2012-09-23 16:35
hoshelly 閱讀(250)
評論(0) 編輯 收藏 引用 所屬分類:
Programming
給你兩個數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不作為輸入樣例。
Output
對于每一對輸入的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<string.h>
int Find_OneNum(long a)
{
long i,b,count=0;
while(a)
{
b=a%10;
if(b == 1)
{
count++;
}
a=a/10;
}
return count;
}
int main()
{
long a,b,i,sum;
while((scanf("%ld%ld",&a,&b) == 2),a)
{
if(a>b)
{
int temp;
temp=a;
a=b;
b=temp;
}
sum=0;
for(i=a;i<=b;i++)
{
sum += Find_OneNum(i);
}
printf("%ld\n",sum);
}
return 0;
}