HDU 4608 I-number(模拟)

本文介绍了一个有趣的算法问题——I-number的实现方法。该问题要求找到大于给定数值x的最小整数y,使得y的各位数字之和为10的倍数。文章提供了一段C++代码示例,详细展示了如何通过字符串模拟处理可能非常大的输入,同时考虑了前导零的情况。

I-number

Time Limit: 5000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描写叙述

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.

输入

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 the I-number of x for each query.

演示样例输入

1
202

演示样例输出

208

提示

来源

2013 Multi-University Training Contest 1
 
我不得不说这道题真的非常坑,我要是不搜题解预计是毁掉了 本来题意非常easy,就是给定一个x(因为可能非常大用字符串模拟),求出比x大的且各位数字之和为10的倍数的数,
要求输出符合上诉条件的最小数。

but,这个x居然是能够有前导0的(它没说)也就是说输入 00202 输出 00208

 
代码略挫QAQ
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <cctype>
using namespace std;
char x[100010];
int digitsum()
{
	int sum=0;
	for(int i=0;i<strlen(x);i++)
		sum+=(x[i]-'0');
	return sum;
}
int is_o()
{
	for(int i=0;i<strlen(x);i++)
		if(x[i]!='0') return 0;
	return 1;
}
int main()
{
 int i,T;
 cin>>T;
 getchar();
 while(T--)
 {
 	cin>>x;
 	int s=-99;
 	while(s%10)
	{
		int p=strlen(x)-1;
		int len=strlen(x);
		x[p]++;
		while(x[p]>'9')
		{
			x[p--]-=10;
			if(p>=0)
			x[p]++;
		}
		if(is_o())
		{
			x[0]='1';
			for(i=1;i<=len;i++)
				x[i]='0';
			x[i]='\0';
		}
		s=digitsum();
	}
	cout<<x<<endl;
 }
  return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值