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;}