交换算法

root 站长 2019-04-16 18:00:23 35

方法

交换a和b的值,先使用临时变量t来保存a,接下来用a保存b, 最后用b保存t。

代码如下

int t = a;
a = b;
b = t;
{{ vote && vote.total.up }}

共 10 条回复

kj123

...... int a,b; cin>>a>>b; cout<<b<<" "<<a; 交换输出位置不就行了吗? 这么麻烦干神马?

yuzite

建议禁用swap

root 站长

是的!

tctm47

是随机输入吗

tctm47
qi20100912
Xionghaoyu straight a student

OK

Xionghaoyu straight a student
dinosaurgy

#include using namespace std; int main(void){ int a,b; cin>>a>>b; cout<<b" "<<a; return 0; }

gaojingzhe

#include #include using namespace std; int main(){ int a,b,c; cin>>a>>b; c=a; a=b; b=c; cout<<a<<" "<<b; return 0; }