c++ - n个数的全排列问题,不知道自己错在哪里了,求助~
ringa_lee
ringa_lee 2017-04-17 13:05:24
0
2
505

include<iostream>

include<stdlib.h>

using namespace std;

void swap(int list[], int m, int n)
{

int temp;
temp = list[m];
list[m] = list[n];
list[n] = temp;

}

void recurrence(int list[],int i, int j)
{

if (i == j)
{
    for (int t = 0; t <= j; t++)
    {
        cout << list[t];
    }
    cout << endl;
}
else
{
    for (int t = i; t <= j; t++)
    {
        swap(list, i, t);
        recurrence(list, i + 1, j);
        swap(list, i, t);

    }
}

}
int main()
{

/*
int b;
cout << "请输入总的个数:\n" << endl;
cin >> b;
int *a = new int[b];
recurrence(a, 0, b-1);
delete[]a;
*/
int *a;
int b;
cout << "请输入总的个数:\n" << endl;
cin >> b;
a = (int*)malloc(b*sizeof(int));
recurrence(a, 0, b - 1);
system("pause");
return 0;

}

我的运行结果是一串的负数

ringa_lee
ringa_lee

ringa_lee

reply all(2)
Peter_Zhu

I didn’t look at it very carefully. But the array a you came out with malloc has neither user input nor initialization, and its values ​​are random.

洪涛

a = (int)malloc(bsizeof(int));

for (int i = 0; i < b; i++)
{
    a[i] = i + 1;
}

recurrence(a, 0, b - 1);
Please insert this paragraph

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!