百度的一道笔试题目,看到博客园讨论挺热烈的,也写一下玩玩。

实现思想:举个简单的例子11233,从高位到低位开始判断是否有重复数,高位有重复数后,首先修改高位的,高位修改后变为12233,因为要求最小的不

重复数,这时实际上要求的是12000这个数的最小不重复数了。在举个例子98989899,它的变化系列可是是这样:

98989900
98990000
99000000
100000000
101000000
101010000
101010100
101010101

1、给定任意一个正整数,求比这个数大且最小的“不重复数”,“不重复数”的含义是相邻两位不相同,例如1101是重复数,而1201是不重复数。

 #include <iostream>
#include <stdio.h>
#include <stdlib.h> using namespace std; int test(int n){
char str[];
sprintf(str,"%d",n);
int len = strlen(str);
int cur = ;
int next = ;
if(len < )
return -;
for(int i = ; i < len; i++)
{
cur = i;
next = i+;
if(str[cur] == str[next]){
int result = len - (i+);
return result;
}
if(next == len)
break;
}
return -;
}
int find(int n)
{
int pos = test(n);
if(pos == -)
return n;
else{
int step = ;
for(int i = ; i < pos; i++)
step *= ;
cout << n/step*step+step <<endl;
find(n/step*step+step);
}
} int main(){ int n = ;
cout << test() << ": " << find() << endl;
cout << test() << ": " << find () << endl;
cout << test() <<": " << find() << endl;
cout << test() << ": " << find() << endl;
cout << "1099012: "<<find()<<endl;
cout << "11234: "<<find()<<endl;
cout << "98989899: "<<find()<<endl;
cout << "10989899: "<<find()<<endl;
return ; }

测试结果如下:

-1: 12345
4: 12010
1: 12
1: 201
1099012: 1201010
11234: 12010
98989899: 101010101
10989899: 12010101

05-07 15:16