Luo Gu P1014.Cantor table
Title Description
One of the famous proof of modern mathematics is Georg Cantor proved the rational numbers is enumerable. He is following this one table to prove this proposition:
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, …
... Our Z-shape to each number on the table. The first 1/1, then 1 / 2,2 / 1,3 / 1,2 / 2, ...
Input Format
Integer N (1≤N≤10000000)
Output Format
Item table N
Sample input and output
Input Sample # 1
7
Sample Output # 1
1/4
Topic ideas
#include<iostream>
#define ll long long
using namespace std;
int main()
{
int flag = 1;
ll n,i=1,j=1,t=1;
scanf("%lld",&n);
while(1){
if(t==n){
printf("%lld/%lld",i,j);
return 0;
}
switch(flag)
{
case 1:
j++;
t++;
flag = 2;
break;
case 2:
i++;
j--;
t++;
if(j==1)flag = 3;
break;
case 3:
i++;
t++;
flag = 4;
break;
case 4:
i--;
j++;
t++;
if(i==1)flag = 1;
break;
}
}
return 0;
}