CodeForces 538B Quasi Binary(不错的贪心题 进制想法)

题目思路:你现在有一些数叫quasibinary ,这些数仅由0 和1构成,如10,.101,0,1

现在给你k,你要用最少的quasibinary 数,加起来等于k,先输出个数,然后输出这些数是哪些.

一开始的思路比较挫,而且严重错误.

这边说一下正确的思维方法:由于我们有的数仅仅是0和1,所以这边假设4135,则至少要加5次.而且也只需要5次,

因为个位无论我们取什么,他每次都是1,所以至少取5次.至于其他位,我们按情况取就可以了.

我的做法是数组a[10],去存每一位要加多少次.假设最多的为maxn

然后遍历maxn次,每次都将需要加的1加掉.这样最终就是最少的.

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int a[10];
int b[10] = { 0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
int main()
{
	int k;
	while (~scanf("%d", &k))
	{
		int maxn = 0;
		int cur = 1;
		while (k)
		{
			maxn = max(maxn, k % 10);
			a[cur++] = k % 10;
			k /= 10;
		}
		printf("%d\n", maxn);
		int res = 0;
		for (int j = 1; j < cur; j++)
		if (a[j])
		{
			res += b[j];
			a[j]--;
		}
		printf("%d", res);
		for (int i = 1; i < maxn; i++)
		{
			res = 0;
			for (int j = 1; j < cur;j++)
			if (a[j])
			{
				res += b[j];
				a[j]--;
			}
			printf(" %d", res);
		}
		puts("");
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章