牛客练习赛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 ...
随机推荐
- git 配置 https和ssh 免密码登录 常用操作命令
git 配置 https和ssh 免密码登录 一. 区分https clone 和 ssh clone 不同的克隆方式导致校验方式不同,对应的免秘方式也不一样. https通过记住账号密码免登,ssh ...
- springboot 配置访问本地图片
spring.mvc.static-path-pattern=/image/** spring.resources.static-locations=file:D://image/
- Singleton(Java)
1.定义私有静态易变的类变量2.定义getInstance静态方法 2.1.若静态变量为null,则在同步类类型的同时判断静态实例是否为null, 是null则创建新实例赋给静态变量 2.2.不为nu ...
- db2缓冲池调优
缓存池: 冲池是内存中的一块区域,db2会将用到数据放到缓冲池中提高性能.缓冲池太小,每次查询仍然要到磁盘中操作,达不到缓冲的效果.缓冲池太大,超出操作系统管理的限制,会导致数据库无法连接的错误. 缓 ...
- 二、Python基础
1.变量名 数字,字母,下划线:aaa1;aa_b1 不能以数字开头:1aa 变量名不能是python内部的关键字 2.getpass import getpass username=raw_inpu ...
- 分布式任务队列 Celery —— 应用基础
目录 目录 前文列表 前言 Celery 的周期定时任务 Celery 的同步调用 Celery 结果储存 Celery 的监控 Celery 的调试 前文列表 分布式任务队列 Celery 分布式任 ...
- apache-httpd2.2编译安装
1.下载源码包 wget http://mirrors.hust.edu.cn/apache/httpd-2.2.32.tar.gz2.解压源码包 tar -zxvf httpd-2.2.32.tar ...
- robotframework(rf)中对时间操作的datetime库常用关键字
1.对固定日期进行操作,增加或减去单位时间或者时间段 2.对两个时间段进行操作 3.对时间格式转化,获取时间戳. 4.从完整时间中取指定年月日等 5.对时间类型进行格式化 6.获取当前时间或者指定时区 ...
- kubernets部署微服务电商平台
一.准备条件 1) 确保kubernetes可以访问:reg.yunwei.edu镜像库(vim /etc/hosts) [root@cicd yml]# cat /etc/hosts 127.0.0 ...
- IntToHex
IntToHex是一种函数,功能是将一个值转换成16进制形式的字符串. IntToHex(int Value, int Digits) 来源: 在Delphi.Pascal或C++ Builder中使 ...