【链接】h在这里写链接


【题意】


给你一个长度为n的数字(n<=1000)
然后让你任意组合这个数字。
使得这个数字能被8整除。
(不能出现前导0)

【题解】


只要后三位能被8整除就可以了。
则枚举最后3位是什么.
从000-999->只枚举8的倍数就可以了。
小于等于3位的情况,特殊判断一下就好。
然后剩下的数字,先放一个最小的非0数字在开头。
剩下的从小到大安排就可以了。
(可以把它放在字符串vector里面.最后排下序输出最小的就好了)

【错的次数】


0

【反思】


在这了写反思

【代码】

#include <bits/stdc++.h>
using namespace std; string s;
int len, cnt[10], cnt1[10];
vector <string> v;
vector <int> v1; void back() {
for (int i = 0; i <= 9; i++) cnt[i] = cnt1[i];
} void sp() {
int start = 3 - len;
if (v1[start] == 0) return;//开头不能为0 for (int i = start; i <= 2; i++) {//只有后start位才有效.
cnt[v1[i]]--;//数字递减。
} for (int i = 0; i <= 9; i++)
if (cnt[i] != 0) {
back();
return;
} string ts = "";
for (int i = start; i <= 2; i++)
ts += (char)('0' + v1[i]); v.push_back(ts);
back();
} int main() {
//freopen("F:\\rush.txt", "r", stdin);
ios::sync_with_stdio(0), cin.tie(0);
cin >> s;
len = s.size();
for (int i = 0; i <= len - 1; i++) cnt[s[i] - '0']++;
v1.resize(3);
for (int i = 0; i <= 999; i += 8) {//枚举末3位是什么。肯定是8的倍数
for (int j = 0; j <= 9; j++) cnt1[j] = cnt[j];//每一位的数字有多少个。 int temp = i;
for (int j = 2; j >= 0; j--, temp /= 10) v1[j] = temp % 10;//把这3位全都获取出来。 if (len <= 3) {
sp();
continue;
} bool ok = true; for (int j = 0; j <= 2; j++) {
cnt[v1[j]]--;
if (cnt[v1[j]] < 0) ok = false;
} if (!ok) {
back();
continue;
} string ts = "";
ok = false;
for (int j = 1; j <= 9; j++)
if (cnt[j]) {
ts += (char)('0' + j);
cnt[j]--;
ok = true;
break;
}
if (!ok) {
back(); continue;
}
for (int j = 0; j <= 9; j++)
while (cnt[j] > 0) {
cnt[j]--;
ts += (char)('0' + j);
}
for (int j = 0; j <= 2; j++)
ts += (char)('0' + v1[j]);
v.push_back(ts);
back();
}
sort(v.begin(), v.end());
if (v.empty())
cout << -1 << endl;
else
cout << v[0] << endl;
return 0;
}

【CS Round #48 (Div. 2 only)】8 Divisible的更多相关文章

  1. 【CS Round #48 (Div. 2 only)】Dominant Free Sets

    [链接]h在这里写链接 [题意] 让你在n个点组成的集合里面选取不为空的集合s. 使得这里面的点没有出现某个点a和b,ax>=bx且ay>=by; 问你s的个数. [题解] 我们把这些点按 ...

  2. 【CS Round #48 (Div. 2 only)】Water Volume

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举0在哪个位置就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> us ...

  3. 【CS Round #48 (Div. 2 only)】Game of Chance

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...

  4. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  5. 【CS Round #36 (Div. 2 only) A】Bicycle Rental

    [题目链接]:https://csacademy.com/contest/round-36/task/bicycle-rental/ [题意] 让你从n辆车中选一辆车; 每一辆车有3个属性 1.到达车 ...

  6. 【CS Round #37 (Div. 2 only) D】Reconstruct Graph

    [Link]:https://csacademy.com/contest/round-37/task/reconstruct-graph/statement/ [Description] 给你一张图; ...

  7. 【CS Round #37 (Div. 2 only) B】Group Split

    [Link]:https://csacademy.com/contest/round-37/task/group-split/ [Description] 让你把一个数分成两个数a.b的和; (a,b ...

  8. 【CS Round #37 (Div. 2 only) A】Boring Number

    [Link]:https://csacademy.com/contest/round-37/task/boring-number/ [Description] 让你找离平均数最近的一个数的下标; [S ...

  9. 【CS Round #39 (Div. 2 only) D】Seven-segment Display

    [Link]:https://csacademy.com/contest/round-39/task/seven-segment-display/ [Description] 0..9各自有一个数字, ...

随机推荐

  1. Java 实现策略(Strategy)模式

    策略模式:行为型模式 将同一行为,不同的处理算法分别封装起来.让它们之间能够互相替换 1. 定义一个超类型接口,及 行为方法 2. 定义不同的实现类,实现该行为的 不同的算法 /** * 策略模式:针 ...

  2. Ubuntu 12.04使用演示

    今年年初,发布了Ubuntu 12.04(代号Precise Pangolin),但正式版预计将于2012年的4月底发布,作者对最新版本的ubuntu做了试用,先将操作视频与大家分享.更多内容请关注本 ...

  3. npm更新方法

    今天npm的版本更新发现小于3.0 尝试了npm install npm -g 安装么有成功换成了 cnpm install npm -g安装之后就可以

  4. Nim游戏算法实现

  5. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) D】Bash and a Tough Math Puzzle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于1操作 令len = r-l+1 等价于查找l..r这个范围内x的倍数的个数是否大于等于len-1 也即l..r这个范围内不是x ...

  6. Nio学习3——基础模型:多路复用模型

    Reactor模式和NIO 本文可看成是对Doug Lea Scalable IO in Java一文的翻译. 当前分布式计算 Web Services盛行天下,这些网络服务的底层都离不开对socke ...

  7. Kali Linux下安装VMware Tools

    引言 Kali Linux是基于Debian的Linux发行版, 设计用于数字取证和渗透測试.安装Kali Linux非常easy,可是安装VMware Tools的过程就有点麻烦了,由于在安装中途会 ...

  8. theme-不同主题资源更改

    1.找到了影响桌面小部件的布局文件packages/apps/Mms$ vim res/layout/widget.xml修改里面的背景颜色属性,可以实现预期效果,至于里面的 <LinearLa ...

  9. 相对路径 System.Web HttpServerUtilityBase Server.MapPath("~/")

    相对路径 System.Web  HttpServerUtilityBase  Server.MapPath("~/")

  10. 1.字符设备驱动------Linux中断处理体系结构

    一.中断处理体系结构的初始化 Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称. ...