描述
把分数按下面的办法排成一个数表。
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
很相似于Z字形扩展
别人的总结:
自己把它想复杂了,或者是自己一开始根本就束手无策没弄明白思路。
可以把这个图形右转45度,成一金字塔形状,每一行的分母和分子都与上一行有关系,并且每行的最大编号实际上就是前面所有分母的和。
当分母为奇数时,编号从左往右进行,当分母为偶数时,编号从右往左进行。
然后就OK了。
#include <iostream>
#include <bits/stdc++.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int f(int n){
if(n==1)
return 1;
else
return f(n-1)+n;
}
int main(int argc, char *argv[]) {
int s,m,i,j;
cin>>s;
while(s--){
cin>>m;
int c=0;
int b=0;
for(i=1;;i++){
if(f(i)>=m){
c=f(i)-m;
b=i;
break;
}
}
if(b%2!=0)//奇数
cout<<c+1<<"/"<<b-c<<endl;
else//偶数
cout<<b-c<<"/"<<c+1<<endl;
}
return 0;
}