題目描述
下面的圖形是著名的楊輝三角形:

如果我們按從上到下、從左到右的順序把所有數排成一列,可以得到如下
數列:1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ...
給定一個正整數 N,請你輸出數列中第一次出現 N 是在第幾個數?
輸入
輸入一個整數 N,
輸出
輸出一個整數代表答案,
樣例輸入
6
樣例輸出
13
提示
【評測用例規模與約定】
對于 20% 的評測用例,1 ≤ N ≤ 10;
對于所有評測用例,1 ≤ N ≤ 1000000000,
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[40][10005];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || i == j) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
}
int N = sc.nextInt();
int count = 1;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j <= i; j++) {
if (N == arr[i][j]) {
System.out.println(count);
return;
} else {
count++;
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/413298.html
標籤:其他
上一篇:Python 如何賺錢?學會我交給你的Python賺錢大法,就算是大學生,月入1W外快不在話下。
下一篇:力扣——演算法入門計劃第二天
