USACO Runaround Numbers 模拟

根据题意的 Runaround 规则去找比当前数大的最近的一个 Runaround数字

模拟题~

Source code:

/*
ID: wushuai2
PROG: runround
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 660000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;
const int MAXSIZE = 101000000;

ll num;
ll a[30];

void toStr(ll num){
    int i, j, k;
    memset(a, 0, sizeof(a));
    while(num){
        a[++a[0]] = num % 10LL;
        num /= 10LL;
    }
    for(i = 1; i <= a[0] / 2; ++i){
        swap(a[i], a[a[0] - i + 1]);
    }
}

void toNum(){
    int i, j, k;
    num = 0;
    ll nl = 1LL;
    for(i = a[0]; i >= 1; --i){
        num += nl * a[i];
        nl *= 10LL;
    }
}

bool solve(){
    int i, j, k, pos;
    ll ans[10];
    ll cur = a[1];
    ll b[30], bigg = 0;
    bool vis[30];
    memset(vis, 0, sizeof(vis));
    memset(ans, 0, sizeof(ans));
    for(i = 1; i <= a[0]; ++i){
        checkmax(bigg, a[i]);
        if(a[i] == 0)   return false;
        if(ans[a[i]])   return false;
        ans[a[i]] = true;
    }
    for(;;){
        memset(b, 0, sizeof(b));
        for(pos = 1; pos <= a[0]; ++pos){
            if(cur == a[pos])   break;
        }
        for(i = pos; i <= cur + pos - 1; ++i){
            b[++b[0]] = a[i % a[0] + 1];
        }
        /*
        for(i = 1; i <= b[0]; ++i){
            cout << b[i];
        }
        cout << endl;
        */
        if(vis[b[b[0]]]) return false;
        else vis[b[b[0]]] = true;
        for(i = 1; i <= bigg; ++i){
            if(vis[i] != ans[i])    break;
        }
        if(i == bigg + 1)   return true;
        cur = b[b[0]];
    }
}

int main() {
    ofstream fout ("runround.out");
    ifstream fin ("runround.in");
    int i, j, k, t, n, s, c, w, q;
    fin >> num;
    ++num;
    toStr(num);
    /*
    for(i = 1; i <= a[0]; ++i){
        cout << a[i];
    }
    cout << endl;
    */
    while(!solve()){
        //cout << num << endl;
        toNum();
        ++num;
        toStr(num);
    }
    fout << num << endl;

    fin.close();
    fout.close();
    return 0;
}
原文地址:https://www.cnblogs.com/wushuaiyi/p/4295437.html