(紀中)2154. 權勢二進制【數學】

(File IO): input:a.in output:a.out
時間限制: 1000 ms 空間限制: 262144 KB 具體限制
Goto ProblemSet


題目描述
一個十進制整數被叫做權勢二進制,當他的十進制表示的時候只由0或1組成。例如011011100110,1,101,110011都是權勢二進制而2129002,12,900不是。
當給定一個nn的時候,計算一下最少要多少個權勢二進制相加才能得到nn


輸入
kk組測試數據。
11行給出一個整數k(1<=k<=10)k (1<=k<=10)
22k+1k+1行每行一個整數n(1<=n<=1000000)n(1<=n<=1000000)

輸出
輸出答案佔kk行。
每行爲每個nn的答案。


樣例輸入
1
9

樣例輸出
9


數據範圍限制


解題思路
思路:貪心
因爲每一位最多爲11,所以就求出最大的數字(每個位置上)


代碼

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int k,n,maxn;
int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	scanf("%d",&k);
	for(int i=1;i<=k;i++)
	{
		maxn=0;
		scanf("%d",&n);
		while(n){
			maxn=max(maxn,n%10);
			n/=10;
		}
		printf("%d\n",maxn);
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章