牛客網出題的一點小錯誤(關於全排列)

#include <bits/stdc++.h>
using namespace std;
 
void prem(int list[],int k,int m)
{
    if(k==m)
    {
        copy(list,list+m+1,ostream_iterator<int>(cout,","));cout<<endl;
        return ;
    }
    for(int i=k;i<=m;i++)
    {
        swap(list[k],list[i]);
        prem(list,k+1,m);
        swap(list[k],list[i]);
    }
}
int main()
{
	cout << "run at:http://www.anycodes.cn/zh/" << endl;
	int a[]={1,2,3,4,5};
	prem(a,0,3);
	return 0;
}

 一點點小錯誤而已:但咱們須要明白於心。ios

原文;c++

        copy(list,list+m,ostream_iterator<int>(cout,","));cout<<endl;
附帶可變參初始化鏈表:
  平臺兼容版本:gcc與vs2008+   anycodes
#include <iostream>
#include<cstdarg>
using namespace std;

struct List
{
    List(int x):val(x),next(NULL){}
    int val;
    List *next;
};

void Insert(List *p,int x)
{
    if(p == NULL)
        return ;
    while(p->next != NULL)
    {
        p = p->next;
    }
    List *tmp = new List(x);
    p->next = tmp;
}

void Print(List *p)
{
    while(p != NULL)
    {
        cout<<p->val<<" ";
        p = p->next;
    }
    cout<<endl;
}

void CreateList(List *p,...)
{
    if(p == NULL) return ;
    va_list var_arg;
    va_start(var_arg,p);
    int n = va_arg(var_arg,int);
    for(int i=0;i < n;++i)
    {
        int val = va_arg(var_arg,int);
        Insert(p,val);
    }
    
    va_end(var_arg);
}

int main()
{
    List * wz = new List(0);
  /*  for(int i=1;i <= 3;++i)
    {
        Insert(wz,i);
    }*/
    CreateList(wz,4,1,2,3,4);
    Print(wz);
    return 0;
}
相關文章
相關標籤/搜索