2016年第七届蓝桥杯C/C++ A组国赛 —— 第一题:随意组合

标题:随意组合

小明被绑架到X星球的巫师W那里。

其时,W正在玩弄两组数据 (2 3 5 8) 和 (1 4 6 7)
他命令小明从一组数据中分别取数与另一组中的数配对,共配成4对(组中的每个数必被用到)。
小明的配法是:{(8,7),(5,6),(3,4),(2,1)}

巫师凝视片刻,突然说这个配法太棒了!

因为:
每个配对中的数字组成两位数,求平方和,无论正倒,居然相等:
872 + 562 + 342 + 212 = 12302
782 + 652+ 432 + 122 = 12302

小明想了想说:“这有什么奇怪呢,我们地球人都知道,随便配配也可以啊!”
{(8,6),(5,4),(3,1),(2,7)}

862 + 542 + 312 + 272 = 12002
682 + 452 + 132 + 722 = 12002

巫师顿时凌乱了。。。。。

请你计算一下,包括上边给出的两种配法,巫师的两组数据一共有多少种配对方案具有该特征。
配对方案计数时,不考虑配对的出现次序。
就是说:
{(8,7),(5,6),(3,4),(2,1)}

{(5,6),(8,7),(3,4),(2,1)}
是同一种方案。

注意:需要提交的是一个整数,不要填写任何多余内容(比如,解释说明文字等)

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 —— 2016_Finals_A_1.cpp created by VB_KoKing on 2019-05-04:19.
/* Procedural objectives:

 Variables required by the program:

 Procedural thinking:
 (2 3 5 8) 和 (1 4 6 7) 取数配对,明显是一个排列问题
 2分别与1 4 6 7配对,然后3与剩下的三个数配对,5与剩下的两个数配对,8与剩下的一个数配对。
 也就是说,总情况有4*3*2*1=24种

 换种思路,第一个数列的全排列与第二个数列的全排列进行组合,然后判断两个平方和是否相等,之后在去重。

 再思考一下,第一个数列的全排列已经能够组合出所有的情况了。
 Functions required by the program:

*/
/* 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 <algorithm>
using namespace std;
int main()
{
    int ans=0,num0[4]={2,3,5,8},num1[4]={1,4,6,7};

    do {
        int a=num0[0]*10+num1[0],b=num0[1]*10+num1[1],c=num0[2]*10+num1[2],d=num0[3]*10+num1[3];
        int e=num1[0]*10+num0[0],f=num1[1]*10+num0[1],g=num1[2]*10+num0[2],h=num1[3]*10+num0[3];
        if (a*a+b*b+c*c+d*d==e*e+f*f+g*g+h*h)
        {
            ans++;
            cout<<a<<"^2^+"<<b<<"^2^+"<<c<<"^2^+"<<d<<"^2^="<<a*a+b*b+c*c+d*d<<endl;
            cout<<e<<"^2^+"<<f<<"^2^+"<<g<<"^2^+"<<h<<"^2^="<<e*e+f*f+g*g+h*h<<endl;
            cout<<endl;
        }
    }while (next_permutation(num0,num0+4));
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43336281/article/details/89816915