C++交换函数如何交换的
#include<iostream>voidwsad(int,int);intmain(){intx=1,y=9;std::cout<<x<<","<<y<<"输出1:在...
#include <iostream>
void wsad(int,int);
int main()
{
int x=1,y=9;
std::cout<<x<<","<<y<<"输出1:在main函数中x值为:1,y的值为:9\n";
wsad(x,y);
std::cout<<x<<","<<y<<"输出4:在wsad函数中x值为:1,y的值为:9\n";
return 0;
}
void wsad(int x,int y)
{
std::cout<<x<<","<<y<<"输出2:在wsad函数中x值为:1,y的值为:9\n";
int z;
z=x;
x=y;
y=z;
//求告知为什么这里x变为9,y变为1?只知道是加入变量z通过z来交换的但不怎么理解!
std::cout<<x<<","<<y<<"输出3:在wsad函数中x值为:9,y的值为:1\n";
} 展开
void wsad(int,int);
int main()
{
int x=1,y=9;
std::cout<<x<<","<<y<<"输出1:在main函数中x值为:1,y的值为:9\n";
wsad(x,y);
std::cout<<x<<","<<y<<"输出4:在wsad函数中x值为:1,y的值为:9\n";
return 0;
}
void wsad(int x,int y)
{
std::cout<<x<<","<<y<<"输出2:在wsad函数中x值为:1,y的值为:9\n";
int z;
z=x;
x=y;
y=z;
//求告知为什么这里x变为9,y变为1?只知道是加入变量z通过z来交换的但不怎么理解!
std::cout<<x<<","<<y<<"输出3:在wsad函数中x值为:9,y的值为:1\n";
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询