帮小弟我接到一下循环变量是如何变化的
帮我接到一下循环变量是怎么变化的
#include <iostream.h>
void perm(char list[],int ,int );
void swap(char &,char &);
void main()
{
char list[3]={'a','b','c'};
perm(list,0,2);
}
void perm(char list[],int k,int m)
{
if(k==m)
{
for(int i=0;i<=m;i++)
{ cout<<endl;
cout<<"排序结果:";
cout<<list[i]<<" ";
}
cout<<endl;
}
else
for(int i=k;i<=m;i++)
{
cout<<"递归前的交换"<<endl;
cout<<list[k]<<" "<<list[i]<<endl;
swap(list[k],list[i]);
perm(list,k+1,m);
cout<<"递归后的交换"<<endl;
cout<<list[k]<<" "<<list[i]<<endl;
swap(list[k],list[i]);
}
}
void swap(char &a,char &b)
{
char temp=a;a=b;b=temp;
}
在递归调用的时候循环变量是怎么变化的?
------解决方案--------------------
for(int i=k;i <=m;i++)
{
cout < <"递归前的交换" < <endl;
cout < <list[k] < <" " < <list[i] < <endl;
swap(list[k],list[i]);
perm(list,k+1,m); //进入第归前i=a,退出这层第归i=a,各层的第归的循环变量相互独立,按正常方式改变
cout < <"递归后的交换" < <endl;
cout < <list[k] < <" " < <list[i] < <endl;
swap(list[k],list[i]);
}
#include <iostream.h>
void perm(char list[],int ,int );
void swap(char &,char &);
void main()
{
char list[3]={'a','b','c'};
perm(list,0,2);
}
void perm(char list[],int k,int m)
{
if(k==m)
{
for(int i=0;i<=m;i++)
{ cout<<endl;
cout<<"排序结果:";
cout<<list[i]<<" ";
}
cout<<endl;
}
else
for(int i=k;i<=m;i++)
{
cout<<"递归前的交换"<<endl;
cout<<list[k]<<" "<<list[i]<<endl;
swap(list[k],list[i]);
perm(list,k+1,m);
cout<<"递归后的交换"<<endl;
cout<<list[k]<<" "<<list[i]<<endl;
swap(list[k],list[i]);
}
}
void swap(char &a,char &b)
{
char temp=a;a=b;b=temp;
}
在递归调用的时候循环变量是怎么变化的?
------解决方案--------------------
for(int i=k;i <=m;i++)
{
cout < <"递归前的交换" < <endl;
cout < <list[k] < <" " < <list[i] < <endl;
swap(list[k],list[i]);
perm(list,k+1,m); //进入第归前i=a,退出这层第归i=a,各层的第归的循环变量相互独立,按正常方式改变
cout < <"递归后的交换" < <endl;
cout < <list[k] < <" " < <list[i] < <endl;
swap(list[k],list[i]);
}