Q1:三角回文
思路:
写两个函数判断是否为回文和三角数
代码:
public class Question1 {
public static void main(String[] args) {
for (int i = 20220514; ; i++) {
if (isH(i) && isS(i)) {
System.out.println(i);
return;
}
}
}
public static boolean isH(int n) {
String str = n + "";
for (int i = 0; i <= str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) return false;
}
return true;
}
public static boolean isS(int n) {
int cnt = 0;
for (int i = 1; ; i++) {
if (cnt > n) return false;
if (cnt == n) return true;
cnt += i;
}
}
}
Q2:数数
思路:
从2开始,每一个数去除这个数,一直除到
,如果一共有12个就存起来,最后看一共有多少个,速度有点慢,得2-3分钟才跑完。
代码:
public class Question2 {
public static void main(String[] args) {
int cnt = 0;
for (int i = 2333333; i <= 23333333; i++) {
if (check(i)) {
cnt++;
}
}
System.out.println(cnt); //25606
}
public static boolean check(int n) {
int cnt = 0;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
n /= i;
cnt++;
}
}
if (n > 1) cnt++;
return cnt == 12;
}
}