我的思路,打印出 杨辉三角,定义一个计数器,对其元素进行一一比对,每轮循环使count++。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int count = 0;
long[][] arr = new long[100][100];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j <= i; j++) {
if(j == 0 || j == i){
arr[i][j] = 1;
}else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
count++;
if(arr[i][j] == n){
System.out.print(count);
return;
}
}
}
}
}
问题:
只获得了40分。
问题在于后面的测试用例过大,会出现超时的问题
研究别人的满分思路: 类比单调数列杨辉三角最外层全部是 1 。
第二层则是自然数序列。
因为杨辉三角是左右对称的,因此我们可以忽略右边(左边的数字总是比右边先出现),并将数字按层分成若干序列。
下面看不懂了,放弃了zzzz
2.ASCpublic class Test {
public static void main(String[] args) {
// System.out.println(65 + 'L' - 'A');
System.out.println((int)'L');
}
}
附上ASCII码表:
3.卡片个人思路:
对1到10000进行遍历,每遍历一个数,对其进行判断,因为数字1是用的最快的,故判断该数用了多少个1,定义一个计数器,直到1用完2021次,返回当前遍历的数字。
别人对此思路的理解:
弯道超车:观察 [ 1 , 9 ] [1,9][1,9] 这个区间中,[ 0 , 9 ] [0,9][0,9] 的出现情况。
在 [ 1 , 9 ] [1,9][1,9] 中,1 至 9各出现 1次。
把观察的范围扩大到 [ 1 , 99 ],十位的 1 11 出现 [ 10 , 19 ] [10,19][10,19] 共 10 次,十位的 2 出现 [ 20 , 29 ] 共 10次,⋯ ,十位的 9 出现 [ 90 , 99 ]共 10 次,低位 [ 0 , 9 ]重复出现 10次,1 11 至 9 各出现 20次,0 出现 9次。
将这个观察范围继续扩大,会发现 1的使用次数总是不小于 0、 2 至 9 ,也就是说统计 0 ,2 至 9 是没有意义的。
public class Main {
public static void main(String[] args) {
int count = 2021;
for (int i = 1; i < 10000; i++) {
int temp = i;
while (temp != 0){
if(temp % 10 == 1){
count--;
}
temp /= 10;
}
if(count <= 0){
System.out.println(i);
break;
}
}
}
}
答案正确,但总感觉思路有点毛病zzzzz
别人的解法:
public class Test {
public static void main(String[] args) { new Test().run(); }
void run() { System.out.println(calc(2021)); }
int calc(int upper) {
//定义一个数组存放0-9
int[] count = new int[10];
//定义一个无限循环的for
for (int n = 1, k = 1; ; k = ++n)
//对当前遍历的数进行每位上的拆分,在对应数组位上++
//当数组位超过2021时就返回该数,因为是do-while,故需要返回上一位
do
if (++count[k % 10] > upper)
return n - 1;
while ((k /= 10) > 0);
}
}



