有趣的数
描述
把分数按下面的办法排成一个数表。
1
1 3 6 10 15
1/1 1/2 1/3 1/4 1/5...
2/1 2/2 2/3 2/4
3/1 3/2 3/3
4/1 4/2
.........
我们以z字型方法给上表的每项编号。特定方法:第一项是1/1,然后是1/2、2/1、3/1、2/2、1/3、1/4、2/3……。编程输入项号N(1<=N<=100000),输出表中第N项。
输入
第一行有一个整数m(0<m<=10),表示有m组测试数据;
随后有m行,每行有一个整数N;
输出
输出表中第N项
样例输入
4
3
14
7
12345
样例输出
2/1
2/4
1/4
59/99
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test_4_1 {
publicstatic void main(String[] args) {
Scannersc=new Scanner(System.in);
intcount=sc.nextInt();
int[] shu=new int[count];
for(int i = 0; i < shu.length; i++) {
shu[i]= sc.nextInt();
}
for(int a = 0; a < shu.length; a++) {
intsum =0;//在第个数字内项出现此数字
inti=1 ;
while(true){
sum+=i;
if(sum>=shu[a]){
break;
}
i++;
}
List<Integer>zheng=new ArrayList<>();
for(int j = 1; j <= i; j++) {
zheng.add(j);
}
List<Integer>fan=new ArrayList<>();
for(int j = i-1; j >=0; j--) {
fan.add(zheng.get(j));
}
for(int j = i-1; j >=0; j--) {
if(sum==shu[a]){
if(i%2==0){ //偶数1/i开始遍历
System.out.println(zheng.get(j)+"/"+fan.get(j));
}else{//奇数从i/1开始遍历
System.out.println(fan.get(j)+"/"+zheng.get(j));
}
}
sum--;
}