关注
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;
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
10-16 15:02
重庆大学 C++ 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 为了去实习,我赌上了___ #
18194次浏览 191人参与
# 摸鱼被leader发现了怎么办 #
70564次浏览 406人参与
# 百融云创求职进展汇总 #
130次浏览 0人参与
# uu们,春招你还来吗? #
10348次浏览 75人参与
# 2025年终总结 #
10734次浏览 191人参与
# 十二月请对我好一点 #
23196次浏览 325人参与
# 父母对你找工作是助力还是阻力? #
12349次浏览 191人参与
# 降低公积金和取消房补怎么选 #
23261次浏览 78人参与
# 工作中哪个瞬间让你想离职 #
109230次浏览 771人参与
# 哪一瞬间让你觉得“这班不如不上” #
9937次浏览 140人参与
# 一人推荐一个值得做的项目 #
8176次浏览 112人参与
# 运营每日一题 #
112573次浏览 885人参与
# 高薪高压 vs 低薪wlb,你怎么选? #
9582次浏览 108人参与
# 第一份工作能做外包吗? #
85635次浏览 573人参与
# 这些公司卡简历很严格 #
80157次浏览 366人参与
# 硬件人的简历怎么写 #
317005次浏览 3063人参与
# 工作前VS工作后,你的心态变化 #
12101次浏览 148人参与
# 秋招提前批启动你开冲了吗 #
160629次浏览 2244人参与
# 工作中出现了XX情况正常吗 #
29719次浏览 207人参与
# 公司福利里最没用的一项是啥 #
6170次浏览 94人参与
# 回顾今年你干过的最“勇”的一件事 #
12308次浏览 154人参与