解题思路:
这个题目,其实告诉你,四个不同的数的全排列的得到方式:看样例选取的四个数分别为1 2 3 4,细心你会发现,题目的输出就是告诉你,四个数的全排列输出时所有数的下标情况,那么,我们只需要,几次赋值操作,就可以得到4 个数的全排列了;

2017-12-03 23-28-39屏幕截图.png


看图中,把数分成了四组,我们只要得到每组开头的三个数(假设把它们存在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个赋值等式;还有更快的,把上面图片给的输出结果当做下标输出即可;(代码三)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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;
}

代码三:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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;
 
}


点赞(33)
 

3.4 分

70 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 19 条评论

无为 3年前 回复TA
这B是怎么顶着C的名号打C++代码还拿优质题解的???
牧乔 3年前 回复TA
暴力出奇迹
青云 4年前 回复TA
这波啊,我大意了没有闪
息巉 4年前 回复TA
666666太强了
英格拉姆 4年前 回复TA
为啥不直接输出样本数据哈哈哈开个玩笑
星空 4年前 回复TA
666
Arrays.sort 5年前 回复TA
大神啊,我实在是膜拜