HDU4608 I-number

來源:互聯網
上載者:User
                                                                    I-number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1369    Accepted Submission(s): 554

Problem DescriptionThe I-number of x is defined to be an integer y, which satisfied the the conditions below:
1. y>x;
2. the sum of each digit of y(under base 10) is the multiple of 10;
3. among all integers that satisfy the two conditions above, y shouble be the minimum.
Given x, you're required to calculate the I-number of x. 

InputAn integer T(T≤100) will exist in the first line of input, indicating the number of test cases.
The following T lines describe all the queries, each with a positive integer x. The length of x will not exceed 105. 

OutputOutput the I-number of x for each query. 

Sample Input

1202
 

Sample Output

208
 

Source2013 Multi-University Training Contest 1
 

Recommendliuyiding


解題思路:看給的時間,應該知道可以直接暴力解決,直接連續自加直到所有數字之和為10的倍數為止。本題涉及到達數,不能直接用某個資料類型儲存,用數組類比存貯即可,看到有大神用數組類比萬進位儲存,在下不才,還處於小鳥階段,只能做到數組十進位儲存,容易理解。注意,本題坑爹的地方在:如果輸入得資料中有前置字元為零時,輸出也要注意前置字元為零,還要注意進位。

#include<stdio.h>#include<cstring>using namespace std;char num[100005];int main(){    int t;    int sum;    int i,j;    scanf("%d",&t);    while(t--)    {        sum=1;        num[0]='0';        num[1]='0';        scanf("%s",num+2);        int n=strlen(num);        for(i=0;i<n;i++)      //轉換為數值儲存            num[i]=num[i]-'0';        while(sum%10)     //檢查10的倍數        {            sum=0;            j=n-1;            num[j]++;      //自加1            while(num[j]>=10)   //進位            {                num[j-1]+=num[j]/10;                num[j]%=10;                sum+=num[j];                j--;            }            for(;j>=0;j--)    //每個數字相加                sum+=num[j];        }        i=1;       if(num[i]==0)    //檢查最高位是否進位            i++;        for(;i<n;i++)   //直接輸出,不要避開前置0            printf("%d",num[i]);        printf("\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.