首页 新闻 会员 周边

一个ACM题目,向大家请教个算法

0
悬赏园豆:5 [已解决问题] 解决于 2013-08-17 15:59

题目描述:
Description
Recently Ginger found that there are many numbers, digits of whom can be rearranged to a consecutive sequence. For instance, 254310 is a consecutive-digit number, since the digits of it are 2, 5, 4, 3, 1 and 0, which can be rearranged to “0, 1, 2, 3, 4, 5”. 
A sequence is consecutive, if it forms a one-ascending digit sequence either consecutively or circularly from 9 to 0. For example, both “1, 2, 3, 4” and “8, 9, 0, 1” are consecutive sequences. 
In a consecutive-digit number, each digit (0~9) can only appear once. 

Input
There are several input cases, a single positive integer in a single line for each input case.
Input end with 0.
Output
YES or NO in a single line, if the given number is a consecutive-digit number or not.
Sample Input
1423
7980
21350
2100
0
Sample Output
YES
YES
NO
NO

大意是说输入一个整数,每个数位重新排列后是一个连续的序列,如输入254130,重排后为012345。9~0是循环的,如8901。
输入的每个数由0~9构成,每个数字只能出现一次。

我的算法如下:
·是首先获取每个数位,保存在数组 b[]里,然后对b[]排序,判断是否有重复的数字出现;
·否则,再判断数组 b[] 的第一位和最后一位是否分别等于0和9,如果是,在数组c[10]={0,1,2,3,4,5,6,7,8,9}里扣掉数组b[]里的元素,然后如果数组c[]里剩下的元素是连续的,则 b[] 是连续的;
·否则,判断b[i]-b[i-1]是否等于1,如果是,则b[]是连续的。

代码如下:

C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdlib> //qsort
using namespace std;
 
int comp(const void *a, const void *b)
{
    return (*(int*)a - *(int*)b);
}
 
int main()
{
    int n,i,j,len;
    int a[10];
    for(i=0; i<10; ++i)
        a[i]=0;
    while(cin>>n,n)//1423
    {
        int c[10]={0,1,2,3,4,5,6,7,8,9};
        int res=1;
        for(i=0; n!=0 && i<10; ++i)
        {
            a[i] = n % 10;
            n /= 10;
        }
        len = i;//4
        int b[len];
        for(i=0; i<len; ++i)
            b[i]=a[i];
        qsort(b, len, sizeof(int), comp);
                 
        for(i=1; i<len; ++i)//i=1,2,3
        {
            if(b[i]==b[i-1])
            {
                res=0;
                break;
            }   
        }
         
        if(res!=0)
        {
            if(b[0]==0 && b[len-1]==9)
            {
                for(i=0; i<len; ++i)
                {
                    for(j=i; j<10; ++j)
                    {
                        if(c[j]==b[i]) {c[j]=-1; break;}
                    }
                }
                qsort(c, 10, sizeof(int), comp);
                /*
                for(i=0; i<10; ++i)
                    cout<<c[i]<<" ";
                cout<<endl;
                */
                for(i=len+1; i<10; ++i)//i=5
                {
                    if((c[i]-c[i-1])!=1) {res=0; break;}
                }
            }
            else
            {
                for(i=1; i<len; ++i)//i=1,2,3
                if((b[i]-b[i-1])!=1) {res=0; break;}
            }
        }
         
        if(res==1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
         
    }
    return 0;
}



我自己测了几组数据没有问题,但是提交上去就错了。不知道错在哪儿……

duanguyuan的主页 duanguyuan | 初学一级 | 园豆:136
提问于:2013-08-17 11:19
< >
分享
最佳答案
1

我没有把代码看完哈~现在说下自己的意见,仅供参考。

1. 首先 int 型是32位的,除去一位的符号位是31位的,最大是2^31-1吧,也就是2147483647,如果给出的数据是9876543210就错误了吧。

2. 即使没有这么2的测试数据的话,也有可能是这样的测试用例,如 0213,也就是以0打头的,按照楼主的cin>>n方式,n = 213,没有0

3. 代码没看完,大体意思懂了。暂时有这两个想法,可以试下string类型的

收获园豆:5
viczzx | 菜鸟二级 |园豆:211 | 2013-08-17 13:51

谢谢你!真的是数据类型设为int32的错误,我改为string就ok啦!!

非常感谢!

duanguyuan | 园豆:136 (初学一级) | 2013-08-17 15:58

@duanguyuan: 加油!我也要多刷ACM才行啊!

viczzx | 园豆:211 (菜鸟二级) | 2013-08-17 17:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册