CSU1327+贪心+模拟

题意简单,中文题目

方法:对于一个数 从左往右找相同的数 ,有就改变靠右的,同时把该数的右边全置0

注意!!!!n<0!!!

/*

*/
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b)) 
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
const double eps = 1e-8;
const int maxm = 1005;
const int maxn = 105;

int num[ maxn ];
int64 n ;

void Solve( int cnt ){
    int temp = 1;
    int pos = -1;
    for( int i=0;i<cnt;i++,temp *= 10 ){
        if( i+1<cnt && num[i]==0 && num[i+1]==0 ) continue;
        if( num[i]==num[i+1] ){
            n += temp;
            pos = i;
            //printf("pos = %d\n",pos);
            //printf("i = %d\n",i);
            if( pos>0 ){ 
                int tt = 1;
                while( 1 ){
                    n /= 10;
                    pos--;
                    tt *= 10;
                    if( pos==0 ) break;
                }
                n *= tt;
            }
            
            if( num[i]==9 ){
                int64 nn = n;
                int cc = 0;
                while( nn ){
                    num[ cc++ ] = nn%10;
                    nn /= 10;
                }
            }
        }
    }
    //printf("pos = %d\n",pos);
}

bool Judge( int n ){
    int cnt = 0;
    int64 nn = n;
    while( nn ){
        num[ cnt++ ] = nn%10;
        nn /= 10;
    }
    bool flag = true;
    for( int i=0;i<cnt;i++ ){
        if( i+1<cnt && num[i]==0 && num[i+1]==0 ) continue;
        if( num[i]!=num[i+1] ) {}
        else {
            flag = false;
            break;
        }
    }
    if( flag==true )
        return true;
    Solve( cnt );
    return false;
}

int main(){
    int T;
    scanf("%d",&T);
    while( T-- ){
        //scanf("%d",&n);
        cin>>n;
        if( n<0 ){
            cout<<"0"<<endl;
            continue;
        }
        n ++;
        memset( num,0,sizeof( num ) );
        while( 1 ){
            if( Judge(n)==true ) break;
        }
        //printf("%d\n",n);
        cout<<n<<endl;
    }
    return 0;
}


你可能感兴趣的:(CSU1327+贪心+模拟)