【链接】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. MySpring dataSource从配置文件获取

    大神就不看了.本篇是一个人笔记. 原来的数据库配置文件是写死的. 看代码:Mybatis的配置文件 <bean id="dataSource" class="org ...

  2. hdu5305 Friends(dfs+map/hash)

    题目:pid=5305">http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给定N个人和M条朋友关系,是朋友关系的两个人之间有两种联系 ...

  3. mongodb官网文档阅读笔记:write concern

    write concern保证了mongodb写操作的级别,不同的write concern设置相应了不同级别的写操作.设置的级别越高.那么写操作的性能的持久化做得越好,可是写性能也就越差. mong ...

  4. jquery2.0.3 全部源码

    /*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...

  5. 14.字符串hash寻找第一个只出现一次的字符

    //char 0-255一共256个 char getonebyhash(char *str) { if (str == NULL) { return '\0'; } char ch = '\0'; ...

  6. Android 关于编译ijkplayer下的so经验分享

    前言:公司最近需要做直播方面的技术调研,所以需要去研究播放器相关的技术:刚好本人github上收藏了ijkplayer,之前一直没有研究过,现在刚好clone下来研究研究. 我先在Windows安装c ...

  7. 初学h5须知

    9.41.浏览器是页面的环境(类似水是鱼的环境)2.浏览器结构:title    标题,题目                           URL      网址                ...

  8. 删除D盘空目录 、检索大于10M的文件

    删除D盘空目录 @echo off for %%i in (d:\xx) do ( if exist %%i:\ ( for /f "delims=" %%a in ('dir / ...

  9. c#的中英文混合字符串截取

    public class StringHelper     {         public static string GetSubString(string str, int len)       ...

  10. Atcoder At Beginner Contest 068 C - Cat Snuke and a Voyage

    C - Cat Snuke and a Voyage Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem State ...