不使用临时变量交换两个值
#include<conio.h>void main()
#include <conio.h> void main()
{
{
clrscr();
clrscr();
int a,b;
int a,b;
cout<<“Enter value of a:”;
cout <<“输入a的值:”;
cin>>a;
cin >> a;
cout<<“Enter value of b:”;
cout <<“输入b的值:”;
cin>>b;
cin >> b;
a=a+b; b=a-b; a=a-b;
a = a + b; b = ab; a = ab;
cout<<“na=”<<a; cout<<“nb=”<<b; getch(); }
cout <<“ na =” << a; cout <<“ nb =” << b; getch(); }
翻译自: https://www.thecrazyprogrammer.com/2011/12/c-program-to-swap-to-numbers-without.html
不使用临时变量交换两个值
本文介绍了一种在C++中不使用额外的临时变量来交换两个数值的方法。通过数学运算,实现两数的直接交换,展示了算法的巧妙性和效率。适合初学者理解和实践。

4209

被折叠的 条评论
为什么被折叠?



