# 第一个交换功能有什么问题？

``````#include
#include
using namespace std;
int tt=0;
void swap1 (int v[], int i, int j) {
int t;

t = v[i];
v[i] = v[j];
v[j] = t;
}
void permute(int arr[],int n,int index)
{
if(index==n)
{
for(int i=0;i< n ;j++)
{
swap1(arr,index,j);
permute(arr,n,index+1);
swap1(arr,j,index);
}
}
int main()
{
int arr[]={'a','b','c','d'};
permute(arr,4,0);
cout<``````

``````#include
#include
using namespace std;
int tt=0;
void swap(int v[],int i,int j)
{
v[i]= v[i] + v[j];
v[j]= v[i] - v[j];
v[i]= v[i] - v[j];
}
void permute(int arr[],int n,int index)
{
if(index==n)
{
for(int i=0;i< n ;j++)
{
swap(arr,index,j);
permute(arr,n,index+1);
swap(arr,j,index);
}
}
int main()
{
int arr[]={'a','b','c','d'};
permute(arr,4,0);
cout<``````
0

## 2 答案

``````v[i]= v[i] + v[j];
``````

``````a = a + b; //a <- (20 + 30) = 50, b still = 30.
b = a - b; //b <- (50 - 30) = 20, a still = 50.
a = a - b; //a <- (50 - 20) = 30, b still = 20.
``````

``````a = a + b; //a <- (20 + 20) = 40, AND b = 40 as well.
b = a - b; //b <- (40 - 40) =  0, AND a =  0 as well.
a = a - b; //a <- ( 0 -  0) =  0, AND b =  0 as well.
``````

0

@paxdiablo感谢您的建议....

+1，用于防止人们使用丑陋的黑客，因为我们不再使用8位微处理器上的汇编语言，所以没有任何好处。

``````void swap(int v[],int i,int j)
{
v[i]= v[i] + v[j];
v[j]= v[i] - v[j];
v[i]= v[i] - v[j];
}
``````

`i == j` （它随后将v [i]设置为0）时不起作用，这会发生在您的循环中

``````for (int j = index; j < n; ++j) {
swap(arr, index, j);
``````
0