一个老问题了,输入一个正整数n,把1到n的全排列输出来,如何解决啊

kevin820601 2009-03-08 07:22:11
一个老问题了,输入一个正整数n,把1到n这n个数全排列打印出来, 如何解决啊
...全文
3969 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 grellen 的回复:]
C/C++ code#include<iostream>#include<vector>usingnamespacestd;intmain()
{intn;
vector<int>a;
cout<<"请输入一个正整数N:"<<endl;
cin>>n;for(inti=0; i<n; i++)
{
a.push_back(i+1);
}do{for( vector<int>::iterator it=a.begin(); it!=a.end(); it++)
{
cout<<*it<<"";
}
cout<<endl;
}while( next_permutation(a.begin(), a.end()) );return0;
}
[/Quote]
这个STL的。。。LZ似乎是想问算法
clc0925 2009-03-08
  • 打赏
  • 举报
回复
各位高手什么叫做全排列出来啊?具体是什么样子啊?
grellen 2009-03-08
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
vector<int>a;
cout<<"请输入一个正整数N:"<<endl;
cin>>n;
for(int i = 0; i < n; i++)
{
a.push_back(i+1);
}
do
{
for( vector<int>::iterator it = a.begin(); it != a.end(); it++ )
{
cout<<*it<<" ";
}
cout<<endl;
}while( next_permutation(a.begin(), a.end()) );

return 0;
}
sxljody 2009-03-08
  • 打赏
  • 举报
回复
#include<studio.h>
main()
{
int n,i;
printf("Please enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d ",i);
}
ht_61743904 2009-03-08
  • 打赏
  • 举报
回复
提一个想法 大家看看
假设n为5 可以用一个array[5]的数组来模拟2进制
00000
54321

然后对数组进行2进制相加
00001 代表1
00010 代表2
00011 代表12
00100 代表3
。。。。。。。

是不是可以做到全排列啊
aaajj 2009-03-08
  • 打赏
  • 举报
回复
用循环去做,类似x皇后问题
  • 打赏
  • 举报
回复
递归实现的全排列,自己看吧

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void swap(char *str1,char *str2)
{
char temp;
temp=*str1;
*str1=*str2;
*str2=temp;
}

void permStr(char *str,int i)
{
//printf("%d",i);
if(i==strlen(str)-1)
printf("%s\n",str);
else
{
for(int j=i;j<strlen(str);j++)
{
//printf("i %d,j %d",i,j);
swap(&str[i],&str[j]);
permStr(str,i+1);
swap(&str[i],&str[j]);
}
}
}

void main()
{
char str[]={"abcde"};

permStr(str,0);

}

liao05050075 2009-03-08
  • 打赏
  • 举报
回复
全排列的生成算法
http://blog.sina.com.cn/s/blog_4b8f2ad501000bjn.html

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧