首页 > 技术文章 > 蓝桥杯 试题 基础练习 01字串

pcdl 2020-03-04 11:55 原文

试题 基础练习 01字串

 

 思路:我们观察下样例,其实就是0~31的二进制,并且前面补0.所以

#include<iostream>
#include<algorithm>

using namespace std;

int decToBin(int dec){
    int result = 0, temp = dec, j = 1;
    while (temp){
        result = result + j * (temp % 2);
        temp = temp / 2;
        j = j * 10;
    }
    return result;
}
int main(){

    int i;
    for (i = 0; i < 32; i++){
        printf("%05d\n", decToBin(i));
    }

    system("pause");
    return 0;
}

 

方法便有了

 

推荐阅读