import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Solution s = new Solution(); s.doBefore(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++) { int n = sc.nextInt(); s.getResult(n); } sc.close(); } } class Solution { public ArrayList<int[]> curResult = new ArrayList<int[]>(); // public boolean mark = false; public void doBefore() { int last = 0; for(int i = 1; i <= 100000; i++) { if(f(i) == g(i)) { int[] arr = {i, ++last}; curResult.add(arr); } } } public void getResult(int n) { int result = 1; for(int i = 0; i < curResult.size(); i++) { if(curResult.get(i)[0] <= n) { result = curResult.get(i)[1]; } else break; } System.out.println(result); } // int curMostClosed = 1; // int result = 1; //当前结果是0; // // if (mark) { // for (int k : curResult.keySet()) { // if (k > curMostClosed && k <= n) { // curMostClosed = k; // } // } // result = curResult.get(curMostClosed); // } // mark = true; // // for(int i = curMostClosed + 1; i <= n; i++) { // if(f(i) == g(i)) // result++; // } // curResult.put(n, result); // // System.out.println(result); // } private int f(int n ) { int result = 0; while(n != 0) { result += n % 10; n /= 10; } return result; } private int g(int n ) { int result = 0; while(n != 0) { result += (n & 1) == 1 ? 1 : 0; n >>>= 1; } return result; } }
点赞 评论

相关推荐

不愿透露姓名的神秘牛友
07-24 18:34
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务