Repeat Number

1614: Repeat Number

時間限制: 1 Sec  內存限制: 32 MB
提交: 134  解決: 39
[提交][狀態][討論版]

題目描述

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.

暴力是會T 的,正確的做法是先預處理,即打表。x+y=a[i],那麼x的個數爲a[i]/2-x+1。但是這樣會WA,沒有考慮全面,x+y=a[i]有a[i]/2種組合,但是我們應該取【x,a[i]/2】,【a[i]/2,y】兩者區間的最小值,才能保證x,y都在題目所給的區間範圍內。

#include<bits/stdc++.h>
using namespace std;
int a[100],n;
void init()
{
    n=0;
    for(int i=1;i<=9;i++)
    {
        int s=i*10+i;
        a[n++]=s;
        while(s<=2000000)
        {
            s=s*10+i;
            a[n++]=s;
        }
    }
    sort(a,a+n);
}
int main()
{
    init();
    int x,y;
    while(scanf("%d%d",&x,&y)==2)
    {
        if(y*2<a[0]||x*2>a[n-1])
        {
            printf("0\n");
            continue;
        }
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]>=x*2&&a[i]<=y*2)
                ans+=min(a[i]/2-x+1,y-(a[i]+1)/2+1);
        }
        printf("%d\n",ans);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章