hdu 4068 I-number【大数】

题目:



I-number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1006    Accepted Submission(s): 398


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 105.
 

Output
Output the I-number of x for each query.
 

Sample Input
1 202
 

Sample Output
208
 

Source
 

Recommend
liuyiding
 


题意:


第一个数 T 代表测试数据组数

每组给你一个大数 N (N的长度 <= 100000)

求最小的 > N 且满足每一位相加的总和能够整除 10 的数


算法:


大数相加,只是+1比较简单,随便模拟一下就好了

思路:


不断的 + 1 直到满足情况
官方题解中也说的是最多+20个 1 就可以求出

昨天白天比赛的时候是 浩神 AC 的,KB 神也和我说了下怎么做, 白天看了下 浩神的代码,自己写,还是要 WA
昨晚比赛时按照浩神的思路纠结出了 AC的代码
刚刚问了下 KB 神,原来是掉了个初始化,改了也 AC 了


code:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

const int maxn = 200000;
int a[maxn];
char str[maxn];
int len;

void add()
{
    int c = 1;
    for(int i = 0;; i++)
    {
        int tmp = a[i]+c;
        a[i] = tmp%10;
        c = tmp/10;
        if(c == 0) break; //加到没有进位
    }
    if(a[len] != 0) len++; //加到头有进位
}

bool judge()
{
    int sum = 0;
    for(int i = 0; i < len; i++)
        sum += a[i];
    return sum%10;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", str);
        len = strlen(str);

        memset(a,0,sizeof(a)); //不能少否则会WA,前面的组a[len]可能会有价
        int j = len;
        for(int i = 0; i < len; i++) a[i] = str[--j]-'0';
        add();
        while(judge()) add();

        for(int i = len-1; i >= 0; i--) printf("%d",a[i]);
        printf("
");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

const int maxn = 200000;
int a[maxn];
char str[maxn];
int len;

void add()
{
    int c = 1;
    for(int i = 0; i < len; i++)
    {
        int tmp = a[i]+c;
        a[i] = tmp%10;
        c = tmp/10;
        if(c == 0) return;
    }
    a[len] = c; len++;
    return;
}

int judge()
{
    int sum = 0;
    for(int i = 0; i < len; i++)
    {
        sum += a[i];
    }
    return sum%10;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", str);
        len = strlen(str);

        int j = len;
        for(int i = 0; i < len; i++) a[i] = str[--j]-'0';
        add();
        while(judge()) add();

        for(int i = len-1; i >= 0; i--) printf("%d", a[i]);
        printf("
");
    }
}


原文地址:https://www.cnblogs.com/freezhan/p/3219042.html