交换价值 [英] SWAP VALUE

查看:71
本文介绍了交换价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

基本上我想要一个程序如何交换价值......我知道2方法1 ---

temp = a;

a = b;

b = temp;


2nd ----

a = a + b ;

b = ab;

a = ab;


如果你知道任何其他方法然后给我程序..

Pramod Sahgal

解决方案

Pramod写道:


大家好,

基本上我想要一个程序如何交换价值...我知道2方法1 ---

temp = a;

a = b;

b = temp;


2nd ----

a = a + b;

b = ab;

a = ab;


如果你知道任何其他方法然后给我程序..

Pramod Sahgal



std :: swap(a,b);


据推测它适用于任何类型a和b是。

在第二个你避免临时v ariable,但实际上是

如果temp的范围有限,它可能不存在于寄存器外的任何地方,所以你不会保存任何东西

秒。


第二个有重大问题。


如果a + b或ab有溢出会怎样?

有些人使用XOR做同样的事情,但XOR

对所有数字类型都不起作用。


< blockquote> 6月12日下午4:26,Ron Natalie< r ... @ spamcop.netwrote:


Pramod写道:


大家好,

基本上我想要一个程序如何交换价值...我知道2方法1 ---

temp = a;

a = b;

b = temp;


2nd ----

a = a + b;

b = ab;

a = ab;


如果你知道任何其他方法然后给我程序..

Pramod Sahgal



std :: swap(a,b);


据推测,它针对a和b类型进行了优化。

在第二个你避免一个临时变量,但实际上

如果temp有足够的范围,它可能不存在于寄存器外的任何地方,所以你不是通过

秒保存任何东西。


第二个有重大问题。


如果a + +会发生什么b或ab有溢出吗?

有些人用XOR做同样的事情,但是XOR

甚至对所有数字类型都不起作用。



哥们我想要另一种方法......如果你有帖子就交换价值。


< blockquote> Pramod写道:


6月12日下午4:26,Ron Natalie< r ... @ spamcop.netwrote:


>>
std :: swap(a,b);

据推测,它针对a和b类型进行了优化。在第二个中你避免使用一个临时变量,但实际上如果temp的范围有限,它可能不存在于寄存器外的任何地方,所以你不会通过
第二。

第二个有重大问题。

如果a + b或ab有溢出会怎样?
有些人用XOR做同样的事情,但XOR
对所有数字类型都不起作用。



哥们我想要另一个方法......如果你有帖子就交换价值。



Nice那里的男人(或加仑)...


那里有另一种方法。


-

[我的电子邮件中没有x']


我有权保持沉默

(并且应该尽可能多地使用它)

我打字的任何东西都可以用来对付我

在白痴法庭上

我有权利出错

(可能是我)

如果我不能提出自己的错误

我相信有人会为我提供它。

Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal

解决方案

Pramod wrote:

Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal

std::swap(a,b);

Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you''re not saving anything by the
second.

The second has SUBSTANTIAL problems.

What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn''t work on even all the numeric types.


On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:

Pramod wrote:

Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal


std::swap(a,b);

Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you''re not saving anything by the
second.

The second has SUBSTANTIAL problems.

What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn''t work on even all the numeric types.

buddy i want another method...to swap value if u have then post.


Pramod wrote:

On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:

>>
std::swap(a,b);

Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you''re not saving anything by the
second.

The second has SUBSTANTIAL problems.

What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn''t work on even all the numeric types.


buddy i want another method...to swap value if u have then post.


Nice manners there guy (or gal)...

And there was another method there.

--
[there are no x''s in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.


这篇关于交换价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆