程序如下
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
void Swap(T& a, T& b);
template<class T>
void Perm(T list[], int k, int m)
{
int i;
if (k==m)
{
for (i = 0; i <= m; i++)
cout << list[i];
cout << endl;
}
else
for (i = k; i <= m; i++){
Swap(list[k], list[i]);
Perm(list, k + 1, m);
Swap(list [k], list [i]);
}
}
template<class T>
inline void Swap(T& a, T& b)
{
T temp = a; a = b; b = temp;
}
int main()
{
int s, r, ac[20];
do
{
cout << "输入2-20个大于0的正整数,输入0结束";
for (r = 0; r <= 20; r++)
{
ac[r] = s;
}
} while (cin >> s&&s != 0);
Perm( ac, 0, r);
return 0;
}
输出是 858993460666666
模板不好调试,实例化后自己单步调试,把调试情况贴出来,别人可没时间帮你一步步调试找bug
数组越界了呀,
cout << "输入2-20个大于0的正整数,输入0结束";
for (r = 0; r <= 20; r++)
这里不能用<=,要<