2017年第八届蓝桥杯C/C++ C组国赛 —— 第二题:数字划分

标题:数字划分

w星球的长老交给小明一个任务:
1,2,3…16 这16个数字分为两组。
要求:
这两组数字的和相同,
并且,两组数字的平方和也相同,
并且,两组数字的立方和也相同。

请你利用计算机的强大搜索能力解决这个问题。
并提交1所在的那个分组的所有数字。

这些数字要从小到大排列,两个数字间用一个空格分开。
即类似:1 4 5 8 … 这样的答案。

注意,只提交这一组数字,不要填写任何多余的内容。


笨笨有话说:
只要一个组的成员确定了,另一个组的成员也就确定了。枚举一个组的成员就可以了。
凭直觉,两个组的成员数目不会差太多吧。
歪歪有话说:
既然求 1 所在的那个组,那只要枚举剩余的成员就可以了。
貌似都是8个成员的可能性很大啊。

Code

/*
                                ^....0
                               ^ .1 ^1^
                               ..     01
                              1.^     1.0
                             ^ 1  ^    ^0.1
                             1 ^        ^..^
                             0.           ^ 0^
                             .0            1 .^
                             .1             ^0 .........001^
                             .1               1. .111100....01^
                             00                 11^        ^1. .1^
                             1.^                              ^0  0^
                               .^                                 ^0..1
                               .1                                   1..^
                             1 .0                                     ^  ^
                              00.                                     ^^0.^
                              ^ 0                                     ^^110.^
                          0   0 ^                                     ^^^10.01
                   ^^     10  1 1                                      ^^^1110.1
                   01     10  1.1                                      ^^^1111110
                   010    01  ^^                                        ^^^1111^1.^           ^^^
                   10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                    11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                    1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                   10   00 11                                               ^^^^^   1 0           1.
                   0^  ^0  ^0                                                ^^^^    0            0.
                   0^  1.0  .^                                               ^^^^    1 1          .0
                   ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                   1 ^      11                             1.                ^^^     ^ ^        ..^
                  ^..^      ^1                             ^.^               ^^^       .0       ^.0
                  0..^      ^0                              01               ^^^       ..      0..^
                 1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                ^  1.        00                              0.             ^^^        ^.0 ^.1
                . 0^.        ^.^                             ^.^            ^^^         ..0.0
               1 .^^.         .^                  1001        ^^            ^^^         . 1^
               . ^ ^.         11                0.    1         ^           ^^          0.
                0  ^.          0              ^0       1                   ^^^          0.
              0.^  1.          0^             0       .1                   ^^^          ..
              .1   1.          00            .        .1                  ^^^           ..
             1      1.         ^.           0         .^                  ^^            ..
             0.     1.          .^          .         0                                  .
             .1     1.          01          .        .                                 ^ 0
            ^.^     00          ^0          1.       ^                                 1 1
            .0      00           .            ^^^^^^                                   .
            .^      00           01                                                    ..
           1.       00           10                                                   1 ^
          ^.1       00           ^.                                            ^^^    .1
          ..        00            .1                                        1..01    ..
         1.1         00           1.                                       ..^      10
        ^ 1^         00           ^.1                                      0 1      1
        .1           00            00                                       ^  1   ^
         .           00            ^.^                                        10^  ^^
       1.1           00             00                                              10^
       ..^           1.             ^.                                               1.
      0 1            ^.              00                 00                            .^
        ^            ^.              ^ 1                00   ^0000^     ^               01
     1 0             ^.               00.0^              ^00000   1.00.1              11
     . 1              0               1^^0.01                      ^^^                01
      .^              ^                1   1^^                                       ^.^
    1 1                                                                              0.
    ..                                                                              1 ^
     1                                                                               1
   ^ ^                                                                             .0
   1                                                                             ^ 1
   ..                                                          1.1            ^0.0
  ^ 0                                                           1..01^^100000..0^
  1 1                                                            ^ 1 ^^1111^ ^^
  0 ^                                                             ^ 1      1000^
  .1                                                               ^.^     .   00
  ..                                                                1.1    0.   0
  1.                                                                  .    1.   .^
  1.                                                                 1    1.   ^0
 ^ .                                                                 ^.1 00    01
 ^.0                                                                  001.     .^
 */
// VB_king —— 2017_Finals_C_C++_2.cpp created by VB_KoKing on 2019-05-09:09.
/* Procedural objectives:

 Variables required by the program:

 Procedural thinking:
 两组数字的和相等,所以都是(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16)/2;
 Functions required by the program:
 
 Determination algorithm:
 
 Determining data structure:
 

*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first gentle breeze that passed through my ear is you,
The first star I see is also you.
The world I see is all your shadow."

FIGHTING FOR OUR FUTURE!!!
*/
#include <iostream>
#include <cstring>

using namespace std;

bool temp[16];
int sum1 = 0, sum2 = 0, sum3 = 0;
int num[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

void dfs(int n, int ans1, int ans2, int ans3) {
    if (n == 16)
        return;
    if (ans1 > sum1 || ans2 > sum2 || ans3 > sum3)
        return;
    if (ans1 == sum1 && ans2 == sum2 && ans3==sum3) {
        for (int i = 0; i < 16; i++)
            if (!temp[i]) cout << num[i] << ' ';
        cout << endl << endl;
    }
    dfs(n + 1, ans1, ans2, ans3);
    temp[n] = true;
    dfs(n + 1, ans1 + num[n], ans2 + num[n] * num[n], ans3 + num[n] * num[n] * num[n]);
    temp[n] = false;
}

int main() {
    memset(temp, 0, sizeof(temp));
    for (int i : num) {
        sum1 += i;
        sum2 += i * i;
        sum3 += i * i * i;
    }
    sum1 /= 2;
    sum2 /= 2;
    sum3 /= 2;

    dfs(0, 0, 0, 0);
    return 0;
}
原文地址:https://www.cnblogs.com/AlexKing007/p/12338330.html