Coderforces 204A

此题比赛时没有好的方法不会做,最后从别人那里学到了一个好的方法,就是从小到大慢慢数,从11到99开始数数,直到l < 100...1 < r < 9000..9

当加到这一步时,输出count就是所求结果,比如 47   1024  首先是两位数的 11 到99 ,47在里面 ,此时数目是5,再到三位数,101 到 999,此时1024大于他们

数目count直接加上9*10(101 到191共10个),接着是四位数,1001 到 9999 ,1024在里面,此事要计算1001到1024首尾相同的数的个数,总共有3个,所以

总数就是98个。

View Code
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<iostream>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int i, j;
13     long long a, b;
14     long long x, l, r, count, num;
15         
16     count = 0LL;
17     x = 10;
18     cin>>l>>r;
19        
20      for(i=1; i<=9; i++)
21      {
22         if(l<=i && i<=r)
23            count++;
24      }      
25      for(int len=2; len<=18; len++)//长度从小到大递增
26      {
27          for(int d=1; d<=9; d++)//每个长度的数,都要计算count个数
28          {
29             a = d*x + d;   
30             b = (d + 1)*x - 10 + d;
31             long long ln = max(a,l);
32             long long rn = min(b,r);
33    
34             if(ln <= rn)
35             {
36                long long l0 = ln/10;     
37                if(ln%10 > d)//比如上面的r == 47时
38                    l0++;  
39                long long r0 = rn/10;
40                if(rn%10 < d)
41                    r0--;
42                    
43                 count += r0 - l0 + 1;   
44             } 
45            }
46            x = x*10;
47         }
48          
49     printf("%I64d\n",count);
50     //system("pause");
51      return 0;
52 }    

 

posted @ 2012-08-10 10:58  zhongya  阅读(169)  评论(0编辑  收藏  举报