题目描述
如下数列,前5项分别是1/1,1/2,2/1,3/1,2/2……。输入n,输出第n项。
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
5/1
样例输入
3
14
7
12345
样例输出
2/1
2/4
1/4
59/99
思路:
通过斜线对数据进行分类
#include <stdio.h> int main(void) { int i = 0;int j = 0;int count = 0;int k = 0; int left;int right; int flag = 0; scanf("%d",&count); while(1) { k++; if((k%2) == 0) { for(right = k,left = 1; right >= 1; right--,left++) { i++; printf("count:%d:%d/%d\n",i,left,right); if(i == count) { flag = 1; break; } } }else{ for(left = k,right = 1; left >= 1; left--,right++) { i++; printf("count:%d:%d/%d\n",i,left,right); if(i == count) { flag = 1; break; } } } if(flag > 0) break; } printf("%d/%d\n",left,right); return 0; }
时间: 2024-11-08 10:28:42