Skip to content

Commit cbbe444

Browse files
Added Conton problem
1 parent 4307d00 commit cbbe444

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

CONTON/QUESTION.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable.
2+
The proof works by using an explicit enumeration of rational numbers as shown in the diagram below.
3+
1/1 1/2 1/3 1/4 1/5 ...
4+
2/1 2/2 2/3 2/4
5+
3/1 3/2 3/3
6+
4/1 4/2
7+
5/1
8+
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.
9+
Input
10+
The input starts with a line containing a single integer t <= 20, the number of test cases. t test cases follow.
11+
12+
Then, it contains a single number per line.
13+
Output
14+
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.
15+
16+
Example
17+
Input:
18+
3
19+
3
20+
14
21+
7
22+
23+
Output:
24+
TERM 3 IS 2/1
25+
TERM 14 IS 2/4
26+
TERM 7 IS 1/4

CONTON/solution.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
int main()
7+
{
8+
9+
long long n,a,b,num,den;
10+
11+
int t;
12+
13+
cin>>t;
14+
15+
while(t--)
16+
17+
{
18+
19+
cin>>a;
20+
21+
n=(-1+sqrt(1+(8*a)))/2;
22+
23+
b=(n*(n+1))/2;
24+
25+
if(a==b)
26+
27+
{
28+
29+
if(a%2==0)
30+
31+
{
32+
33+
den=n;
34+
35+
num=1;
36+
37+
cout<<"TERM "<<a<<" IS "<<num<<"/"<<den<<endl;
38+
39+
}
40+
41+
else
42+
43+
{
44+
45+
den=1;
46+
47+
num=n;
48+
49+
cout<<"TERM "<<a<<" IS "<<num<<"/"<<den<<endl;
50+
51+
}
52+
}
53+
54+
else
55+
56+
{
57+
58+
if(n%2==0)
59+
60+
{
61+
62+
den=a-b;
63+
64+
num=n+2-den;
65+
66+
cout<<"TERM "<<a<<" IS "<<num<<"/"<<den<<endl;
67+
68+
}
69+
70+
else
71+
72+
{
73+
74+
num=a-b;
75+
76+
den=n+2-num;
77+
78+
cout<<"TERM "<<a<<" IS "<<num<<"/"<<den<<endl;
79+
80+
}
81+
}
82+
}
83+
return 0;
84+
}

0 commit comments

Comments
 (0)