HDU 4608 I-number(暴力模拟)

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.
 

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的l数y,y需要满足
1.大于x
2.y中的所有数字(0-9)之和sum要是10的倍数
3.所有满足条件1.2的数中最小的数
分析:
题目给的x的长度上限10^5,注意是 长度不是大小。。。
所以想到用字符串,由条件1,3,想到每次在x的基础上加1(字符串模拟),然后判断是否满足条件2,找到第一个满足的就是最小的答案
如果是单独每次加一操作之后用循环判断是否为10的倍数肯定会超时,毕竟字符串的长度能达到10^5
其实每次进行加一操作的时候就可以判断出y中所有数字是如何改变的,然后每次在原来的基础上改变sum的值
字符串模拟加法的时候,正常+1会导致sum+=1,而进位会导致9变成0,sum-=9。具体操作见代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<cctype>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
struct Node
{
    string s;
    int sun;
};
Node Addone(string s)
{
    int sun = 0;
    int len = s.size()-1;
    s[len]++;sun++;
    while (len>0&&s[len]>'9')
    {
        s[len] = '0';
        s[len-1]++;
        len--;
        sun -= 9;//进位9变0,数字和减9
    }
    if (s[0]>'9')//99会变成100
    {
        s[0] -= 10;
        s = "1"+s;
        sun -= 9;
    }
    Node ans;//为了同时返回加法的结果和sum的值而用的结构体,其实sum开全局也可以。。
    ans.s = s;ans.sun = sun;
    return ans;
}
int  fool(string s)//求所有数字的和
{
    int sun = 0;
    for (int i = 0;i < s.size();++i)
    {
        sun += (s[i]-48);
    }
    return sun;
}
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        string s;
        cin>>s;
        int sun = fool(s);//初始的sum先求出来
        while (1)
        {
            Node tmp = Addone(s);
            s = tmp.s;
            sun += tmp.sun;//每次更新sum的值
            if (sun%10==0)//第一个找到的满足最小
            {
                cout<<s<<endl;
                break;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值