I-number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3968 Accepted Submission(s): 1415
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
Source
Recommend
liuyiding
题目大意:
输入一个数,求比这个数大的各位数字之和为10的倍数的数字中最小的数字。
解题思路:
由于所求的数的各个数字之和为10的倍数,所以所求的数减去输入的数一定不会超过10,所以可以将输入的数不断加一,当满足要求时输出。因为数的长度可以达到10的5次方,所以必须用字符串储存数字,并模拟加一。
模拟时要在输入数字的最前面加一位0,以防进位。由于长度非常大,所以如果每次把各个位的和求一遍的话应该会超时(我没有试),所以需要在每次加一时直接对和进行更新:每加一和就加一,每次进位和减8(由于存在减所以和可能成为负数,所以要在每次操作后检查和是否位负数,如果为负数,不断加10到正数为止)
附AC代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int max_len=100000+5;
char num[max_len];
int sum;
void reset_sum()//维持和为正数
{
while(sum<=0)
sum+=10;
}
void up(int pos)//加一操作
{
num[pos]=num[pos]+1;
if(num[pos]==('0'+10))
{
sum-=9;
reset_sum();
num[pos]='0';
up(pos-1);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%s",num+1);//输入数字
num[0]='0';
size_t len=strlen(num);
for(int i=0;i<len;++i)
sum+=num[i]-'0';//求各位之和
while(true)
{
++sum;
up(len-1);
if(sum%10==0)
break;
}
if(num[0]!='0')//输出结果
puts(num);
else
{
for(int i=1;i<len;++i)
printf("%c",num[i]);
putchar('\n');
}
}
return 0;
}