经典名师---字典顺序输出全排列
2007-06-05 12:30
260 查看
#include<iostream>
#include<math.h>
#define MAXNUM 6
using namespace std;
int SWAPI(int &,int &);
float computer(float,float,int);
int main()
{
int perm[MAXNUM];
int last,left,right,i,j;
cout<<"Pleaset input 4 number"<<endl;
for(i=0;i<MAXNUM;i++)//初始入perm[i]
{
perm[i]=i+1;
}
for(last=0;last>=0;)
{
for(j=0;j<MAXNUM;j++)//输出结果
{
cout<<perm[j]<<" ";
}
cout<<endl;
for(last=MAXNUM-2;last>=0&&perm[last]>perm[last+1];last--);//从后往前找perm[i]<perm[i+1]
if(last<0)//没找到
{
break;
}
for(j=MAXNUM-1;perm[last]>perm[j];j--);//从后往前找perm[i]<perm[j]
SWAPI(perm[last],perm[j]);//交换
for(left=last+1,right=MAXNUM-1;left<right;left++,right--)
{
SWAPI(perm[left],perm[right]);
}
}
cin>>i;
return 0;
}
int SWAPI(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
return 0;
}
#include<math.h>
#define MAXNUM 6
using namespace std;
int SWAPI(int &,int &);
float computer(float,float,int);
int main()
{
int perm[MAXNUM];
int last,left,right,i,j;
cout<<"Pleaset input 4 number"<<endl;
for(i=0;i<MAXNUM;i++)//初始入perm[i]
{
perm[i]=i+1;
}
for(last=0;last>=0;)
{
for(j=0;j<MAXNUM;j++)//输出结果
{
cout<<perm[j]<<" ";
}
cout<<endl;
for(last=MAXNUM-2;last>=0&&perm[last]>perm[last+1];last--);//从后往前找perm[i]<perm[i+1]
if(last<0)//没找到
{
break;
}
for(j=MAXNUM-1;perm[last]>perm[j];j--);//从后往前找perm[i]<perm[j]
SWAPI(perm[last],perm[j]);//交换
for(left=last+1,right=MAXNUM-1;left<right;left++,right--)
{
SWAPI(perm[left],perm[right]);
}
}
cin>>i;
return 0;
}
int SWAPI(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
return 0;
}
相关文章推荐
- 牛客网三道编程题(四)华为机试在线训练(三)将数字逆序输出、将n个字符串按照字典顺序排序
- 用冒泡排序排列数组中的数字,并按一定顺序输出结果
- 按字典顺序打印字符串的所有排列
- 从标准输入读入一个由字母构成的串(不大于30个字符)。从该串中取出3个不重复的字符,求所有的取法。取出的字符,要求按字母升序排列成一个串。不同的取法输出顺序可以不考虑。
- 用java.lang.Math.random()语句,随机输出{size:自定义参数}个数不重复并且按顺序从小到大排列(冒泡排序)
- 用数组输出一段字符,并将其按照由大到小的顺序排列出来
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。
- :输入5个数(含负数、小数)将它们按由小到大的顺序排列起来 提示:需要排数的数字通过参数传递进来, 例如:输入:./a.out -1 2.1 -3 5 7 输出: -3 -1 2.1
- 经典名题2----字典顺序列出集合的子集
- 字符串练习:对字符串数组,按字典顺序排列大小
- 编写一个程序,输入用户的姓名和电话号码,按姓名的词典顺序排列后,输出用户的姓名和电话号
- 用字典顺序得到所有的排列
- 从键盘输入10个学生的姓名和成绩,按字典序排列学生的姓名并输出(姓名和成绩对应关系保持不变)
- 有 N个国家名,要求按字母先后顺序排列(用起泡排序法)后输出。
- 题目:输入5个数(含负数、小数)将它们按由小到大的顺序排列起来 提示:需要排数的数字通过参数传递进来, 例如:输入:./a.out -1 2.1 -3 5 7 输出: -3 -1 2.1
- 对输入的字符串按字典顺序输出所有的全排列,字符串可以由重复字符
- 在扑克牌游戏中,每次游戏开始都要求把54张牌重新排列一下,称为洗牌。试编写程序将一副扑克牌(用54个整数1~54表示)随机洗好后,顺序输出54张牌的情况。
- 从大到小的顺序排列,用函数将他们输出《学习记录》
- 给定一个只包含小写字母的字符串,删除重复的字母,每个字母只出现一次。在所有结果中,输出字典顺序最小的。
- Python按照书写顺序输出字典中的元素