import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); TreeMap<Integer,Integer> *** = new TreeMap<Integer,Integer>(); ***.put(1, 1); while(scan.hasNext()){ int t = scan.nextInt(); for(int i=0;i<t;i++){ int n = scan.nextInt(); Entry<Integer,Integer> last = ***.floorEntry(n); if(n==last.getKey()){ System.out.println(last.getValue()); continue; }else{ int count = last.getValue(); for(int x=last.getKey()+1;x<=n;x++){ if(f(x)==g(x)) count++; } ***.put(n, count); System.out.println(count); } } } scan.close(); } private final static int f(int x){ String str = String.valueOf(x); int sum = 0; for(int i=0;i<str.length();i++){ sum+=str.charAt(i)-'0'; } return sum; } private final static int g(int x){ int sum = 0; int flag = 1; while(x!=0){ sum += (x&flag); x>>=1; } return sum; } }
点赞 2

相关推荐

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