cf-538B Quasi Binary【贪心】
2015-05-14 00:08
399 查看
cf-538B Quasi Binary
[code] time limit per test2 seconds memory limit per test256 megabytes
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 — are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
Input
The first line contains a single integer n (1 ≤ n ≤ 106).
Output
In the first line print a single integer k — the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers — the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn’t matter. If there are multiple possible representations, you are allowed to print any of them.
input
9
output
9
1 1 1 1 1 1 1 1 1
input
32
output
3
10 11 11
题目思路:贪心。比如说32–>3: 1 – 1 – 1 2: 1 – 1 – 0 所以,答案为11,11,10.
题目链接:cf 538B
以下是代码:
[code]#include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> using namespace std; int main(){ int n; cin >> n; vector <int> a; int maxsize = 0; for(int i = 0; n; i++) { a.push_back(n % 10); if (n % 10 > maxsize) maxsize = n % 10; //求出可以拆分成几个数字 n /= 10; } reverse(a.begin(),a.end()); cout << maxsize << endl; for (int i = 0; i < maxsize ;i++) { bool flag = 0; for (int j = 0; j < a.size(); j++) { if (a[j] > 0) { flag = 1; a[j]--; cout << 1; } else if (flag) cout << 0; } cout << " "; } return 0; }
相关文章推荐
- CF - 805C. Find Amir - 贪心思维+构造
- CF#318-Bear and Elections-贪心暴力
- CF 557A(Ilya and Diplomas-贪心)
- CF 329B(Biridian Forest-贪心-非二分)
- 贪心 CF 333B Chips
- cf#AIM Tech Round -C. Graph and String-贪心/ 二分图染色
- CF 342C(Cupboard and Balloons-难得对的贪心)
- CF 525D(Arthur and Walls-贪心2*2方格补全)
- CF - 752B. Santa Claus and Keyboard Check 贪心+字符串
- CF - 757B. Bash's Big Day - 贪心+暴力
- CF 779 C Dishonest Sellers 贪心,排序
- CF 778B Bitwise Formula 位运算,贪心
- CF - 782D. Innokenty and a Football League - 模拟+思维+贪心+dfs插入
- CF D. Population Size(贪心+模拟)
- CF-Pasha and Tea(贪心6)
- (CF 792E Colored Balls) 思维题 贪心
- Jzzhu and Chocolate - CF 449C 贪心
- cf 853 A planning [贪心]
- CF 600C Make Palidrome (贪心)
- CF 305A(Strange Addition-贪心+分类讨论)