解法1cinclude<stdio.hintmain(){intnum=0;intcount=0;scanf("%d",&num);while(num){if(1num%2)count;num=num/2;}printf("二进制中1的个数=%d\n",count);return0;}//如果是负数,可以试试1,只循环一次(此代码存在问题)解法2(按位与法)cinclude<stdio.hintm
解法1(引第三变量)cinclude<stdio.hintmain(){inta=3;intb=5;inttmp=0;printf("before:a=%db=%d\n",a,b);tmp=a;a=b;b=tmp;printf("after:a=%db=%d\n",a,b);return0;}//效率高解法2(加减法)cinclude<stdio.hintmain(){inta=3;intb=5;
Copyright © 2005-2024 51CTO.COM 版权所有 京ICP证060544号