蓝桥杯 试题 基础练习 01字串

试题 基础练习 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;
}

方法便有了

猜你喜欢

转载自www.cnblogs.com/pcdl/p/12408613.html