牛客练习赛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\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';
}
}
}
牛客练习赛33 C tokitsukaze and Number Game (结论+字符串处理)的更多相关文章
- 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)
链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...
- 牛客练习赛33 E. tokitsukaze and Similar String (字符串哈希)
题目链接:https://ac.nowcoder.com/acm/contest/308/E 题意:中文题 见链接 题解:哈希预处理(三哈希模板) #include <bits/stdc++.h ...
- 牛客练习赛33 D tokitsukaze and Inverse Number (树状数组求逆序对,结论)
链接:https://ac.nowcoder.com/acm/contest/308/D 来源:牛客网 tokitsukaze and Inverse Number 时间限制:C/C++ 1秒,其他语 ...
- 牛客练习赛33 B tokitsukaze and RPG (类埃筛)
链接:https://ac.nowcoder.com/acm/contest/308/B 来源:牛客网 tokitsukaze and RPG 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/ ...
- 牛客练习赛50 D tokitsukaze and Event (最短路,思维)
牛客练习赛50 D tokitsukaze and Event 链接:https://ac.nowcoder.com/acm/contest/1080/D来源:牛客网 时间限制:C/C++ 1秒,其他 ...
- 【牛客练习赛53】A-超越学姐爱字符串
// 题目地址:https://ac.nowcoder.com/acm/contest/1114/A /* 找规律(碰运气) n:1 = 2 n:2 = 3 n:3 = 5 n:4 = 8 ... d ...
- 牛客练习赛51 C 勾股定理 (数学,结论)
链接:https://ac.nowcoder.com/acm/contest/1083/C来源:牛客网 题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角 ...
- 牛客练习赛63 牛牛的斐波那契字符串 矩阵乘法 KMP
LINK:牛牛的斐波那契字符串 虽然sb的事实没有改变 但是 也不会改变. 赛时 看了E和F题 都不咋会写 所以弃疗了. 中午又看了一遍F 发现很水 差分了一下就过了. 这是下午和古队长讨论+看题解的 ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
随机推荐
- 腾讯这套SpringMVC面试题你懂多少(面试题和答案)
1.什么是 SpringMvc? 答:SpringMvc 是 spring 的一个模块,基于 MVC 的一个框架,无需中间整合层来整 2.Spring MVC 的优点: 答:1)它是基于组件技术的.全 ...
- windows下mingw编译faac
进入目录先运行 ./bootstrap 接着就可以 ./configure --without-mp4v2 然后 make make install
- centOS7 flask项目布署
先用1张图表示centOS布署flask的关键点,以及可能遇到的问题,及解决办法. 图片看不清,可以[下载]下来看,清晰度能够看清字 [目标] 局域网内,通过url可以访问flask编写的api 更新 ...
- ApplicationSettingsBase运用
先建一个类继承于ApplicationSettingsBase using System; using System.ComponentModel; namespace Concert.Configu ...
- 阶段3 1.Mybatis_11.Mybatis的缓存_1 今日课程安排
- 现有1~100 共一百个自然数,已随机放入一个有98个元素的数组a[98].要求写出一个尽量简单的方案找出没有被放入数组的那2个数,并在屏幕上打印这2个数
void test7() { try { ]; ]; ]; ; ; int i; ; i < num.Length; i++) { num[i] = i + ; num1[i] = i + ;/ ...
- C 语言中的预处理
C 语言中以 # 开头的就是预处理指令,例如 #include . 预处理指令的用途 所有的预处理指令都会在 GCC 编译过程的预处理步骤解析执行,替换为对应的内容.在下一步编译过程中,看不到任何预处 ...
- Java基础之 多线程
一.创建多线程程序的第一种方式: 继承(extends) Thread类 Thread类的子类: MyThread //1.创建一个Thread类的子类 public class MyThread e ...
- Django REST framework JWT学习
1.JWT学习 在用户注册或登录后,我们想记录用户的登录状态,或者为用户创建身份认证的凭证.我们不再使用Session认证机制,而使用Json Web Token认证机制. Json web toke ...
- Linux cd命令(4)
可以说在Linux上的一切操作都是从 cd 命令开始的.cd 是change directory的简写,其作用就是更改当前工作目录. 使用cd 对于这个命令的使用,不用多说了.需要注意的两点就是: c ...