Problem B: 编写函数:Swap (I) (Append Code)
Time Limit: 1 Sec Memory Limit: 16 MBSubmit: 9776 Solved: 7037
[ Submit][ Status][ Web Board]
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 579 -3-12 43 5
Sample Output
57 12-3 94 -125 3
HINT
“Append Code”中用到的头文件、全局变量或宏的定义应自行补充。
Append Code
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<bits/stdc++.h>
using namespace std;
void swap(int *x1,int *y1)
{
int temp;
temp=*x1;
*x1=*y1;
*y1=temp;
}
void swap(double *x2,double *y2)
{
double temp;
temp=*x2;
*x2=*y2;
*y2=temp;
}
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;
}
using namespace std;
void swap(int *x1,int *y1)
{
int temp;
temp=*x1;
*x1=*y1;
*y1=temp;
}
void swap(double *x2,double *y2)
{
double temp;
temp=*x2;
*x2=*y2;
*y2=temp;
}
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;
}