题解 | #求int型正整数在内存中存储时1的个数#

求int型正整数在内存中存储时1的个数

http://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9

用纯粹的数学方法计算,每次除2,有余数就加一,一直到剩下2或者1
import java.util.Scanner;

public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int x = in.nextInt(); //读取数字
//int x=5;
int cnt = 0; //计数变量
while (x>2){
if (x%2==1)cnt++;
x=x/2;
}
if(x==2||x==1)cnt++;
System.out.println(cnt);
}
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 13:54
点赞 评论 收藏
分享
废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务