codevs 1083 1083 Cantor表【模拟+二分改进】

题目链接:

http://codevs.cn/problem/1083/


一个模拟题,列表出来就是酱紫:

这样为了找到数据在哪一组,很明显就要二分一下,自己写一个改进的二分,类似STL中的lower_bound。。。

 1 #pragma comment(linker, "/STACK:16777216") //防爆栈
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cmath>
 8 #include<string>
 9 #include<vector>
10 #include<set>
11 #include<queue>
12 #include<map>
13 #include<stack>
14 #include<iterator>
15 using namespace std;
16 #define clr(c) memset(c, 0, sizeof(c));
17 #define pi acos(-1.0)
18 #define debug(x) cout<<"debug "<<x<<endl;
19 #define LLD "%I64d"
20 int dirx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
21 int diry[8] = {1, 0, -1, 0, 1, -1, -1, 1}; // 移动方向
22 const int INF = 0x3f3f3f3f;
23 const int mod = 1e9+7; // 模一个很大的素数, 素数的性质使模后得到相同数字的概率会低
24 const int _mod = 1e9+9; // int最多到2*10^9 long long最多到10^18 这样模后的运算也不会溢出
25 const double eps = 1e-8; // 浮点数精度
26 typedef long long ll;
27 typedef unsigned long long ull;
28 typedef struct point{
29     int x, y;
30     bool operator < (const point& p) const{
31         if(x == p.x) return y < p.y;
32         else return x < p.x;
33     }
34     bool operator > (const point& p) const{
35         return p < *this;
36     }
37 }p;
38 const int MAXL = 10005;
39 int sum[MAXL];
40 void pre(){
41     sum[1] = 1;
42     for(int i = 2; i <= 10000; i++){
43         sum[i] = sum[i-1]+i;
44     }
45 }
46 int binarySearch(int value){ // 修正的二分查找算法
47     //如果找到value就返回value所在的位置
48     //否则返回小于value的最大元素的位置
49     int l = 1;
50     int r = 10000;
51     while(l <= r){
52         int mid = (l+r) / 2;
53         if(sum[mid] == value) return mid;
54         else if(sum[mid] < value) l = mid + 1;
55         else r = mid - 1;
56     }
57     return r;//小于value的最大元素的位置
58     //l是大于value的最小元素的位置
59 }
60 int n;
61 int pos, Div, Sum, part1, part2;
62 
63 int main(){
64     pre();
65     while(~scanf("%d", &n)){
66         pos = binarySearch(n);
67         Div = n - sum[pos];
68         if(Div == 0){
69             Sum = pos+1;
70             if(pos%2 == 1){ // 奇数
71                 part1 = 1;
72                 part2 = Sum-part1;
73             }
74             else{
75                 part2 = 1;
76                 part1 = Sum-part2;
77             }
78         }
79         else{
80             Sum = pos+2;
81             if((pos+1)%2 == 1){ // 奇数
82                 part1 = Sum-Div;
83                 part2 = Div;
84             }
85             else{
86                 part1 = Div;
87                 part2 = Sum-part1;
88             }
89         }
90         printf("%d/%d
", part1, part2);
91 
92     }
93 
94     return 0;
95 }

原文地址:https://www.cnblogs.com/miaowTracy/p/5125481.html