不重复数问题解答~~

DuGuloveUU 2010-06-20 02:35:13
如果一个数字十进制表达时,不存在连续两位数字相等,则称之为“不重复数”。例如,105,1234和12121都是“不重复数”,而11,100和1225不算。给定一个long类型数字A,返回大于A的最小“不重复数”。
Examples:
0)
54
returns: 56
大于54的最小数字是55,但55不是“不重复数”。下一个数字是56,它满足条件。
1)
10
returns: 12
2)
9
returns: 10
3)
98
returns: 101
99和100都不是“不重复数”, 101是。
4)
21099
returns: 21201
...全文
302 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
庄鱼 2010-06-22
  • 打赏
  • 举报
回复
int MinNumber(int N)
{
int n=0,s=N,buf[10]={0},ret=0;
do{
s /=10;
++n;}while(s);
--n;
for(int i=0,m=pow10(n),k=N;i<n;++i,m/=10)
buf[i]=k /m, k %= m;
buf[n] = N % 10;
for(int i=0;i<=n;){
if(buf[i]==buf[i+1]){
buf[i+1]++;
while(buf[i+1]>9){
buf[i]++,buf[i+1]=0;--i;
if(0>i){
for(int j=0;j<=n+2;++j,ret*=10)
ret += j%2;
return ret/10;
}
}
if(buf[i]==buf[i+1]){
continue;}
++i;
for(int j=0;j<=i;++j,ret*=10)
ret += buf[j];
for(int j=0;j<n-i;++j,ret*=10)
ret += j%2;
return ret/10;}
++i;}
return N;
}
njhsliliangx 2010-06-21
  • 打赏
  • 举报
回复
简单的代码,工整的代码会让项目上很多的BUG
fanster28_ 2010-06-21
  • 打赏
  • 举报
回复
一个一个试必然TLE,代码美了,算法很丑陋。。。
[Quote=引用 14 楼 we_sky2008 的回复:]

C/C++ code

unsigned int get_the_number(unsigned int ui)
{
unsigned int ret = ui;
here:
while(ui && ((ui % 10) != ((ui /= 10) % 10)));
if (ui)
{
ui = ++ret;//此处可以再优化,……
[/Quote]
赵4老师 2010-06-21
  • 打赏
  • 举报
回复
《编译原理》词法分析--有限状态自动机
we_sky2008 2010-06-21
  • 打赏
  • 举报
回复

int get_the_number(unsigned int ui)
{
int array1[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};
int array2[] = {0, 0, 1, 10, 101, 1010, 10101, 101010, 1010101, 10101010, 101010101};

while(1)
{
unsigned int ret = ui;
int cnt = 0;

while(ui && ((ui % 10) != ((ui / 10) % 10)))
{
if (ui)
{
cnt++;
ui /= 10;
}
}
if (ui)
{
ui += 1;
ui *= array1[cnt];
ui += array2[cnt];
}
else
return ret;
}
}
lonegunman 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 gkathere 的回复:]

一个个试是stupid算法啦!
从高位向低位查,找到重复的第一个,对其进行处理,后面基本就是加上01010101了。
如:
5678911414;==>10位
得:
(5678911+1)*1000 + 010;
[/Quote]
十七楼正解!!
用堆栈比较好。
DuGuloveUU 2010-06-20
  • 打赏
  • 举报
回复
哈哈[Quote=引用 19 楼 yxy10072510329 的回复:]
你的代码写得真不错,但是我一运行就overflow。。

Eleven 2010-06-20
  • 打赏
  • 举报
回复
囧。。。
yxy10072510329 2010-06-20
  • 打赏
  • 举报
回复
你的代码写得真不错,但是我一运行就overflow。。
[Quote=引用 11 楼 gkathere 的回复:]
C/C++ code
int iT = 0xfffffff; //查询数
bool Check(int x) {return x ? x % 10 == (x /= 10) % 10 ? true : Check(x) : false;}
int main()
{
iT++;
if(!Check(iT))
printf("%d",iT);
……
[/Quote]
DuGuloveUU 2010-06-20
  • 打赏
  • 举报
回复
但是你这个没有交互的吧
我想随便输入一个数字然后得到他的最小不重复数
对吧[Quote=引用 17 楼 gkathere 的回复:]
一个个试是stupid算法啦!
从高位向低位查,找到重复的第一个,对其进行处理,后面基本就是加上01010101了。
如:
5678911414;==>10位
得:
(5678911+1)*1000 + 010;
[/Quote]
GKatHere 2010-06-20
  • 打赏
  • 举报
回复
一个个试是stupid算法啦!
从高位向低位查,找到重复的第一个,对其进行处理,后面基本就是加上01010101了。
如:
5678911414;==>10位
得:
(5678911+1)*1000 + 010;
DuGuloveUU 2010-06-20
  • 打赏
  • 举报
回复
这个VS08里面为啥没有结果?
这个代码看着虽然简洁,不过~~~~
[Quote=引用 11 楼 gkathere 的回复:]
C/C++ code
int iT = 0xfffffff; //查询数
bool Check(int x) {return x ? x % 10 == (x /= 10) % 10 ? true : Check(x) : false;}
int main()
{
iT++;
if(!Check(iT))
printf("%d",iT);
……
[/Quote]
DuGuloveUU 2010-06-20
  • 打赏
  • 举报
回复
赞~~~ 这个简洁
[Quote=引用 12 楼 kuyucman 的回复:]
C/C++ code
#include <iostream>
#include <string>

int main()
{
long base;
char *buf=(char *)malloc(sizeof(char)*8);
std::cin >> base;
for(int i=base+1; ;i++)
{
c……
[/Quote]
we_sky2008 2010-06-20
  • 打赏
  • 举报
回复

unsigned int get_the_number(unsigned int ui)
{
unsigned int ret = ui;
here:
while(ui && ((ui % 10) != ((ui /= 10) % 10)));
if (ui)
{
ui = ++ret;//此处可以再优化,不必每次只增加1
goto here;
}
else
return ret;
}
apoorcowboy 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gkathere 的回复:]

C/C++ code
int iT = 0xfffffff; //查询数
bool Check(int x) {return x ? x % 10 == (x /= 10) % 10 ? true : Check(x) : false;}
int main()
{
iT++;
if(!Check(iT))
printf("%d",iT);
else……
[/Quote]
确实够简洁
东莞某某某 2010-06-20
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

int main()
{
long base;
char *buf=(char *)malloc(sizeof(char)*8);
std::cin >> base;
for(int i=base+1; ;i++)
{
char *p = buf ;
itoa(i,buf,10);

while( *p!=*(p+1) && *(p+1) ) p++;

if( !*++p ) break;
}
std::cout << i << std::endl;
return 0;
}


写个丑的
GKatHere 2010-06-20
  • 打赏
  • 举报
回复
int iT = 0xfffffff;	//查询数
bool Check(int x) {return x ? x % 10 == (x /= 10) % 10 ? true : Check(x) : false;}
int main()
{
iT++;
if(!Check(iT))
printf("%d",iT);
else
main();
}

代码以简洁为美:>
vc6. PASS
DuGuloveUU 2010-06-20
  • 打赏
  • 举报
回复
简化之后的
#include<iostream>
using namespace std;
void Pudan(int num); //求解不重复数函数
void main()
{

int num;
cout<<"请输入任意一个32位数字:";
cin>>num;
cout<<"\n则这个数字的最小不重复数为:";
Pudan(num);
system("pause");
}
void Pudan(int num)
{
bool isDouble=false;
char isD[16];
while(!isDouble)
{
num++;
//isD=InttoChar(num);
itoa(num,isD,10);
for(int i=0;i<strlen(isD)-1; ){
if(isD[i+1]!=isD[i] && isD[i+1]!=isD[i+2]) {
isDouble=true;
i++; }
else{
num++;
itoa(num,isD,10);
}
}

}
cout<<isD;
}
DuGuloveUU 2010-06-20
  • 打赏
  • 举报
回复
这个好像没Bug
赞~~~
[Quote=引用 8 楼 fanster28_ 的回复:]
这个看看有bug没

C/C++ code
#include <iostream>

const int ten[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
const int ais[]={0,0,1,10,101,1010,10101,101010,1010101,10101010,10……
[/Quote]
fanster28_ 2010-06-20
  • 打赏
  • 举报
回复
这个看看有bug没
#include <iostream>

const int ten[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
const int ais[]={0,0,1,10,101,1010,10101,101010,1010101,10101010,101010101};

long helper(long n) {
for (long k=0;n/ten[k];k++) if (n/ten[k]%10==n/ten[k+1]%10)
return (n+ten[k])/ten[k]*ten[k]+ais[k];
return n;
}

long unrep(long n) {
long ans;
for (;(ans=helper(n))!=n;)
n=ans;
return ans;
}

int main() {
long n;
while (scanf("%d",&n)!=EOF) {
printf("%d\n",unrep(n+1));
}
return 0;
}
加载更多回复(7)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧