好长时间没写博客了,一想起“水可三日不饮,饭可七日不食,而程不可一日不编”,还是回顾一下最近的内容,下午打了场多校说下这道A下的第一题。
Problem Description
The 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.
Input
An 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 10 5.
Output
Output the I-number of x for each query.
Sample Input
1 202
Sample Output
208
具体过程就是末尾加 1 ,直到满足条件,注意一些细节。
#include "string.h"
#include "stdio.h"
char str[100005];
int num[100005];
int main()
{
int i,j,k,sum,T,need,flag;
int end;
scanf("%d",&T);
while(T--)
{
flag=0;
memset(str,0,sizeof(str));
memset(num,0,sizeof(num));
scanf("%s",str);
end=strlen(str)-1;
for(i=0,sum=0;i<=end;i++)
{
num[i]=str[i]-'0';
sum+=num[i];
}
sum=sum%10; //记录一下当前的和
while(1)
{
str[end]++;
sum++;
i=end;
while(str[i] > '9') // 考虑进位
{
sum-=10; //这个减10,后面又加1,就和谐了
str[i]='0';
if(i == 0) //考虑向前进位的情况,比如输入为9时,但显然任何数要进只用进一次
{
flag=1;
sum++;
break;
}
str[i-1]++;
sum++;
i--;
}
if(sum%10 == 0) break;
}
if(flag == 1)
{
printf("1%s\n",str); //向前进位的情况
}
else
{
printf("%s\n",str);
}
}
}