Problem X: 完善程序6-搜索算法——2

Problem X: 完善程序6-搜索算法——2

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 13  Solved: 1
[Submit] [Status] [Web Board] [Creator:]

Description

2.NOIP2009】寻找等差数列

有一些长度相等的等差数列(数列中每个数都为0~59的整数),设长度均为L,将等差数列中的所有数打乱顺序放在一起。现在给你这些打乱后的数,问原先,L最大可能为多大? n先读入一个数(1<=n<=60),再读入n个数,代表打乱后的数。输出等差数列最大可能长度L

#include <iostream>

#include <cstring>

using namespace std;

int hash[60];

int n, x, ans, maxnum;

int work(int now) {

       int first, second, delta, i;

       int ok;

       while (_____①____&& !hash[now])

              ++now;

       if (now > maxnum)

              return 1;

       first = now;

       for (second = first; second <= maxnum; second++)

              if (hash[second]) {

                     delta =_____②____;

                     if (first + delta *_____③____> maxnum)

                            break;

                     if (delta == 0)

                            ok = (_____④____);

                     else {

                            ok = 1;

                            for (i = 0; i < ans; i++)

                                   ok =_____⑤____&& (hash[first+delta*i]);

                     }

                     if (ok) {

                            for (i = 0; i < ans; i++)

                                   hash[first+delta*i]--;

                            if (work(first))

                                   return 1;

                            for (i = 0; i < ans; i++)

                                   hash[first+delta*i]++;

                     }

              }

       return 0;

}

int main() {

       int i;

       memset(hash, 0, sizeof(hash));

       cin >> n;

       maxnum = 0;

       for (i = 0; i < n; i++) {

              cin >> x;

              hash[x]++;

              if (x > maxnum)

                     maxnum = x;

       }

       for (ans = n; ans >= 1; ans--)

              if ( n%ans==0 && ____⑥_____) {

                     cout << ans << endl;

                     break;

              }

       return 0;

}


选择题

1)①处应填(   

  A. now < maxnum                                              B. now >= maxnum           

C. now <= maxnum                                           D. now > maxnum

2)②处应填(   

  A. second-first                                                   B. second                   

C. first-second                                                   D. second-ans

3)③处应填(   

  A. ans-1                                                             B. ans-delta                       

C. ans                                                                D. ans-first   

4)④处应填(   

  A. hash[first] > ans                                             B. hash[first] >= ans                         

C. hash[second] <= ans                                      D. hash[second] < ans

5)⑤处应填(   

  A. 0                                                                    B. (!ok)          

C.1                                                                     D. ok     

6处应填(   

  A. work(0)                                                          B. work(0) <= 0                         

C. work(ans)                                                       D. (!work(ans) )