牛客网编程练习之京东2017校招题:幸运数
不需要思路,简单的模拟就可以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; } }
.