标签:
#include <stdio.h> #include <stdlib.h> /* 4.统计一个整数对应的二进制数的1的个数。 输入一个整数(可正可负), 输出该整数的二进制包含1的个数, “ctl+ z” 结束。 */ int main(){ int number; while (scanf("%d", &number) != EOF){ int num[100] = {0}; int length = 0; if (number < 0) number = -number; do{ num[length++] = number % 2; number /= 2; } while (number != 0); int count = 0; for (int i = 0; i < length; ++i) if (num[i] == 1) count++; printf("The binary form of this number is: "); for (int i = length - 1; i >= 0; --i) printf("%d",num[i]); printf("\n"); printf("The number of 1 is: %d.\n",count); } system("pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/mutaohengheng/p/5104714.html