Repeat Number(数论)

Repeat Number

题目描述:

Definition: a+b = c, if all the digits of c are same ( c is more than ten),

then we call a and b are Repeat Number. 

My question is How many Repeat Numbers in [x,y].

输入

There are several test cases.

Each test cases contains two integers x, y(1<=x<=y<=1,000,000) described above.

Proceed to the end of file.

输出

For each test output the number of couple of Repeat Number in one line.

样例输入

1 10

10 12

样例输出

5

2

提示:

If a equals b, we can call a, b are Repeat Numbers too, and a is the Repeat Numbers for itself.

 

题目大意:

     输入一个范围[x,y],判断在这个范围内能够找到多少组<a,b>使得a+b=c,

     c需要满足每一位上的数全都一样并且 c > 10。

 

解题思路:

    先根据题目中给定的范围,将范围内所有满足条件的c存入数组。

    然后根据输入的范围求出最小范围x+x和最大范围y+y,在该范围

    内找满足条件的组合。

 

AC代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <math.h>
 5 #include <stdlib.h>
 6 
 7 using namespace std;
 8 
 9 int aa[50];
10 int f()   // 将满足情况的c值存入数组
11 {
12     int i,j,k=0;
13     for (i = 11; i < 1000000; i=i*10+1)
14         for (j = 1; j <= 9; j ++)
15             aa[k ++] = i*j;
16     aa[k] = 1111111;   // 不要忘记这个最大的哦(1111111  在这个最大的范围内 1000000+1000000)
17 }
18 int main ()
19 {
20     f();
21     int a,b,j,i;
22     while (scanf("%d%d",&a,&b)!=EOF)
23     {
24         int sum = 0;
25         for (i = 0; i < 47; i ++)
26         {
27             if (a*2 <= aa[i] && aa[i] <= b*2)  // 判断是否在[a+a,b+b]范围内
28                 sum += min(aa[i]/2-a+1,b-(aa[i]+1)/2+1);  
29         }
30         printf("%d\n",sum);
31     }
32     return 0;
33 }
34 
35 /*
36  sum += min(aa[i]/2-a+1,b-(aa[i]+1)/2+1);  
37  关于以上代码的解释:
38  
39  举一个简单的例子  假如输入的x,y为1 10
40  只有一个11在[2,20]范围内
41  想一想那些数能够组合成11
42  (1,10)(2,9)(3,8)(4,7)(5,6)这五个
43  以(5,6)这一组为分界
44  (1(2(3(4(5,6)7)8)9)10)
45  
46  假如输入的x,y为3 10
47  也只有一个11在[6,20]范围内 
48  满足条件的组合
49  (3,8)(4,7)(5,6)这五个
50  以(5,6)这一组为分界
51 (3(4(5,6)7)8)
52 
53 所以,根据c/2 到x和y的距离就能判断出有多少组合(选择较小值)
54  */

 

 



 

 

posted @ 2017-04-25 22:19  gaoyanliang  阅读(486)  评论(0编辑  收藏  举报