您的位置:首页 > 编程语言

牛客网编程练习之京东2017校招题:幸运数

2017-12-10 18:20 369 查看

 

不需要思路,简单的模拟就可以AC....

 

AC代码:

import java.util.Scanner;

/**
* @author CC11001100
*/
public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = 0;
for(int i=1; i<=n; i++){
ans += f(i) == g(i) ? 1 : 0;
}
System.out.println(ans);

}

private static int f(int n){
int res = 0;
while(n>0){
res+=n%10;
n/=10;
}
return res;
}

private static int g(int n){
int res = 0;
while(n>0){
res+= n & 0X1;
n>>=1;
}
return res;
}

}

 

题目来源: https://www.nowcoder.com/practice/4d1afe11171c44a385287e29092cdb3f?tpId=85&tqId=29881&tPage=1&rp=1&ru=/ta/2017test&qru=/ta/2017test/question-ranking

 

.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: