2018年第九届蓝桥杯C/C++ C组国赛 —— 第三题:全排列

标题:全排列

对于某个串,比如:“1234”,求它的所有全排列。
并且要求这些全排列一定要按照字母的升序排列。
对于“1234”,应该输出(一共4!=24行):
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

下面是实现程序,请仔细分析程序逻辑,并填写划线部分缺少的代码。

#include <stdio.h>
#include <string.h>

//轮换前n个,再递归处理
void permu(char* data, int cur)
{
	int i,j;
	
	if(data[cur]=='\0'){
		printf("%s\n", data);
		return;
	}
	
	for(i=cur; data[i]; i++){
		char tmp = data[i]; 
		for(j=i-1; j>=cur; j--) data[j+1] = data[j];
		data[cur] = tmp;			

		permu(data, cur+1);			

		tmp = data[cur]; 
		___________________________________ ;  //填空
		data[i] = tmp;			
	}
}

int main()
{
	char a[] = "1234";
	permu(a,0);
	return 0;
}

请注意:只需要填写划线部分缺少的内容,不要抄写已有的代码或符号。

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 —— 2018_Finals_C_C++_3.cpp created by VB_KoKing on 2019-05-19:20.
/* Procedural objectives:

 Variables required by the program:

 Procedural thinking:

 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 <stdio.h>
#include <string.h>

//轮换前n个,再递归处理
void permu(char* data, int cur)
{
    int i,j;

    if(data[cur]=='\0'){
        printf("%s\n", data);
        return;
    }

    for(i=cur; data[i]; i++){
        char tmp = data[i];
        for(j=i-1; j>=cur; j--)
            data[j+1] = data[j];
        data[cur] = tmp;

        permu(data, cur+1);

        tmp = data[cur];
        data[cur] = data[cur+1];
//        ___________________________________ ;  //填空
        for(int j=cur; j<i; j++)
            data[j+1] = data[i];
        data[i] = tmp;
    }
}

int main()
{
    char a[] = "1234";
    permu(a,0);
    return 0;
}
posted @ 2019-05-19 21:04  AlexKing007  阅读(240)  评论(0编辑  收藏  举报