给定一个组数,1到n,将其的排列结果输出,如
给定1 2 3,输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
向大家讨教算法。
给定1 2 3,输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
向大家讨教算法。
解决方案 »
- 哪位大侠能推荐有关多媒体编程的书吗?
- 我想问下大家 消息里的API函数 postmessage是怎样做到非阻塞的
- 在VC里,我读入一个BMP图象,怎么样以最合适的尺寸显示到VIEW里去?
- 如何让CFileDialog的Domodal函数执行时文件列表固定
- 中英文错作系统造成GUI显示不正确
- 自己想编一个简单的浏览器,但是要可以自己设定代理,不用ie的
- RSA算法中的提到“互质”,问:何为“互质”?
- 线程突然莫名的退出了?
- 有没什么函数可以用来判断某个窗口对应的HANDLE 是否为有效的HANDLE?
- 高手的挑战:Static Libray 与 share Library
- Jpeg编码中控制压缩品质的是哪一个步骤
- 我的树控件怎么不能显示出图标呢
#include <algorithm>
#include <iostream>
#include <vector>
void main()
{
std::vector<int> intV;
std::cout<<"输入整数,小于0结束:";
int i;
std::cin>>i;
while(i>=0){
intV.push_back(i);
std::cin>>i;
}
std::sort(intV.begin(),intV.end());
std::copy(intV.begin(), intV.end(), ostream_iterator<int>(cout, " "));
std::cout<<std::endl;
while(std::next_permutation(intV.begin(),intV.end())){
std::copy(intV.begin(),intV.end(),ostream_iterator<int>(cout," "));
std::cout<<std::endl;
}
}
/*初始字符串str[0,n)组成一个最小数(升序),从尾巴开始向头判断,
如果涵盖的字符串可以组成一个更大的数,则转换成最小的较大数并且
重新从尾巴向头判断。
比如1234,34可以组成更大的数字,将34变成43:1243。243可以组成比
它大的最小数324:1324。24又可以变成42:1342。342->423:1423。
23->32:1432...
*/
//BC++3.1
#include <iostream.h>
#include <string.h>
void sort(char*,char*);//排序成为升序
void display(const char*,const char*);//显示
int canGreater(const char*,const char*);
void beGreater(char*,char*);
void main()
{ char buff[10],ch;
int i,length;
cout<<"Input numbers:"<<endl;
cin>>buff;
length=strlen(buff);
sort(buff,buff+length);
cout<<"The result is:"<<endl;
display(buff,buff+length);
i=length-2;
while(canGreater(buff,buff+length)){
if(canGreater(buff+i,buff+length)){
beGreater(buff+i,buff+length);
display(buff,buff+length);
i=length-1;
}
else i--; }
}
void sort(char*first,char*end)
{ char *p1,*p2,ch;
for(;first<end-1;first++){
ch=*first;p2=first;
for(p1=first+1;p1<end;p1++){
if(ch>*p1){
ch=*p1;
p2=p1;
}//if
}//for(p1
*p2=*first;
*first=ch;
}//for
}void display(const char*first,const char*end)
{
for(;first<end;first++)
cout<<*first;
cout<<' ';
}
//判断[first,end)涵盖的字符串是否可以组成更大的数字
int canGreater(const char*first,const char*end)
{ const char *p;
for(p=end-1;p>first;p--)
if(*p>*(p-1)) return 1;
return 0;
}
//把[first,end)涵盖的字符串组成最小的更大数,
//其中[first+1,end)为降序
void beGreater(char*first,char*end)
{ char *p,ch=*first;
for(p=end-1;p>first;p--)
if(*p>*first)
{ ch=*p;
break;
}
*p=*first;
*first=ch;
sort(first+1,end);
}
void CTttView::OnTest()
{
// TODO: Add your command handler code here
char chr[10];
char preChr[10];
int n = 5;
strcpy(chr, "12345");
strcpy(preChr, "");
m_strtemp = "";
m_strAllSort = "";
allSortOut(chr, preChr, n);
AfxMessageBox(m_strAllSort);
}void CTttView::allSortOut(char* chr, char* preChr, int n)
{
if (n==1)
{
m_strAllSort += preChr;
m_strAllSort += chr[0];
m_strAllSort += "\t";
m_strtemp = "";
return;
}
for (int i=0; i<n; i++)
{
char chrTemp[10];
char chrPreT[10];
CString strTemp;
strTemp.Format("%s", preChr);
strTemp += chr[i];
strcpy(chrPreT, strTemp);
strTemp.Format("%s", chr);
strTemp.Delete(i,1);
strcpy(chrTemp, strTemp);
allSortOut(chrTemp, chrPreT, n-1);
}
}
void dosomething();int in[4] = {1,2,3,4};
int out[4];
void main()
{
array( in, 4, out );
}void array( int *pin, int length, int *pout )
{
if( length == 1 )
{
*pout = *pin;
dosomething();
}
else
{
int temp;
for( int i = 0; i < length; i++ )
{
*pout = *(pin+i);
*(pin+i) = *pin;
array( pin+1, length-1, pout+1 );
*(pin+i) = *pout;
}
}
}void dosomething()
{
for( int i = 0; i< 4; i++ )
cout << *(out+i);
cout << endl;
}
本题是不需要考虑什么算法的只要在输出上出一点点花招就可以了我用普通c为你编了如下代码不知是否OK!
设数组为a[n],设一变量m用来表示当前的第一输出位置。#include <stdio.h>
#define n 3
void main()
{
int a[n]={...............};
int i;
int m=1;
int dy();
dy(int m)
{
printf("%d ",a[m]);
for(i=1;i<=n;i++)
{
if(i==m) continue;
else if(i==n)
{
printf("\n");
m++;
dy(m);
}
else
printf("%d ",a[i]);
}
}
}
{//将长度为length的数组pin的所有排列组合通过pout输出
if( length == 1 )
{//pin长度为1,直接输出
*pout = *pin;
dosomething();
}
else
{//pin长度大于1
for( int i = 0; i < length; i++ )
{//取所有以第i个元素开头的排列组合
//pout中以第i个元素开头
*pout = *(pin+i);
//pin中第i个元素被pin中的第一个元素取代
//以便取得所有以第i个元素开头的组合
*(pin+i) = *pin;
//从pin的第二个元素开始,所有排列组合通过
//pout+1输出
array( pin+1, length-1, pout+1 );
//恢复pin原始状态
*(pin+i) = *pout;
}
}
}
array({a1,a2,a3,...}) =
for( i = 0; i < k; i++ )
ai + array({a2,a3,a4...a(i-1),a1,a(i+1),...ak})
---------------------------------
ai和a1对调然后去实现array()吧