给定一个整数,求它的二进制表示中有多少个1。


1
#include<stdio.h> 2 3 int countOne( int n ) 4 { 5 int count = 0; 6 while( n != 0 ) 7 { 8 n &= n - 1; 9 ++count; 10 } 11 return count; 12 } 13 14 int main() 15 { 16 int num; 17 printf("Input the number : "); 18 scanf( "%d", &num ); 19 printf( "The number of binary one is %d.\n", countOne(num) ); 20 return 0; 21 }

 

posted @ 2013-04-05 22:11  多解方程式  阅读(455)  评论(0编辑  收藏  举报