# 求解一个ACM题，不知道自己错在哪

duanguyuan 2013-08-17 11:16:17

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

·是首先获取每个数位，保存在数组 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[]是连续的。

``````
#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;
}
``````

...全文
186 3 打赏 收藏 转发到动态 举报

3 条回复

FancyMouse 2013-08-17
• 打赏
• 举报

duanguyuan 2013-08-17
• 打赏
• 举报

``````
#include <iostream>
#include <string>
#include <cstdlib> //qsort
#define MAXN 10000
using namespace std;

int comp(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}

int main()
{
string s;
int i,j,len;
int a[MAXN];
for(i=0; i<10; ++i)
a[i]=0;
while(cin>>s)//1423
{
int res=1;
int c[10]={0,1,2,3,4,5,6,7,8,9};
if(s[0] == '0') break;
//cout<<"s.length():"<<s.length()<<endl;
for(i=0; i<s.length(); ++i)
a[i]=s[i]-'0';

qsort(a, s.length(), sizeof(int), comp);

/*
for(i=0; i<s.length(); ++i)
cout<<a[i]<<" ";
cout<<endl;
*/

int b[10];
b[0]=a[0];
for(i=1,j=1; i<s.length(); ++i)
{
if(a[i]!=a[i-1])
{
b[j]=a[i];
j++;
}
}
int len=j;
//cout<<"len:"<<len<<endl;

for(i=1; i<s.length(); ++i)//i=1,2,3
{
if(a[i]==a[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;
}
``````

• 打赏
• 举报

``````#include <iostream>
#include <queue>
using namespace std;

int func(unsigned long long x);

int main()
{
queue<int> result;
const char* maps[2] = {"NO", "YES"};

unsigned long long input;
cin >> input;
while (cin && input != 0)
{
result.push(func(input));
cin >> input;
}
cout << endl;
while (!result.empty())
{
cout << maps[result.front()] << endl;
result.pop();
}
return 0;
}

int func(unsigned long long x)
{
int count[20] = {0}; //用于存放每位数字的统计，20是为了构造一个循环，便于判断
int mod;
int degree = 0;  //位数
do
{
mod = x % 10;
x /= 10;
++count[mod];
++count[mod+10];
++degree;
}while (x > 0);

count[0] = 0; //设置1个标志

//从数组中间的0开始往左扫描第一个计数为0的位置
int pos = 10;
while (count[pos] > 0) pos--;

//往右扫描第一个计数>0的位置
while (count[pos] == 0) pos++;

for (int i = 0; i < degree; i++)
{
if (count[pos+i] != 1)
return 0;
}
return 1;
}``````

64,720

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下