Posted on 2012-09-23 16:35
hoshelly 閱讀(242)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
Programming
給你兩個(gè)數(shù)a和b,你的任務(wù)是計(jì)算出1在a和b之間出現(xiàn)的次數(shù),比如說(shuō),如果a=1024,b=1032,那么a和b之間的數(shù)就是:
1024 1025 1026 1027 1028 1029 1030 1031 1032
則有10個(gè)1出現(xiàn)在這些數(shù)中。
Input
輸入不會(huì)超過(guò)500行。每一行有兩個(gè)數(shù)a和b,a和b的范圍是0 < a, b < 100000000。輸入兩個(gè)0時(shí)程序結(jié)束,兩個(gè)0不作為輸入樣例。
Output
對(duì)于每一對(duì)輸入的a和b,輸出一個(gè)數(shù),代表1出現(xiàn)的個(gè)數(shù)。
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;
}