权势二进制(51Nod 1413)

版权声明:本文为博主原创文章,如果有错误欢迎指正,转载请留言且附带网址 https://blog.csdn.net/Mercury_Lc/article/details/82024018

一个十进制整数被叫做权势二进制,当他的十进制表示的时候只由0或1组成。例如0,1,101,110011都是权势二进制而2,12,900不是。

当给定一个n的时候,计算一下最少要多少个权势二进制相加才能得到n。

Input

单组测试数据。 
第一行给出一个整数n (1<=n<=1,000,000)

Output

输出答案占一行。

Sample Input

9

Sample Output

9

题解:无论给的什么数,只要找到这个数中每个位中最大的那个数x就可以了,用x个数,每个数都是这么长度的,其他位数的0和1自由分配就能组合成这样的数。

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
char s[55];  // 如果位数很大,字符串就很好的优势了。
int main()
{
    cin >> s;
    int m = -1;
    int n = strlen(s);
    for(int i = 0; i < n; i ++){
        if(s[i] - '0' > m)m = s[i] - '0';
    }
    printf("%d\n",m);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n;
    int max = -1;
    scanf("%d",&n);
    while(n)
    {
        int x = n % 10;
        if(x > max) max = x;
        n = n / 10;
    }
    printf("%d\n", max);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/82024018
今日推荐