string Test38_ADJ(string str){ //++ to ensure the smallest 自增一,确保比之前的数 大 int orilen = str.size(); if( str[str.size()-1] !='9' ){ str[str.size()-1] += 1; }else{ int pos = str.size()-1; str[pos--]='0'; bool jump = true; while(jump && pos>=0){ if( str[pos] == '9' ){ str[pos] ='0';jump = true; pos--; }else{ str[pos] +=1; jump = false; } } if(jump){ str = "1"+str; } } //cout<<str; if( str.size() > orilen ){ //length increase for(int i =1;i< str.size();i++){ if(i%2==1)str[i]='0';else str[i]='1'; //010101... } }else{ //length stay if(str.size()>1){ //ensure length > 1 int fixed = 1; while(fixed < str.size()){ //every bit if(str[fixed]!=str[fixed-1]){ fixed++; //@OK,no repeat }else{//poorly wrong if(str[fixed]=='9'){ //Circle string tmp = Test38_ADJ(str.substr(0,fixed+1)); //int tchar = tmp[tmp.size()-1]-'0'; //cout<<"h3:"<<tmp<<endl; if(tmp[tmp.size()-1]=='0'){ for( int i = 0; i< str.size()-fixed-1;i++ ){ if(i%2==0)tmp+='1';else tmp +='0'; } }else{ for( int i = 0; i< str.size()-fixed-1;i++ ){ if(i%2==0)tmp+='0';else tmp +='1'; } } str = tmp; fixed++; //cout<<"he1:"<<str<<endl; }else{ //adjust while not wrong str[fixed]+=1; fixed++; //cout<<"he2:"<<str<<endl; } } } } } return str; } void Test38(){ string str; cin>>str; str = Test38_ADJ(str); cout<<str<<endl; }
嗯...好吧,这是大于等于N的不重复。。。只好在处理之前先加一了。#include<iostream>using namespace std;string minX(const string& s){if(s.size()==1)return string(s);string result;result.push_back(s[0]);bool carry=false;for(int i=1;i<s.size();++i){if(carry)result.push_back(result.back()=='0'?'1':'0');else if(s[i]==result.back()){carry=true;if(s[i]=='9'){result.pop_back();if(result.empty())result.push_back('1');else result.back()=result.back()+1;result.push_back('0');result.push_back('1');}else result.push_back(s[i]+1);}else result.push_back(s[i]);}return result;}int main(){string s;cin>>s;cout<<minX(s)<<endl;return 0;}
#include <iostream> #include <string> using namespace std; int main() { int i = 0; char c = 0; long num = 0; cin >> num; string strNum = std::to_string(num+1); int nHightLength = strNum.length(); do { //查找相邻的重复数 i = 0; do { c = strNum[i]; } while (++i < nHightLength && c != strNum[i]); //在高位遍历 //有重复位数,截取高位加1,替换原有的高位 if (i != nHightLength) { string substr = strNum.substr(0, i+1); //截取存在重复的高位 nHightLength = substr.length(); long nHignt = stol(substr) + 1; //加1 strNum.replace(strNum.begin(), strNum.begin() + i + 1, std::to_string(nHignt)); } } while (i != nHightLength); //如果循环中有重复,则再次循环,如99+1=100出现了两个0 //尾部填充0或1,判断填充前高位的最后一个是0还是1 bool change = strNum[nHightLength - 1] - '0'; for (int i = nHightLength; i < strNum.length(); ++i, change = !change) { strNum[i] = change ? '0' : '1'; } cout << strNum << endl; system("pause"); return 0; }
1 /*************************************************************************
2 > File Name: no_overlap.cpp
3 > Author: dsui
4 */
5 #include <iostream>
6 using namespace std;
7 int main(int argc, char* argv[]){
8 int n;
9 cin>>n;
10 while(n>=10){
11 int cur = n % 10;
12 n /= 10;
13 int next = n % 10;
14 if(cur == next){
15 cout<<"overlap"<<endl;
16 return 0;
17 }
18 }
19 cout<<"non-overlap"<<endl;
20 return 0;
21 }
算法思想:当然最直接的方法是采用暴力法,从N+1开始逐步加1判断是否是不重复数,是就退出循环输出,这种方法一般是不可取的,例如N=11000000,你要一个个的加1要加到12010101,一共循环百万次,每次都要重复判断是否是不重复数,效率极其低下,因此是不可取的。这里我采用的方法是:从N+1的最高位往右开始判断与其次高位是否相等,如果发现相等的(即为重复数)则将次高位加1,注意这里可能进位,如8921—>9021,后面的直接置为010101...形式,如1121—>1201,此时便完成“不重复数”的初步构造,但此时的“不重复数”不一定是真正的不重复的数,因为可能进位后的次高位变为0或进位后变成00,如9921—>10001,此时需要再次循环判断重新构造直至满足条件即可,这种方法循环的次数比较少,可以接受。
Source Code: