用一个函数实现2个变量值的交换:

#include<iostream>
#include<cstdio>
using namespace std;
int heap_size,n;
void swap(int &a,int &b)  //swap()作为独立函数,应用于交换。
{  int t=a;a=b;b=t; }

int main()
{   int x,y;
    cin>>x>>y;
    swap(x,y);
    cout<<x<<" "<<y;
    return 0;
}

使用stl中的交换函数swap( ),增加头文件 <algorithm>,也可完成交换。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int heap_size,n;
int main()
{
    int x,y;
    cin>>x>>y;
    swap(x,y);
    cout<<x<<" "<<y;
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-02-21
  • 2021-12-28
  • 2022-12-23
  • 2021-08-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2021-11-19
  • 2021-10-12
  • 2021-07-30
相关资源
相似解决方案