HDU 4608 I-number(模拟)

28 篇文章 0 订阅
3 篇文章 0 订阅

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
 

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;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值