Count on Canton
来源:互联网 发布:录音什么软件好 编辑:程序博客网 时间:2024/06/06 03:36
Count on Canton
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64uDescription
One 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.
In 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.
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
In 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.
Input
The input list contains a single number per line and will be terminated by endof-file.
Output
You 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
3147
Sample Output
TERM 3 IS 2/1TERM 14 IS 2/4TERM 7 IS 1/4
题意: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蛇形排列,第一个数是1/1,第二个数是1/2,第三个数是2/1,第四个数3/1,第五个数是2/2。。。。。。如此排列,问第多少个数是多少。解题思路:按照图中的排列,第一列有1个数,第二列有2个数,第n列有n个数,这是一个公差为1的等差数列,利用二分查找,找到要询问数所在列(即区间)的最大值,可知,当处于第i列的时候,分子分母和为i+1,由此可知规律:#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;long long cun[5000];int rfen(int l,int r,int n){ int mad,bj = -1; while(l <= r) { mad = (l + r)/2; if(cun[mad] < n) l = mad + 1; else { bj = mad; r = mad - 1; } } return bj;}int main(){ int i,j,k; int right,cha; long long n; cun[1] = 1; for(i = 2; i < 4472; i++) { cun[i] = i + cun[i-1]; } while(scanf("%lld",&n)!= EOF) { right = rfen(1,4472,n); cha = cun[right] - n; if(right % 2 == 0) printf("TERM %lld IS %d/%d\n",n,right - cha,cha + 1); else { printf("TERM %lld IS %d/%d\n",n,cha + 1,right - cha); } } return 0;}
0 0
- hoj Count on Canton
- Count on Canton
- Count on Canton
- Count on Canton
- Count on Canton
- zoj 2185 Count on Canton
- poj 1969 Count on Canton
- HOJ 1649 Count on Canton
- ZOJ 2185 Count on Canton
- POJ 1909 Count on Canton
- POJ-1969-Count on Canton
- No.1969 Count on Canton (ACM)
- zoj 2185 || poj 1969 Count on Canton
- poj 1969 Count on Canton 摘规律
- POJ 1969---Count on Canton【数学问题】
- UVA 264 Count on Canton 找规律
- POJ 1969 Count on Canton 笔记
- POJ 1969 Count on Canton(数学题,找规律)
- Android S5PV210 cameraHAL 录像功能分析
- MakerBot 使用Linux进行开源创新的经验与教训
- 【Java编程基础】Chapter 3:流程控制语句
- hdu 1871 无题
- linux设备驱动之USB数据传输分析 三
- Count on Canton
- C++ primer(第五版) 练习 5.10 个人code
- [ACM-PAT]逆序对
- HDU3988-Harry Potter and the Hide Story(数论-质因数分解)
- Oracle RAC 常用工具和命令
- POJ 1286 Necklace of Beads(Polya简单应用)
- [题解][LeetCode][Valid Palindrome]
- 《除法与取余运算符》
- 找朋友(bfs)