请友友帮忙用C++写一个小程序,万分感谢!
发布网友
发布时间:2024-10-22 09:17
我来回答
共2个回答
热心网友
时间:2024-10-22 17:43
#include <stdio.h>
#include <stdlib.h>
int array[] = {2,6,9,13,14,21,22,25,29,30,31,32,33,34};
int main() {
freopen("表1.txt", "w", stdout);
for(int i0=0; i0<14;++i0) {
for(int i1=i0+1; i1<14; ++i1){
for(int i2=i1+1; i2<14; ++i2){
for(int i3=i2+1; i3<14; ++i3){
for(int i4=i3+1; i4<14; ++i4) {
for(int i5=i4+1; i5<14; ++i5) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", array[i0], array[i1],array[i2],array[i3],array[i4],array[i5]);
}
}
}
}
}
}
//system("pause");
}
运行后 会生成一个 表1.txt文件,就是你要的
热心网友
时间:2024-10-22 17:40
#include <iostream>
using namespace std;
int n = 0;
FILE *fp;
void swap(int *a ,int *b)
{
int m ;
m = *a;
*a = *b;
*b = m;
}
void perm(int list[],int k, int m )
{
int i;
if(k >m)
{
for(i = 0 ; i <= m ; i++)
{
cout<<list[i]<<",";
// 这里插入输出到文件语句
fprintf(fp,"%d,",list[i]);
}
cout<<"\n";
fprintf(fp,"\n");
n++;
}
else
{
for(i = k ; i <=m;i++)
{
swap(&list[k],&list[i]);
perm(list,k+1,m);
swap(&list[k],&list[i]);
}
}
}
int main()
{
int list[] ={2,6,9,13,14,21,22,25,29,30,31,32,33,34};
fp=fopen("result.txt","wt");
perm(list,0,12);
cout<<"total:"<<n<<"/n";
fclose(fp);
return 0;
}