【变量引用】面向对象程序设计上机练习四(变量引用)

来源:互联网 发布:杀肖数据统计资料区 编辑:程序博客网 时间:2024/05/29 18:17

Think:
【引用变量】建议参考博客

SDUT面向对象程序设计上机练习四(变量引用)
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description
将变量的引用作为函数形参,实现2个int型数据交换。

Input
输入2个int型整数。

Output
输出2个整数交换前后的值。

Example Input
88 66

Example Output
88 66
66 88

Hint

Author
zlh

以下为Accepted代码——(引用传递:将引用用作函数参数,使得函数中的变量名称为调用程序中的变量的别名)

#include <iostream>using namespace std;void Swap(int &x, int &y);int main(){    int a, b;    while(cin >> a >> b){        cout << a << ' ' << b << endl;        Swap(a, b);        cout << a << ' ' << b << endl;    }    return 0;}void Swap(int &x, int &y){    int t;    t = x, x = y, y = t;}
阅读全文
0 0
原创粉丝点击