关注
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;
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 你小心翼翼的闯过多大的祸? #
3673次浏览 68人参与
# 找不到实习会影响秋招吗 #
1399629次浏览 13635人参与
# 实习没事做是福还是祸? #
3911次浏览 66人参与
# 重来一次,你会对开始求职的自己说 #
884次浏览 19人参与
# 2025年终总结 #
133766次浏览 2285人参与
# 考研人,我有话说 #
156542次浏览 1211人参与
# 哪些公司笔/面试难度大? #
7049次浏览 32人参与
# 实习简历求拷打 #
23774次浏览 248人参与
# 你觉得现在还能进互联网吗? #
29935次浏览 201人参与
# 携程工作体验 #
18930次浏览 66人参与
# 大厂VS公务员你怎么选 #
69120次浏览 638人参与
# 扒一扒那些奇葩实习经历 #
140152次浏览 1149人参与
# 找不到好工作选择GAP真的丢人吗 #
93668次浏览 1007人参与
# 那些我实习了才知道的事 #
253029次浏览 1784人参与
# 非技术投递记录 #
672854次浏览 6820人参与
# 机械求职避坑tips #
81049次浏览 531人参与
# 投格力的你,拿到offer了吗? #
154852次浏览 829人参与
# 第一份工作能做外包吗? #
94039次浏览 599人参与
# 作业帮求职进展汇总 #
85388次浏览 559人参与
# 秋招遇到的奇葩面试题 #
101240次浏览 416人参与
