原题链接:C语言考试练习题_排列
解题思路:
这个题目,其实告诉你,四个不同的数的全排列的得到方式:看样例选取的四个数分别为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分
70 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
#include<stdio.h> #include<string.h> int main(void) { int ch[4]; int i, j, k; for(int c = 0; c < 4; c++) scanf("%d", &ch[c]); for(i = 0; i < 4; i++) for(j = 0; j < 4; j++) for(k = 0; k < 4; k++) if(ch[i]!= ch[j]&&ch[j]!= ch[k]&&ch[i]!=ch[k]) printf("%d %d %d\n", ch[i], ch[j], ch[k]); return 0; }//简单一点#include<stdio.h> int main() { int arr[4]; int i, j,z; for (i = 0; i < 4; i++) { scanf_s("%d", &arr[i]); } for (i = 0; i < 4; i++) { for (j = 3; j > i; j--) { for (z = 0; z < 4; z++) { if (arr[z] != arr[i] && arr[z] != arr[j]) { printf("%d %d %d", arr[i], arr[j], arr[z]); printf("\n"); } } } } for (i = 3; i>=0; i--) { for (j = 0; j < i; j++) { for (z = 0; z < 4; z++) { if (arr[z] != arr[i] && arr[z] != arr[j]) { printf("%d %d %d", arr[i], arr[j], arr[z]); printf("\n"); } } } } }