ZOJ.3336 Friend Number II【有点水】 2015/10/09

Friend Number II

Time Limit: 1 Second      Memory Limit: 32768 KB

Given a positive integer x, let S(x) denotes the sum of all x's digits. Two integers x and y are friend numbers if S(x)=S(y). Here comes the problem: Given a positive integer x, of course it has a lot of friend numbers, find the smallest one which is greater than x,please.

Input

There are multiple test cases. The first line of input is an integer T (0<T<230) indicating the number of test cases. Then T test cases follow. Each case is an integer x (0<x<=101000).

Output

For each test case, output the result integer in a single line.

Sample Input

3
12
19
222

Sample Output

21
28
231

Note: No input data start with digit 0 and you should not output a number starts with 0.


Author: CAO, Peng
Source: ZOJ Monthly, May 2010

刚开始的时候想错了,以为直接加9就可以了,后来想想不对,仔细想想才明白,从后往前找,找到第一个不为0的减1,再继续往前找,找到第一个不为9的加1(如果找到最前面也没找到一个不为9的,就先输出个1),然后对此位置之后的串排序输出;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main(){
    int t,len,i;
    char s[1010];
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        //if( s[0] == '0' ) continue;
        len = strlen(s);//printf("%d\n",len);
        for( i = len-1 ; i >= 0 ; --i ){
            if( s[i] != '0' ){
                s[i]--;
                break;
            }
        }
        for( --i ; i >= 0 ; --i ){
            if( s[i] != '9' ){
                s[i]++;
                break;
            }
        }
        if( i < 0 ) printf("1");
        sort(s+i+1,s+len);
        for( i = 0 ; i < len ; ++i )
            printf("%c",s[i]);
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值