HDU 4608 I-number

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

 

 

题目挺水的,不过是高精度

 

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

int maxLen;
int a[110000];
int y[110000];

void plus(int s){
    int carry=0;
    int cnt=0;
    int k=maxLen-1;
    if(s==10){
        y[cnt++]=a[k--];
        carry=(a[k]+1)/10;
        y[cnt++]=(a[k]+1)%10;
        k--;
    }else{
        y[cnt++]=(a[k]+s)%10;
        carry=(a[k]+s)/10;
        k--;
    }
    while(carry!=0 || k>=0){
        if(k>=0){
            y[cnt++]=(a[k]+carry)%10;
            carry=(a[k]+carry)/10;
            k--;            
        }else{
            y[cnt++]=carry;
            carry=0;
        }
    }
    maxLen=cnt;
}

int main()
{
    int T;
    char x[110000];
    scanf("%d",&T);
    while( T-- ){
        int sum=0;
        int cnt=0;
        scanf("%s",x);
        for(int i=0; x[i]!='\0'; i++){
            sum+=x[i]-'0';
            a[cnt++]=x[i]-'0';
        }
        maxLen=cnt;
        int t=10-sum%10;
        if(t>=(10-a[maxLen-1])){
            plus(10-a[maxLen-1]);
            sum=0;
            cnt=0;
            for(int i=maxLen-1; i>=0; i--){
                sum+=y[i];
                a[cnt++]=y[i];
            }
            t=10-sum%10;
            if(sum%10!=0)
                plus(t);
        }else{
            plus(t);    
        }
        for(int i=maxLen-1; i>=0; i--){
            printf("%d",y[i]);
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2014-02-16 23:00  chen2013  阅读(209)  评论(0编辑  收藏  举报