I-number(hdu4608,暴力枚举)

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#problem/G

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

I-number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 565    Accepted Submission(s): 215

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(T100) 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 105.

Output

Output the I-number of x for each query.

Sample Input

1

202

Sample Output

208

Source

2013 Multi-University Training Contest 1

Recommend

liuyiding

Statistic | Submit | Discuss | Note

解析:

题意:给出一个数y。要求找到一个比y大且每位上数字之和是10的倍数的最小数

思路:由于是大数,且枚举次数不会超过20次,所以要直接用暴力。

注:暴力还是要优化的。不要把每次加的结果存到字符串上,而是将结果存到整型数组内,通过判断是否有进位来改变数组。

Accepted 906MS 736K 973 B C++

*/


#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
const int maxn=100000+10;
char ch[maxn];
int a[maxn];
int main()
{
    int i,T,len,flag,j;
    scanf("%d",&T);
    while(T--)
    {
    	scanf("%s",ch);
    	j=0;
    	len=strlen(ch);
    	memset(a,0,sizeof(a));
    	for(i=len-1;i>=0;i--)
    	 a[++j]=ch[i]-'0';//存到整型数组中,可以节省大数相加的时间
    	 int ans,t=0;
    	 for(;;)
    	 {
    	 	a[1]++;
    	 	flag=1;
    	 	while(a[flag]>=10)//使得每位上的数字都能保持一位数
    	 	{   a[flag]=a[flag]%10;
    	 	 a[++flag]++;//printf("flag=%d",flag);
    	 	}
    	 	ans=0;
    	 	if(flag>len)
    	 	 len=flag;
    	 	for(i=1;i<=len;i++)
    	 	{
    	 	 ans+=a[i];
    	 	 if(ans>=10)
    	 	 ans-=10;
    	 	}
    	 	if(ans==0)
    	 	break;
    	 	t++;
    	 }
    	 //printf("t==%d\n",t);
    	 for(i=len;i>=1;i--)
    	 printf("%d",a[i]);
    	 printf("\n");
    }
    return 0;
}
下面使用同一种方法写的,但是存在字符串里头了
6Time Limit Exceeded 46085000MS 324K708 BC++
#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
const int maxn=100000+50;
char s1[maxn];
void add()
{    int len=strlen(s1);
     int j=0;
     s1[j]++;
     while((s1[j]-'0')>=10)
     {
         s1[j]=(s1[j]-'0')%10+'0';
         s1[++j]++;
     };
     if(j>=len)
     {  s1[j]+='0';
         s1[++j]='\0';
     }
}
int work()
{  int ans=0,i;
    for(i=0;i<strlen(s1);i++)
    {ans+=s1[i]-'0';
    }
    if(ans%10==0)
    return 1;
    else
    return 0;
}
int main()
{int T;
scanf("%d",&T);
while(T--)
{   memset(s1,'\0',sizeof(s1));
    scanf("%s",&s1);
    strrev(s1);
    add();
    while(!work())
     {
         add();
     }
     strrev(s1);
    printf("%s\n",s1);
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值