【链接】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. leetcode第一刷_Rotate List

    我提交了好多次,错误莫名其妙的,到后来才明确过来.原来我把题目给理解错了. 这个题的意思不是说让你把最后的那k个位置的元素移到前面来,这样的问题的做法就是用两个指针,先让一个走.走到一定的长度之后两个 ...

  2. final使用方法

     final的作用随着所修饰的类型而不同 1.final修饰类中的属性或者变量 不管属性是基本类型还是引用类型.final所起的作用都是变量里面存放的"值"不能变. 这个值,对 ...

  3. Android与webserver数据交互编程---3网络爬虫项目实现虚拟浏览器的jsp后台执行

    背景:原先的b/s设计中在一个jsp界面中实现多个复杂的工作流... 为实现移动接口的调用保证工作流的正常webproject特别给提供了该虚拟浏览器的方案 原理:通过该方案实现虚拟浏览器后台运行js ...

  4. php 内置的 html 格式化/美化tidy函数 -- 让你的HTML更美观

    php 内置的 html 格式化/美化tidy函数 https://github.com/htacg/tidy-html5 # HTML 格式化 function beautify_html($htm ...

  5. vim 基础学习之普通模式

    .操作 = 操作符 + 动作 aaa bbb例如,d是删除命令,b是移动到距离光标最近的字符串开头当我们执行db的时候,就会删除光标(不包括光标位置)到最近字串开头之间的字符dj则会删除光标所在行以及 ...

  6. Day1上午解题报告

    预计分数:100+60+0=160 实际分数:100+30+20=150 T1立方数(cubic) 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...

  7. Server.MapPath()的用法

    http://blog.csdn.net/qiuhaifeng_csu/article/details/19416407 Server.MapPath(string path)作用是返回与Web服务器 ...

  8. Autoencoders and Sparsity(一)

    An autoencoder neural network is an unsupervised learning algorithm that applies backpropagation, se ...

  9. Oracle primary key&foreign key

    --主键 alter table tablename1 add constraint pk_tablename1 primary key(column1);--增加数据表1的主键column1,如果是 ...

  10. Gym 100952 F. Contestants Ranking

    http://codeforces.com/gym/100952/problem/F F. Contestants Ranking time limit per test 1 second memor ...