64,641
社区成员
发帖
与我相关
我的任务
分享
// test1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#include <stdio.h>
int n = 0;
void swap(int *a, int *b)
{
int m;
m = *a;
*a = *b;
*b = m;
}
int z = 0;
//m为整数数组结束位置
void perm(int list[], int k, int m)
{
z++;
int i;
if(k > m)
{
for(i = 0; i <= m; i++)
printf("%d ", list[i]);
printf("\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[] = {1, 2, 3, 4, 5};
perm(list, 0, 4);
printf("total:%d\n", n);
cout<<z;
return 0;
}
//买一送一
#include <iostream>
#include <algorithm>
using namespace std;
template<typename T>
int next_perm(T* data, int n)
{
for (int i = n - 2; i >= 0; --i)
for (int j = n - 1; j > i; --j)
if (data[i] < data[j])
{
swap(data[i], data[j]);
sort(data+i+1, data+n);
return 1;
}
return 0;
}
int main()
{
char str[] = "12345";
int x = 0;
do
{
cout << str << endl;
++x;
} while (next_perm(str, 5));
cout << x << endl;
return 0;
}