计数问题

it2024-12-24  46

 

计数问题

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Description

给你两个数ab,你的任务是计算出1ab之间出现的次数,比如说,如果a=1024,b=1032,那么ab之间的数就是:

1024 1025 1026 1027 1028 1029 1030 1031 1032

则有101出现在这些数中。

Input

输入不会超过500行。每一行有两个数abab的范围是0 < a, b < 100000000。输入两个0时程序结束,两个0不作为输入样例。

Output

对于每一对输入的ab,输出一个数,代表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"RunTime.h" #include <iostream> using namespace std; int countOne(int n) { int f = 1; int count = 0; int l = 0; int h = 0; int c = 0; while (n/f) { l = n %f; c = (n/f)%10; h = n/(f*10); count += h * f; switch(c) { case 0://通过分析可知,当当前位为0时,1的个数只受高位的影响 break; case 1://为1时,受高位和低位的影响 count += l + 1; break; default://大于1的时候为高位的数+1 count += f; } f*=10; } return count; } int main() { RunTime rt; int m,n; while (1) { cin>>m>>n; if(m==0&&n==0)break; rt.Begin(); if(m>n) cout<<countOne(m)-countOne(n-1)<<endl; else cout<<countOne(n)-countOne(m-1)<<endl; rt.End(); rt.ShowTime(); } return 0; }  

 

转载于:https://www.cnblogs.com/ituff/archive/2011/04/17/2858552.html

最新回复(0)