c++练习 ,速度

来源:百度知道 编辑:UC知道 时间:2024/06/04 17:06:21
C语言考试练习题_排列

Time Limit:1000MS Memory Limit:65536K
Total Submit:459 Accepted:170

Description

有4个互不相同的数字,输出由其中三个不重复数字组成的排列。

Input

4个整数。

Output

所有排列。

Sample Input

1 2 3 4

Sample Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 4
1 4 2
2 1 4
2 4 1
4 1 2
4 2 1
1 3 4
1 4 3
3 1 4
3 4 1
4 1 3
4 3 1
2 3 4
2 4 3
3 2 4
3 4 2
4 2 3
4 3 2

#include <iostream.h>
main()
{
int x=4;
int a,b,c;
for(a=1;a<=x;a++)
for(b=1;b<=x;b++)
for(c=1;c<=x;c++)
{
if(a!=b && b!=c && c!=a) cout<<a<<b<<c<<endl;

}
}

楼上的方法比较经典。
这个问题也可以用全排列的算法,不过你要背下来去考试,是不大可能的。

laji,速你ma个du,不会自己做啊