c++怎么用引用变量交换两个整数的数值
3个回答
展开全部
//--------------------------------
// 环境:VS2005 // 用途:不用中间变量交换两个数 // 时间:2010.9.25 // 作者:http://pppboy.blog.163.com //-------------------------------- #include "stdafx.h" #include <iostream> using namespace std; //交换a,b void fSwap1(int& a, int& b) { a = a ^ b; b = a ^ b; a = a ^ b; } //交换a,b的另一种方法 void fSwap2(int& a, int& b) { a = a + b; b = a - b; a = a - b; } //再一种方法 void fSwap3(int& a, int& b) { a = a * b; b = a / b; a = a / b; } int main(int argc, char* argv[]) { int a, b; cout << "------------------------------" << endl; cout << "input a :" << endl; cin >> a; cout << "input b :" << endl; cin >> b; cout << "------------------------------" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "------------------------------" << endl; fSwap1(a, b); cout << "after swap (1):" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; fSwap2(a, b); cout << "after swap (2):" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; fSwap3(a, b); cout << "after swap (3):" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; system("pause"); return 0; }
结果为: ------------------------------ input a : 32 input b : 65 ------------------------------ a = 32 b = 65 ------------------------------ after swap (1): a = 65 b = 32 after swap (2): a = 32 b = 65 after swap (3): a = 65 b = 32 请按任意键继续. . .当然,第一个还是最好,因为它不担心a,b数字大时越界的问题。
// 环境:VS2005 // 用途:不用中间变量交换两个数 // 时间:2010.9.25 // 作者:http://pppboy.blog.163.com //-------------------------------- #include "stdafx.h" #include <iostream> using namespace std; //交换a,b void fSwap1(int& a, int& b) { a = a ^ b; b = a ^ b; a = a ^ b; } //交换a,b的另一种方法 void fSwap2(int& a, int& b) { a = a + b; b = a - b; a = a - b; } //再一种方法 void fSwap3(int& a, int& b) { a = a * b; b = a / b; a = a / b; } int main(int argc, char* argv[]) { int a, b; cout << "------------------------------" << endl; cout << "input a :" << endl; cin >> a; cout << "input b :" << endl; cin >> b; cout << "------------------------------" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "------------------------------" << endl; fSwap1(a, b); cout << "after swap (1):" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; fSwap2(a, b); cout << "after swap (2):" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; fSwap3(a, b); cout << "after swap (3):" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; system("pause"); return 0; }
结果为: ------------------------------ input a : 32 input b : 65 ------------------------------ a = 32 b = 65 ------------------------------ after swap (1): a = 65 b = 32 after swap (2): a = 32 b = 65 after swap (3): a = 65 b = 32 请按任意键继续. . .当然,第一个还是最好,因为它不担心a,b数字大时越界的问题。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int swapf(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
{
int temp = a;
a = b;
b = temp;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#define swab(a, b) (a ^= b ^= a ^= b)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |