Fork me on GitHub

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

image

 

不需要思路,简单的模拟就可以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

 

.

posted @ 2017-12-10 18:20  CC11001100  阅读(291)  评论(0编辑  收藏  举报