“玲瓏杯”#19 A -- A simple math problem 找規律

DESCRIPTION

You have a sequence an, which satisfies:

Now you should find the value of 10an

.

INPUT
The input includes multiple test cases. The number of test case is less than 1000.Each test case contains only one integer n(1n109)

OUTPUT
For each test case, print a line of one number which means the answer.

SAMPLE INPUT
5
20
1314

SAMPLE OUTPUT
5
21
1317

    抱枕杯簽到題,結果糾結了半天才做出來...不小心把後面的an都當做小於1來處理了...
    這道題題意不用多說,就是圖片裏的公式,求10an,因爲是向下取整,所以對於公式an,在an小於10的時候,一定是一個小於1的數。同理,在an位於10到100之間時,一定是一個小於2的數,這樣的話大部分數可以通過n-位數+1的公式得到。比較特殊的是幾個節點,因爲差是不斷增加的,所以依次爲10,99,998....
    下面AC代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[15];

int init()
{
    int i;
    a[1]=10;
    for(i=2;i<=9;i++)
    {
        a[i]=a[i-1]*10;
    }
    return 0;
}

int main()
{
    int n;
    int t;
    int tim;
    int flag;
    init();
    while(scanf("%d",&n)!=EOF)
    {
        flag=0;
        if(n==1)
        {
            cout<<1<<endl;
            continue;
        }
        tim=0;
        t=n;
        while(t>10)
        {
            t=t/10;
            tim++;
        }
        tim++;
        if(a[tim]-n<tim-1)
            flag=1;
        if(flag==0)
            cout<<n+tim-1<<endl;
        else
            cout<<n+tim<<endl;
    }
    return 0;
}


發佈了140 篇原創文章 · 獲贊 197 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章