2018年第九届蓝桥杯C/C++ C组国赛 —— 第三题:全排列
生活随笔
收集整理的這篇文章主要介紹了
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^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^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.101 10 1.1 ^^^1111110010 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. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 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^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 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; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的2018年第九届蓝桥杯C/C++ C组国赛 —— 第三题:全排列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018年第九届蓝桥杯C/C++ C组国
- 下一篇: 2018年第九届蓝桥杯C/C++ C组国