牛客练习赛33 C tokitsukaze and Number Game (结论+字符串处理)

链接:https://ac.nowcoder.com/acm/contest/308/C
来源:牛客网

tokitsukaze and Number Game
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
tokitsukaze又在玩3ds上的小游戏了,现在她遇到了难关。
tokitsukaze得到了一个整数x,并被要求使用x的每一位上的数字重新排列,组成一个能被8整除的数,并且这个数尽可能大。
聪明的你们请帮帮可爱的tokitsukaze,如果无法组成被8整除的数,请输出-1。
保证输入不含前导0,输出也不能含前导0。
输入描述:
第一行包括一个正整数T(T<=1000),表示T组数据。
接下来T行,每一行包括一个整数x,(0≤x≤10^100)。
输出描述:
请输出用这些数字组成出能被8整除的最大的数,如果无法组成出能被8整除的数,请输出-1。
示例1
输入
复制
2
666
1256
输出
复制
-1
6512

题意:

思路:

首先应该知道一个规律:
一个数的后三位如果是8的倍数,那么这个数就是8的倍数。

那么我们把数读进来之后,如果长度小于3,特殊处理即可。。

如果长度大于3,
我们先预处理小于1000的所有8的倍数,包括0,然后枚举这些数作为数字的后三位,通过字符数量来判断是否合法,然后从所有情况中取最大值就是答案。

细节见代码:

#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
",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), '', 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_textcode_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 == '
');
	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';
		}
	}
}




本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/11184007.html