c++实验2-E(1107)Swap (I)

Problem E: 编写函数:Swap (I) (Append Code)

Time Limit: 1 Sec  Memory Limit: 16 MB
Submit: 18942  Solved: 12144
[Submit][Status]

Description

 

编写用来交换两个数的函数,使得“Append Code”中的main()函数能正确运行。

 

-----------------------------------------------------------------------------

用C实现三个函数int_swap()、dbl_swap()、SWAP(),其中SWAP()是个带参宏。

用C++实现两个函数,都以Swap()命名。

以上函数的调用格式见“Append Code”。这里不给出函数原型,它们的参数请通过main()函数自行确定。

 

Input

输入为4行,每行2个数。

Output

输出为4行,每行2个数。每行输出的两数为每行输入的逆序。

Sample Input

12 57 9 -3 -12 4 3 5

Sample Output

57 12 -3 9 4 -12 5 3

HINT

 

“Append Code”中用到的头文件、全局变量或宏的定义应自行补充。

 

Append Code

append.cc,

int main()
{
    int x1, y1;
      
    cin>>x1>>y1;
    Swap(&x1, &y1);
    cout<<x1<<" "<<y1<<endl;
      
    cin>>x1>>y1;
    Swap(x1, y1);
    cout<<x1<<" "<<y1<<endl;
  
    double x2, y2;
      
    cin>>x2>>y2;
    Swap(&x2, &y2);
    cout<<x2<<" "<<y2<<endl;
      
    cin>>x2>>y2;
    Swap(x2, y2);
    cout<<x2<<" "<<y2<<endl;
}

答案:

#include <iostream>
using namespace std;
void Swap(int* x,int* y)
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}
void Swap(int& x,int& y)
{
    int t;
    t=x;
    x=y;
    y=t;
}
void Swap(double* x,double* y)
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}
void Swap(double& x,double& y)
{
    int t;
    t=x;
    x=y;
    y=t;
}
int main()
{
    int x1, y1;

    cin>>x1>>y1;
    Swap(&x1, &y1);
    cout<<x1<<" "<<y1<<endl;

    cin>>x1>>y1;
    Swap(x1, y1);
    cout<<x1<<" "<<y1<<endl;

    double x2, y2;

    cin>>x2>>y2;
    Swap(&x2, &y2);
    cout<<x2<<" "<<y2<<endl;

    cin>>x2>>y2;
    Swap(x2, y2);
    cout<<x2<<" "<<y2<<endl;
}

分析:

指针

引用

函数重载

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值