zcmu 1445: Repeat Number(思维)

【题目】

1445: Repeat Number

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 202  Solved: 60
[Submit][Status][Web Board]

Description

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].

Input

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.

Output

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

Sample Input

1 10 10 12

Sample Output

5 2

HINT

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

【题解】

题意:若存在a,b满足a+b=c且c的所有位上的数字相同,则称a,b为一对重复数。给你一段区间,问存在多少对重复数。

思路:a,b可相等,易得c的范围为[2*x,2*y],打表所有可能的c,再根据范围找出符合要求的c,最后根据x,y找到右边界与左边界计数即可。

【代码】

#include<bits/stdc++.h>
using namespace std;
int a[]={11,22,33,44,55,66,77,88,99,111,222,333,444,555,666,777,888,999,1111,2222,3333,4444,
5555,6666,7777,8888,9999,11111,22222,33333,44444,55555,66666,77777,88888,99999,
111111,222222,333333,444444,555555,666666,777777,888888,999999,1111111};
main()
{
    int x,y;
    while(~scanf("%d%d",&x,&y))
    {
        int l=lower_bound(a,a+46,2*x)-a;
        int r=upper_bound(a,a+46,2*y)-a-1;
        int sum=0;
        for(int i=l;i<=r;i++)
        {
            if(a[i]-x<=y)
            {
                int n=(a[i]-x)-x+2;
                sum+=n/2;
            }
            else
            {
                int n=y-(a[i]-y)+2;
                sum+=n/2;
            }
        }
        printf("%d\n",sum);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41117236/article/details/81153679