刘晖-6分.txt
来自「这是很不错的计算机算法」· 文本 代码 · 共 56 行
TXT
56 行
#include <iostream.h>
#include <fstream.h>
void perm(int *out,int g,int h); //函数声明
ofstream fout("output.txt");
int main ()
{
int n,k,*a;
ifstream fin("input.txt",ios::nocreate,filebuf::sh_read);
fin>>n;
if(fin.fail())
{
cout<<"the input.txt is not exist!";
return 1;
}
if ((a=new int[n])==NULL)
{
cout <<"内存不够,程序运行中止";
return -1;
}
for (k=0;k<n;k++) a[k]=k+1;
perm(a,0,n-1);
return 0;
}
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void perm(int *out,int g,int h)
{
int i;
if (g==h)
{
for(i=0;i<=h;i++) fout<<out[i];
fout<<endl;
}
else
{
for (i=g;i<=h;i++)
{
swap(out+g,out+i);
perm(out,g+1,h);
swap(out+g,out+i);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?