2010-02-01.ural1064-pku1757
这个题。。。。
把题目中给的代码拷下来,给一个数组赋值,然后调用这个函数计算合法的所有值。
之后按照题目要求输出即可。
1
2 #define MAXN 10000
3 int A[MAXN];
4 int N,K,x;
5 int BinarySearch(int x)
6 {
7 int p, q, i, L;
8
9 p = 0; /* Left border of the search */
10 q = N-1; /* Right border of the search */
11 L = 0; /* Comparison counter */
12 while (p <= q) {
13 i = (p + q) / 2;
14 ++L;
15 if (A[i] == x) {
16 return L;
17 }
18 if (x < A[i])
19 q = i - 1;
20 else
21 p = i + 1;
22 }
23 return 0;
24 }
25
26 int vis[MAXN+10];
27 int main()
28 {
29 int i,j;
30 scanf("%d%d",&x,&K);
31 for (i = 0;i <= MAXN;i++) { A[i] = i; }
32 for (i = 1;i <= MAXN;i++) {
33 N = i;
34 if (K == BinarySearch(x)) {
35 //printf("%d accepted\n",i);
36 vis[i] = true;
37 }
38 }
39 int res = 0;
40 for (i = 1;i <= MAXN;i++) {
41 if (vis[i-1] == 0 && vis[i] == 1) { res++; }
42 }
43 printf("%d\n",res);
44 for (i = 1;i <= MAXN;i++) {
45 if (vis[i-1] == 0 && vis[i] == 1) { printf("%d ",i); }
46 if (vis[i] == 1 && vis[i+1] == 0) { printf("%d\n",i); }
47 }
48 return 0;
49 }
50