【HDOJ】2051 Bitset
水题。
#include <stdio.h> #include <string.h> #define MAXNUM 32 char bin[MAXNUM]; void itob(int a, char bin[]) { int tmp = a, i=0; char ch; memset(bin, 0, sizeof(bin)); do { ch = tmp&1; bin[i++] = ch + '0'; tmp = tmp >> 1; } while (tmp > 0); // bin[i] = '\0'; } int main() { int n; int i; while (scanf("%d", &n) != EOF) { itob(n, bin); for (i=strlen(bin)-1; i>=0; --i) printf("%c", bin[i]); printf("\n"); } return 0; }