UVa 264 - Count on Cantor

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

找出在哪一斜排,再判斷分子分母的數字順序即可

Code

UVa 264UVa 264 - Count on Cantor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include<cstdio>

int main()
{
int slash[4500] = { 0, 1 };//到每一斜排的數字個數
for (int i = 2; i < 4500; i++)
slash[i] = slash[i - 1] + i;

int n;
while (scanf("%d", &n) != EOF)
{
int s = 0;//第幾斜排
for (s = 1; n > slash[s]; s++);

if (s & 1)//偶數排時,由上到下
printf("TERM %d IS %d/%d\n", n, slash[s] - n + 1, n - slash[s - 1]);
else//奇數排時,由下到上
printf("TERM %d IS %d/%d\n", n, n - slash[s - 1], slash[s] - n + 1);
}

return 0;
}