关注
import
java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
publicclass 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[]>();
publicboolean
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);
// }
privateint f(int
n
) {
int
result = 0;
while(n != 0) {
result +=
n % 10;
n /= 10;
}
return
result;
}
privateint g(int
n
) {
int
result = 0;
while(n != 0) {
result +=
(n & 1) == 1 ? 1 :
0;
n
>>>= 1;
}
return
result;
}
}
这是幸运数那道题,打注释的是我一开始写的方法,后来发现本地运行是可以的,但是OJ说是运行时错误,于是开始使劲想,发现其实输入大小不是很大,于是直接预处理搞了100000个数。结果这样就AC了
查看原帖
点赞 1
相关推荐
09-16 16:47
门头沟学院 C++ 点赞 评论 收藏
分享

点赞 评论 收藏
分享

点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 从顶到拉给所有面过的公司评分 #
20337次浏览 180人参与
# 机械人春招想让哪家公司来捞你? #
357393次浏览 3109人参与
# 为了求职,我做过的疯狂伪装 #
12545次浏览 230人参与
# 晒晒你的中秋福利 #
15225次浏览 98人参与
# 职场破冰,你们都聊什么? #
7232次浏览 74人参与
# 大家实习每天都在干啥 #
89076次浏览 518人参与
# 校招笔试 #
610次浏览 29人参与
# 机械笔面试考察这些知识点 #
10500次浏览 96人参与
# 你的公司给实习生发中秋礼物吗 #
1934次浏览 30人参与
# bilibili求职进展汇总 #
89533次浏览 810人参与
# 工作压力大怎么缓解 #
105189次浏览 1052人参与
# 秋招OC许愿 #
346859次浏览 2530人参与
# 广联达求职进展汇总 #
11038次浏览 50人参与
# 机械人怎么评价今年的华为 #
208984次浏览 1524人参与
# 宣讲会你有哪些意向不到的收获 #
1415次浏览 22人参与
# 聊聊这家公司值得去吗 #
558967次浏览 3712人参与
# 你面试被问到过哪些不会的问题? #
21935次浏览 817人参与
# 百度秋招提前批进度 #
150429次浏览 1770人参与
# 电网笔面经互助 #
46765次浏览 431人参与
# 秋招的嫡长offer #
30524次浏览 285人参与