解题思路:
这个题目,其实告诉你,四个不同的数的全排列的得到方式:看样例选取的四个数分别为1 2 3 4,细心你会发现,题目的输出就是告诉你,四个数的全排列输出时所有数的下标情况,那么,我们只需要,几次赋值操作,就可以得到4 个数的全排列了;
看图中,把数分成了四组,我们只要得到每组开头的三个数(假设把它们存在A[3]里),就可以输出全排列;
因为,每一组,都是把A[3]中的元素,以下标(1,2,3)(1,3,2)(2,1,3)(2,3,1)(3,1,2)(3,2,1)分别输出;
但是注意数组下标从0开始,每个都要减去1;
代码1: (观察其中赋值操作有重复的,化简后得到:代码2)最后6个赋值等式;还有更快的,把上面图片给的输出结果当做下标输出即可;(代码三)
#include<iostream> using namespace std; void output(); int A[3]; int B[4]; int main() { for(int i=0;i<4;i++) cin>>B[i]; /*------------------------------*/ A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; output(); /*------------------------------*/ A[0]=B[0]; A[1]=B[1]; A[2]=B[3]; output(); /*------------------------------*/ A[0]=B[0]; A[1]=B[2]; A[2]=B[3]; output(); /*------------------------------*/ A[0]=B[1]; A[1]=B[2]; A[2]=B[3]; output(); /*------------------------------*/ return 0; } void output() { cout<<A[0]<<" "<<A[1]<<" "<<A[2]<<endl; cout<<A[0]<<" "<<A[2]<<" "<<A[1]<<endl; cout<<A[1]<<" "<<A[0]<<" "<<A[2]<<endl; cout<<A[1]<<" "<<A[2]<<" "<<A[0]<<endl; cout<<A[2]<<" "<<A[0]<<" "<<A[1]<<endl; cout<<A[2]<<" "<<A[1]<<" "<<A[0]<<endl; }
代码2:
#include<iostream> using namespace std; void output(); int A[3]; int B[4]; int main() { for(int i=0;i<4;i++) cin>>B[i]; /*------------------------------*/ A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; output(); /*------------------------------*/ A[2]=B[3]; output(); /*------------------------------*/ A[1]=B[2]; output(); /*------------------------------*/ A[0]=B[1]; output(); /*------------------------------*/ return 0; } void output() { cout<<A[0]<<" "<<A[1]<<" "<<A[2]<<endl; cout<<A[0]<<" "<<A[2]<<" "<<A[1]<<endl; cout<<A[1]<<" "<<A[0]<<" "<<A[2]<<endl; cout<<A[1]<<" "<<A[2]<<" "<<A[0]<<endl; cout<<A[2]<<" "<<A[0]<<" "<<A[1]<<endl; cout<<A[2]<<" "<<A[1]<<" "<<A[0]<<endl; }
代码三:
#include<iostream> using namespace std; int A[5]; int main() { for(int i=1;i<=4;i++) cin>>A[i]; cout<<A[1]<<" "<<A[2]<<" "<<A[3]<<endl; cout<<A[1]<<" "<<A[3]<<" "<<A[2]<<endl; cout<<A[2]<<" "<<A[1]<<" "<<A[3]<<endl; cout<<A[2]<<" "<<A[3]<<" "<<A[1]<<endl; cout<<A[3]<<" "<<A[1]<<" "<<A[2]<<endl; cout<<A[3]<<" "<<A[2]<<" "<<A[1]<<endl; cout<<A[1]<<" "<<A[2]<<" "<<A[4]<<endl; cout<<A[1]<<" "<<A[4]<<" "<<A[2]<<endl; cout<<A[2]<<" "<<A[1]<<" "<<A[4]<<endl; cout<<A[2]<<" "<<A[4]<<" "<<A[1]<<endl; cout<<A[4]<<" "<<A[1]<<" "<<A[2]<<endl; cout<<A[4]<<" "<<A[2]<<" "<<A[1]<<endl; cout<<A[1]<<" "<<A[3]<<" "<<A[4]<<endl; cout<<A[1]<<" "<<A[4]<<" "<<A[3]<<endl; cout<<A[3]<<" "<<A[1]<<" "<<A[4]<<endl; cout<<A[3]<<" "<<A[4]<<" "<<A[1]<<endl; cout<<A[4]<<" "<<A[1]<<" "<<A[3]<<endl; cout<<A[4]<<" "<<A[3]<<" "<<A[1]<<endl; cout<<A[2]<<" "<<A[3]<<" "<<A[4]<<endl; cout<<A[2]<<" "<<A[4]<<" "<<A[3]<<endl; cout<<A[3]<<" "<<A[2]<<" "<<A[4]<<endl; cout<<A[3]<<" "<<A[4]<<" "<<A[2]<<endl; cout<<A[4]<<" "<<A[2]<<" "<<A[3]<<endl; cout<<A[4]<<" "<<A[3]<<" "<<A[2]<<endl; }
0.0分
83 人评分
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:719 |
回文数字 (C++代码)浏览:890 |
简单的a+b (C语言代码)浏览:528 |
简单的a+b (C语言代码)浏览:764 |
蓝桥杯历届试题-九宫重排 (C++代码)浏览:2812 |
C语言程序设计教程(第三版)课后习题10.1 (Java代码)浏览:1492 |
A+B for Input-Output Practice (IV) (C++代码)浏览:713 |
多输入输出练习1 (C语言代码)浏览:1219 |
字符串对比 (C语言代码)浏览:1471 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:548 |