printf输出二进制

printf输出二进制

题意:

输出0到31的二进制数,要求前导零,保持位数为五位

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int main()
{
    for(int i = 0 ; i < 32 ; i ++){
        char s[10] = {0,0,0,0,0,0,0,0,0,0};
        itoa(i, s, 2);
        if(strlen(s) < 5) {
            for(int i = 5 - strlen(s) ; i > 0 ; i --) cout << "0" ;
            printf("%s",s);
        }
        else printf("%s",s);
        cout << endl ;
    }
    return 0;
}
posted @ 2021-11-17 11:03  ICE_棋  阅读(931)  评论(0)    收藏  举报