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

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

Antworte allen(2)
Peter_Zhu

没有特别仔细看. 但你那个malloc 出来的a数组, 既没有用户输入, 也没有初始化, 它的数值是随机的.

洪涛

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

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

recurrence(a, 0, b - 1);
请插入这段

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!