HDU 4608 I-number (数学&字符串处理)

I-number


http://acm.hdu.edu.cn/showproblem.php?pid=4608


Time Limit: 10000/5000 MS (Java/Others)    

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.
 

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

题意:求一个大于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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值