Count on Canton
2014-07-29 14:18
357 查看
A - Count on CantonTime Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64uSubmit StatusDescriptionOne of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below.
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/1In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on.InputThe input list contains a single number per line and will be terminated by endof-file.OutputYou are to write a program that will read a list of numbers in the range from 1 to 10^7 and will print for each number the corresponding term in Cantor's enumeration as given below.Sample Input
3 14 7Sample Output
TERM 3 IS 2/1 TERM 14 IS 2/4 TERM 7 IS 1/4
这个题需要根据题目提供的前五个数据,找出规律,从1/1开始,到1/2,在左斜着到2/1,是一个S型,斜着的每一个分子分母的和是个定值,且递增,判断时需要判断奇偶,来确定分子分母的变化
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;int main(){ int n,m,i,k; while(~scanf("%d",&n)) { m=n; int t=1; for(i=1;i<10000000;i++) { m=m-i; if(m<=i+1) { //printf("i=%d\n",i); t=i+1; break; } } int x=1,y=1; if(n==1) printf("TERM %d IS 1/1\n",n); else { if(t%2==0) { x=t; y=1; for(i=1;i<=t;i++) { if(i==m) printf("TERM %d IS %d/%d\n",n,y,x); x--; y++; } } else { x=1; y=t; for(i=1;i<=t;i++) { if(i==m) printf("TERM %d IS %d/%d\n",n,y,x); x++; y--; } } // printf("%d m=%d\n",t,m); } } return 0;}
相关文章推荐
- ZOJ 2185 Count on Canton
- HOJ 1649 Count on Canton
- POJ-1969-Count on Canton
- Count on Canton
- zoj 2185 || poj 1969 Count on Canton
- Count on Canton
- Count on Canton
- zoj 2185 Count on Canton
- hoj Count on Canton
- [POJ1969]Count on Canton
- SPOJ Count on a tree II(树上莫队)
- 主席树+dfs SPOJ BZOJ2588 Count on a tree
- 2588: Spoj 10628. Count on a tree 主席树+LCA
- bzoj2588:Count on a tree(可持久化线段树+Lca)
- SQLServer 存储过程 SET NOCOUNT { ON | OFF } 的使用(转载)
- BZOJ 2588: Spoj 10628. Count on a tree | 树上主席树
- SET ANSI_NULLS ON、SET NOCOUNT ON
- Spoj 10628. Count on a tree 树上主席树
- Hdu 4916 Count on the path
- [HDOJ 4916] Count on the path [树+乱搞]