牛客练习赛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 ...
随机推荐
- 实现Runable接口
步骤 定义一个实现Runable接口的类,在类中实现run()方法(线程执行事件的方法).创建一个上述类的对象:Thread t=new Thread(new MyThreadt.start());调 ...
- code first System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a database transaction but it could not be determined whether the transaction succeeded
System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a da ...
- 【机器学习速成宝典】模型篇03逻辑斯谛回归【Logistic回归】(Python版)
目录 一元线性回归.多元线性回归.Logistic回归.广义线性回归.非线性回归的关系 什么是极大似然估计 逻辑斯谛回归(Logistic回归) 多类分类Logistic回归 Python代码(skl ...
- 五大好用的开源MySQL管理工具推荐
众所周知,对于数据库管理工作者(DBA)来说,保持数据正常运行在最佳状态需要具备敏捷.专注.冷静和快速的反应能力.因为数据库几乎是所有应用程序成功运行的核心,由于DBA负责组织数据,因此寻找可靠的工具 ...
- Mybaits配置多个数据库操作sql环境
mybitas可以配置sql语句适用于不同数据库下的操作,因为不同数据库sql语句可能有差别,接下来介绍如果进行操作 1.在jdbc.properprites配置驱动 jdbc.driver=com. ...
- 【flask_sqlalchemy】模糊查询
flask_sqlalchemy的查询方法有filter()和filter_() 这2个方法的主要区别如下: 模块 语法 ><(大于和小于)查询 and_和or_查询 filter_by( ...
- 前端必须掌握的 docker 技能(3)
概述 作为一个前端,我觉得必须要学会使用 docker 干下面几件事: 部署前端应用 部署 nginx 给部署的 nginx 加上 https 使用 docker compose 进行部署 给 ngi ...
- java8 查找字符串中首次出现2次的字母
利用java8的stream函数式编程进行处理 1.实现字母分离 map将整个字符串当成一个单词流来处理 Map<String[], Long> collect14 = Stream.of ...
- 八:flask-重定向示例
现象:访问地址a,跳转到地址b,在flask中,使用redirect()来进行重定向 永久性重定向:301,多用于旧网址被废弃了,需要跳转到新网址访问 例如请求www.jingdong.com,会自动 ...
- hibernate 2 一对多、多对一 双向映射
多对一或一对多中,在多的一方维护关系效率高 一:java实体类 1.Classes.java package cn.gs.ly.school.entity; import java.util.Set; ...