Cattle-off practice match 33 C tokitsukaze and Number Game (conclusion + string processing)

Links: https://ac.nowcoder.com/acm/contest/308/C
Source: Cattle-off network

tokitsukaze and Number Game
time limit: C / C ++ 1 second, other languages 2 seconds
to space constraints: C / C ++ 262144K, other languages 524288K
64bit IO the Format:% LLD
subject description
tokitsukaze and playing games on 3ds, and now she encountered to the storm.
tokitsukaze get an integer x, and requires the use of digital x on every rearranged, the composition can be a number divisible by 8, and this number as large as possible.
Please help you clever cute tokitsukaze, if the composition is not divisible by the number 8, please output -1.
Without leading 0 ensure that the input, output can not with leading 0.
Input Description:
The first line includes a positive integer T (T <= 1000), T represents a set of data.
Next T rows, each comprising an integer x, (0≤x≤10 ^ 100).
Output Description:
Make an output of these numbers can be divided by the maximum number of 8, if the composition not divisible by 8, output a -1.
Example 1
Input
Copy
2
666
1256
outputs
replication
-1
6512

Meaning of the questions: idea: you should know that a rule: After three if a number is a multiple of 8, then this number is a multiple of 8.




So after we readings come in, if the length is less than 3, special treatment can be. .

If the length is greater than 3,
we first of all multiples of less than 1000 pretreatment 8, including 0, then enumerated as the last three digits of the number, judged by the number of characters is legitimate, then take the maximum value in all cases is the answer .

See details Code:

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include<random>
#include<ctime>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

string S(ll n) {stringstream ss; string s; ss << n; ss >> s; return s;}
ll N(string s) {stringstream ss; ll n; ss << s; ss >> n; return n;}

int num[20];
string str;
std::vector<string> v;
int need[15];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\code_stream\\out.txt","w",stdout);

    int t;
    gbtb;
    cin >> t;
    string temp;
    for (int i = 0; i < 1000; i += 8)
    {
        temp = S(i);
        if (temp.size() == 1)
        {
            temp = "00" + temp;
        } else if (temp.size() == 2)
        {
            temp = "0" + temp;
        }
        v.push_back(temp);
    }
    sort(ALL(v));

    while (t--)
    {
        cin >> str;
        MS0(num);
        int len = str.length();
        for (int i = 0; i < len; ++i)
        {
            num[str[i] - '0']++;
        }
        if (len == 1)
        {
            ll x = N(str);
            if (x % 8 == 0)
            {
                cout << x << endl;
            } else
            {
                cout << -1 << endl;
            }
        } else if (len == 2)
        {
            ll x = N(str);
            if (x % 8 == 0)
            {
                reverse(ALL(str));
                if (N(str) % 8 == 0)
                    x = max(x, N(str));
                cout << x << endl;
            } else
            {
                reverse(ALL(str));
                x = N(str);
                if (x % 8 == 0)
                {
                    cout << x << endl;
                } else
                    cout << -1 << endl;
            }
        } else
        {
            string ans = "0";
            int flag = 1;
            for (auto x : v)
            {
                repd(i, 0, 9)
                {
                    need[i] = 0;
                }
                for (int i = 0; i < 3; ++i)
                {
                    need[x[i] - '0']++;
                }
                int tiao = 0;
                repd(i, 0, 9)
                {
                    if (num[i] < need[i])
                    {
                        tiao = 1;
                        break;
                    }
                }
                if (tiao)
                {
                    continue;
                }
                repd(i, 0, 9)
                {
                    num[i] -= need[i];
                }
                flag = 0;
                std::vector<char> ans1;
                ans1.clear();
                repd(i, 0, 9)
                {
                    repd(j, 1, num[i])
                    {
                        ans1.push_back((char)(i + '0'));
                    }
                }
                sort(ALL(ans1), greater<char>());
                string tt = "";
                for (auto c : ans1)
                {
                    tt.push_back(c);
                }
                std::mt19937 generator(time(0));
                string y = x;
                for (auto c : y)
                {
                    tt.push_back(c);
                }

                ans = max(ans, tt);
                repd(i, 0, 9)
                {
                    num[i] += need[i];
                }
            }
            if (flag)
            {
                cout << -1 << endl;
            } else
            {
                cout << ans << endl;
            }
        }





    }



    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11184007.html