B - Quasi Binary(暴力贪心)Codeforces Round #300

在这里插入图片描述
测试样例

Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11

解题思路: 我们需要构建最少的准二进制数目,必然是要充分利用 1 1 1所以我们可以从高位开始贪心(暴力贪心,由于位数很小,时间复杂度很低)取数,构成这样的准二进制数。 即可得出答案。

AC代码

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n;
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>n){
    
    
		vector<int> result;
		vector<int> temp;
		while(n>0){
    
    
			temp.push_back(n%10);
			n/=10;
		}
		//得到了n的所有位数,接下来从后往前遍历,并可以依次减小。
		int len=temp.size();
		for(int i=len-1;i>=0;i--){
    
    
			for(int j=1;j<=temp[i];j++){
    
    
				 int temp1=pow(10,i);
				 for(int k=i-1;k>=0;k--){
    
    
					if(temp[k]>0){
    
    
						temp1+=pow(10,k);
						temp[k]--;
					}
				 }
				 result.push_back(temp1);
			}
		}
		len=result.size();
		cout<<len<<endl;
		rep(i,0,len-1){
    
    
			cout<<result[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109198821
今日推荐