Java转换Python语法import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- > 0) { System.out.println(getN(scan.nextLong())); } } public static long getN(long n){ if (n == 1) { return 1; } long[] a = new long[1000]; a[1] = 1; long l, r; for (int i = 1; i <= 1000000000; i++) { l = 0; for (int j = 1; j <= i; j++) { r = a[j]; a[j] = l + r; /*每个数是上面两数之和*/ l = r; if(a[j] > n){ break; } if(a[3] > n){ n--; return (n * n + n) / 2 + n + 3; } if(a[j] == n){ return ((long) i * i + i) / 2 - i + j; } } } return -1; } }
时间: 2023-05-17 21:06:37 浏览: 95
java代码-import java.util.Scanner;
以下是Java代码转换为Python的语法:
import sys
def getN(n):
if n == 1:
return 1
a = [0] * 1000
a[1] = 1
for i in range(1, 1000000000):
l = 0
for j in range(1, i+1):
r = a[j]
a[j] = l + r #每个数是上面两数之和
l = r
if a[j] > n:
break
if a[3] > n:
n -= 1
return (n * n - n) // 2 + n - 3
if a[j] == n:
return ((i * i - i) // 2) - i + j
return -1
if __name__ == '__main__':
t = int(input())
for i in range(t):
n = int(input())
print(getN(n))
阅读全文