博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ZOJ3336(花了两天才搞定的一道算法题!)

Posted on 2010-05-29 17:55  还好  阅读(420)  评论(0编辑  收藏  举报

先说一下,如果你想提高自己的编程能力的话,建议你到浙大的ACM网站或者是北大的ACM网站,这两个网站也是国内最老牌的了,感兴趣不妨去试试,里面有很多编程题,各式各样的都有!网址我就不贴了,不知道的google一下。

题目描述:

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<=10^1000).

Output

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

Sample Input


3
12
19
222

 

Sample Output


21
28
231

 

折腾了两天才搞定,真是不容易啊(是不是我太菜了?),题目意思我就不说了,下面主要说一下我的解题思路。我们以数字1985(我的出生年份)为例子,满足条件的最小的数字是多少呢?这个我想一般人都应该想到,那就是将个位数减1,十位数加1,结果也就是1994了。这个是正常情况下的做法,有人可能会立马想到下面情况,就是如果十位数是9了?或者个位数是0了?其实为主要也就是在这边了。我们先考虑个位为零的情况,这次以1780为例,满足条件的最小数是1807,也就是将7加1,8变为零,同时将最后一个0变成7(8-1)。然后就是有9的情况了,这个相对来说是最复杂的一种情况了。我们以19995为例,满足条件的最小数是24999,变法就是从十位数开始有多少个连续的9,统统将其变为0,然后再对应的将数字的末尾变成9,在将连续的9的最后一个9(从右到左)变成原来数的个位数值减1。哎实在是不怎么好表达,说的自己都觉得有点乱,大家就将就将就了。总的来说分为以下几种情况:

1。十位数为9。

2。十位数不是9,这个又要分为末尾是否有0的两种情况。

大家慢慢琢磨去吧!第一次写这样的文章,多多包涵!

 

下面是我的代码:

#include <iostream>

#include <string>

using namespace std;

int a[1003];

string s;

void Print()

{

                if(a[0]!=0)cout<<a[0];

                for(int i=1;i<s.length()+1;i++)

                {

                                cout<<a[i];

                }

                cout<<endl;

}

void GetFriendNumber()

{

                int len=s.length();

                if(len==1)

                {

                                cout<<a[0]+1<<a[1]-1<<endl;

                                return;

                }

                if(a[len]==0)

                {

                                int k=len;

                                while(a[k]==0)k--;

                                if(a[k-1]!=9)

                                {

                                                a[len]=a[k]-1;

                                                a[k-1]++;

                                                a[k]=0;

                                                Print();

                                                return;

                                }

                                else

                                {

                                                int m=len;

                                                int n=k-1;

                                                int temp=a[k]-1;

                                                while(a[n]==9)

                                                {                             

                                                                a[n]=0;

                                                                a[m]=9;

                                                                n--;

                                                                m--;

                                                }

                                                a[n]++;

                                                a[m]=temp;

                                                Print();

                                                return;

                                }

                }

                else

                {

                                int k=len;

                                if(a[k-1]!=9)

                                {

                                                a[k-1]++;

                                                a[k]--;

                                 Print();

                                                return;

                                }

                                else

                                {

                                                int m=len;

                                                int n=k-1;

                                                int temp=a[k]-1;

                                                while(a[n]==9)

                                                {                             

                                                                a[n]=0;

                                                                a[m]=9;

                                                                n--;

                                                                m--;

                                                }

                                                a[n]++;

                                                a[m]=temp;

                                                Print();

                                                return;

                                }

                }

}

 

int main(void)

{

                int num;

                cin>>num;

                for(int k=0;k<num;k++)

                {

                                cin>>s;

                                a[0]=0;

                                for(int i=0;i<s.length();i++)

                                {

                                                a[i+1]=s[i]-'0';                   

                                }

                                GetFriendNumber();

                }

                return 0;

}


代码是用C++写的,虽然我现在做开发是用C#,不过没办法,网站只接受C++,做之前还特地复习了一下C++,感觉C++在智能方面确实不如C#。代码写的有点烂,有些重复的代码,AC了过后,我就不想改了。多多包涵!别喷我,谢谢!