在一行中排序3个数字。 [英] Sort 3 numbers in a single line.

查看:83
本文介绍了在一行中排序3个数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们有一个对3个整数进行排序的函数,是否可以在一个语句中对它进行排序并返回它们?


例如:


bool sort(int a,int b,int c){

return //这是代码所在的地方......;

}


是否可以不使用外部函数交换两个值?

解决方案



" Hasan Ammar" <是**** @ gmail.com> schrieb im Newsbeitrag

新闻:ci ******** @ odak26.prod.google.com ...

让我们假设我们有一个排序功能3整数,是否有可能在一个语句中对它们进行排序和返回?

示例:

bool sort(int a,int b, int c){
返回//这是代码所在的地方....;
}


na = max(a,max(b,c) ));

nc = min(a,min(b,c));

nb =(na == a)?(nc == c?b: c):( nc == c?b:a);

是否可以不使用外部函数来交换两个
值?



仅适用于整数:

#define swap(a,b){(a)=(b)^(a); (B)=(A)^(B); (a)=(b)^(a);}

-

-Gernot

int main(int argc,char ** argv){printf

("%silto%c%cf%cgl%ssic%ccom%c"," ma",58,''g'',64," ba" ,46,10);}


________________________________________

寻找好游戏?亲自动手吧!

GLBasic - 你可以这样做
www.GLBasic .com


>仅适用于整数:

#define swap(a,b){(a)=(b)^(a); (B)=(A)^(B); (a)=(b)^(a);}




圣洁吸烟蝙蝠侠!


Hasan Ammar写道:

让我们假设我们有一个对3个整数进行排序的函数,是否可以在一个语句中对它们进行排序和返回?

例如:

bool sort(int a,int b,int c){
返回//这是代码的位置......;
}


我想你需要多考虑一下你想做什么..


一开始,你的函数返回bool。你不能通过bool返回3

排序整数的列表。


你想要像void sort(int& a,int)这样的东西吗? & b,int& c){..},其中

sort(x,y,z)将改变x,y和z的实际值,或者你想要

更像vector< int> sort(int a,int b,int c),你在哪里
返回一个变量排序的向量?


是否可以不使用外部函数交换两个值?




您是否想出于任何特殊原因这样做?不要为了效率原因而烦恼,因为如果交换功能足够小,任何体面的编译器都会优化对

交换函数的调用。


Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

is it possible without using an external function to swap two values?

解决方案


"Hasan Ammar" <am****@gmail.com> schrieb im Newsbeitrag
news:ci********@odak26.prod.google.com...

Lets suppose we have a function to sort 3 integers, is it possible
to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}
na = max(a, max(b,c));
nc = min(a, min(b,c));
nb = (na==a)?(nc==c ? b : c) : (nc==c ? b : a);
is it possible without using an external function to swap two
values?



for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);}
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com


> for integers only:

#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);}



Holy smokes Batman!


Hasan Ammar wrote:

Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}
I think you need to think a bit more about what you want to do..

for a start, your function returns bool. You can''t return a list of 3
sorted integers via a bool.

Do you want something like void sort(int &a,int &b,int &c) {..}, where
sort(x,y,z) will change the actual values of x,y and z, or do you want
something more like vector<int> sort(int a,int b,int c), where you
return a vector with the variables sorted?


is it possible without using an external function to swap two values?



Do you want to do this for any particular reason? Don''t bother for
efficency reasons, as any decent compiler will optimise away a call to a
swapping function if the swapping function is sufficently small.


这篇关于在一行中排序3个数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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