描述:
把分数按下面的办法排成一个数表。
1/1 1/2 1/3 1/4.....
2/1 2/2 2/3....
3/1 3/2 ....
4/1.....
.........
我们以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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for (int j = 0; j < a; j++) {
int m = sc.nextInt();
int i;
for (i = 1; m - i > 0; i++) {
m = m - i;
}
if (i % 2 == 1) {
System.out.println(i + 1 - m + "/" + m);
} else {
System.out.println(m + "/" + (i + 1 - m));
}
}
}
}
.....谁来给我讲一下...14为什么是2/4..
这是我原代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for (int j = 0; j < a; j++) {
int m = sc.nextInt();
int x = 1;
int y = 1;
for (int i = 1; i < m; i++) {
if (y == 1) {
y = x + 1;
x = 1;
} else {
y--;
x++;
}
}
System.out.println(x + "/" + y);
}
}
}
我自己手工找也是4/2 ... 为啥测试上就说2/4