-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCANTON.cpp
70 lines (64 loc) · 1.21 KB
/
CANTON.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Author: Amitrajit Bose
Problem: https://www.spoj.com/problems/CANTON/
Approach: There are basically four elementary types of movements
Move A: Move horizontally right one time (j++) when i=1
Move B: Move diagonally downwards repeat:(i++,j--) until j=1
Move C: Move vertically down one time (i++) when j=1
Move D: Move diagonally upwards repeat:(i--,j++) until i=1
We keep repeating the moves until we reach the target element
*/
#include <iostream>
#define test ll t; cin>>t; while(t--)
typedef long long ll;
using namespace std;
int main()
{
test
{
ll targetterm;
ll term=1;
ll i=1;
ll j=1;
cin>>targetterm;
while(term<targetterm)
{
//Move A
j++;
term++;
//cout<<i<<"A/"<<j<<endl;
if(term==targetterm){
break;
}
//Move B
while(j>1 && term<targetterm){
i++;
j--;
term++;
//cout<<i<<"B/"<<j<<endl;
}
if(term==targetterm){
break;
}
//Move C
i++;
term++;
//cout<<i<<"C/"<<j<<endl;
if(term==targetterm){
break;
}
//Move D
while(i>1 && term<targetterm){
j++;
i--;
term++;
//cout<<i<<"D/"<<j<<endl;
}
if(term==targetterm){
break;
}
}
cout<<"TERM "<<targetterm<<" IS "<<i<<"/"<<j<<endl;
}
return 0;
}