输入一个数N,输出这个N个数的全排列情况;
如:输入N:3
输出:123 132 213 231 312 321
求N给数全排列的C++代码
答案:6 悬赏:70 手机版
解决时间 2021-12-23 18:14
- 提问者网友:兔牙战士
- 2021-12-23 14:29
最佳答案
- 五星知识达人网友:酒安江南
- 2022-01-22 03:16
直接可以运行
没有用任何难的技术
#include <iostream>
using namespace std;
int x;
int *array;
void Arrange( int *arr, int len )
{
if( len == 1 )
{
for( int i = 0; i < x; i++ )
{
cout << array[i] << " ";
}
cout << endl;
return ;
}
Arrange( arr + 1, len - 1 );
for( int j = 1; j < len; j++ )
{
int temp = arr[0];
arr[0] = arr[j];
arr[j] = temp;
Arrange( arr, len - 1 );
}
}
void main()
{
cin >> x;
array = new int[x];
for( int i = 0; i < x; i++ )
{
array[i] = i + 1;
}
Arrange( array, x );
}
没有用任何难的技术
#include <iostream>
using namespace std;
int x;
int *array;
void Arrange( int *arr, int len )
{
if( len == 1 )
{
for( int i = 0; i < x; i++ )
{
cout << array[i] << " ";
}
cout << endl;
return ;
}
Arrange( arr + 1, len - 1 );
for( int j = 1; j < len; j++ )
{
int temp = arr[0];
arr[0] = arr[j];
arr[j] = temp;
Arrange( arr, len - 1 );
}
}
void main()
{
cin >> x;
array = new int[x];
for( int i = 0; i < x; i++ )
{
array[i] = i + 1;
}
Arrange( array, x );
}
全部回答
- 1楼网友:归鹤鸣
- 2022-01-22 05:48
修改一下就可以了
#include <stdio.h>
int n;
int a[4]={4, 5, 8, 9};
void arrange(int a[], int m)
{
int temp, i;
if (m==1)
{
for(i=0; i<n; i++)
printf("%d ", a[i]);
printf("\n");
}
else
{
for(i=0; i<m; i++)
{
temp = a[m-1];
a[m-1] = a[i];
a[i] = temp;
arrange(a, m-1);
temp = a[i];
a[i] = a[m-1];
a[m-1] = temp;
}
}
}
void main()
{
n = 4;//size of a
arrange(a, n);
}
- 2楼网友:底特律间谍
- 2022-01-22 05:11
#include
#include
using namespace std;
int main()
{
int n;
cout << "input n" << endl;
// 输入n
cin >> n;
// 这里可以自己做输入错误检测
// 给数组动态分配大小
int* a = new int[n];
// 给数组赋值(要排列的数据源)
for(int i = 1; i <= n; i++)
{
a[i - 1] = i;
}
do{
// 依次输出每个数
for(int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
// 换行
cout << endl;
}
// 做排列计算(c++ stl函数)
while (next_permutation(a,a+n));
// 记得释放
delete[] a;
return 0;
}
运行结果如下:
- 3楼网友:杯酒困英雄
- 2022-01-22 04:43
用c++的stl。
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a[1000];
int i;
for(i=0;i<1000;i++)
a[i]=i+1;
int n;
cout<<"input the n:";
cin>>n;
do
{
for(i=0;i<n;i++)
cout<<a[i];
cout<<endl;
}
while(next_permutation(a,a+n));
return 0;
}
- 4楼网友:神也偏爱
- 2022-01-22 06:54
i will send a written blessing card for mom: then help mom back massage hammer, help my mother make health, for my beloved mother with a big gift -- to make a food dinner, let mother enjoy a happy fun! because my mother is too hard. i want to say to mother happy holidays! don't be so hard, and i wish my mother a beautiful young forever.
我会送一张写了祝福的贺卡给妈妈:然后帮妈妈槌背按摩,帮妈妈搞卫生,为我敬爱的母亲献上一份大礼物———自己亲手做一顿美食大餐,让母亲享受快乐的乐趣! 因为妈妈太辛苦了。我想对妈妈说节日快乐!不要再那么辛苦了,并祝妈妈永远年轻漂亮。
- 5楼网友:爱难随人意
- 2022-01-22 06:34
On mother's day this day what would you do for mother
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯