I-number
http://acm.hdu.edu.cn/showproblem.php?pid=4608
Memory Limit: 32768/32768 K (Java/Others)
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.
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.
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
题意:求一个大于x的且最接近x的数y,y所有数位上的数字和sum是10的倍数。
思路:由于x的长度可达 10^5,采用字符串处理即可。
关键是判断能不能在不进位(只改变最后一个数)的时候找到y,此时把最后一位加上10 - sum%10就行。(需满足条件:最后一位<sum%10)
若最后一位进位,就先判断仅为之后所有位之和,比如57999进位后变58000,sum%10=3,那么最后一位加上10 - sum%10就行
特判:所有位数为9,那直接先输出一个1,然后把x最后一位变成9,剩下变成0就行。
完整代码:
/*171ms,332KB*/
#include<cstdio>
#include<cstring>
char x[100002];
int main()
{
int t;
scanf("%d", &t);
getchar();
while (t--)
{
memset(x, 0, sizeof(x));
gets(x);
int len = strlen(x), sum = 0, i;
for (int i = 0; i < len; ++i)
sum += x[i] - '0';
sum %= 10;
if (x[len - 1] - '0' < sum)
x[len - 1] += 10 - sum;
else
{
x[len - 1] = '9';
sum = 0;
for (i = len - 1; i >= 0; --i)
if (x[i] != '9')
break;
else
x[i] = '0';
if (i == -1)
{
x[len - 1] = '9';
putchar('1');
printf("%s\n", x);
continue;
}
++x[i];///进位
for (int j = 0; j <= i; ++j)
sum += x[j] - '0';
sum %= 10;
if (sum)
x[len - 1] += 10 - sum;
}
printf("%s\n", x);
}
return 0;
}