二分+找规律 之 codevs 1083 Cantor表
// [10/11/2014 Sjm]
/*
因为是 Z 字形,所以按斜着去看它,即:斜1:1/1; 斜2:1/2, 2/1; 斜3:1/3, 2/2, 3/1; …………
二分判断出第 N 项位于斜几,再找规律,即可求出第 N 项。。。
*/
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cmath> 5 #include <algorithm> 6 #include <functional> 7 #include <string> 8 #include <cstring> 9 #include <vector> 10 #include <stack> 11 #include <queue> 12 #include <map> 13 using namespace std; 14 #define eps 1e-8 15 #define MAX_LEN 10000 16 17 int main() { 18 int N; 19 scanf("%d", &N); 20 int myLeft = 1, myRight = 10000; 21 while (myRight - myLeft >= 1) { 22 int myMid = myLeft + ((myRight - myLeft) >> 1); 23 int myVal = (((myMid + 1)* myMid) >> 1); 24 if (myVal > N) myRight = myMid; 25 else { 26 if (myVal < N) myLeft = myMid + 1; 27 else { 28 myLeft = myMid; 29 break; 30 } 31 } 32 } 33 --myLeft; 34 int myCount = (((myLeft + 1)* myLeft) >> 1); 35 if (myLeft & 1) { 36 printf("%d/%d\n", N - myCount, myLeft + 2 - (N - myCount)); 37 } 38 else { 39 printf("%d/%d\n", myLeft + 2 - (N - myCount), N - myCount); 40 } 41 return 0; 42 }