Valentin
Great question! Thoroughly enjoyed thinking about this one.
sincerekamal
sure it would b wrong...but i tried something...
int res=(a-b)>0?a:b;
it too doesn't have if()
Aakash
That's a good point, but I think the idea is to avoid conditionals altogether. Your compiler turns the ternary ( ? : ) operator into the same code that it turns an if statement into (i.e. a conditional branch with two blocks).
Wyverald
You didn't say you could use a 2 in there.
So you'll need to make one with (a/a + b/b) or something like that.
Ravi
That's a good point. More verbosely, {a+b+abs(a-b)} / [{a+b+abs(a-b)}+{a+b+abs(a-b)}]
MoralMachine
Good point. Nod. But we have to exclude 0.
Whatever, the question should just emphasize on "pure arithmetic" or ask for an algebra expression.
Valentin
Or: {a + b + abs(a-b)}/(a/a + a/a). Slightly shorter version.
Valentin
This, however, of course wouldn't work if a was 0. So perhaps the parent's reply is actually more general.
Valentin
Jeez, I'm going in circles here. ravi's reply is incorrect. That whole expression simply equals 1/2. As long as a and b are not both equal to 0, the following would suffice: {a + b + abs(a-b)}/{(a + a + b + b)/(a+b)}
Aakash
Agreed, unless both a and b are zero...
Aashka Kotecha
this won't hold true if a and b both are odd
Jay Elliott
Isn't that just 1/2?
Oliver Matthews
Thinking about this, I'm fairly sure it is impossible unless you do one of the following: 1. allow numbers in the algorithm (as per solution) 2. assume at least one of the values is not 0. (as per below) 3. allow ^ 4. use a system that defines 0/0 as 1 (i.e. is wrong).
the last two would allow you to implicitly get a 1 by doing (a^a)/(a^a) or a/a respectively (and thus a 2 through addition).
jijiyangyu
=(ABS(A1-B1)+A1+B1)/2
Grant Goodman
Here's an intuitive derivation of the solution:
(a+b)/2 gives you the average, which is directly in between a and b, and it is 1/2 the difference from either a or b. So to get to the larger one, just add the difference of the two divided by two, giving you (a+b)/2 + abs(a-b)/2, or (a+b+abs(a-b))/2
Ravi
That's an awesome explanation. MoralMachine, I suggest adding it to the puzzle solution.
Eduard Nicodei
Wohooo touring-completeness achieved once again! Time to play with this in Haskell.
Anurag Poddar
max(a,b) = ((a+b)+|a-b|)/2
Martyn Shutt
This is the solution I came up with in C++;
int largerNum(int x, int y){
int pt1 = (x - y);
int pt2 = (pt1 - abs(pt1)) / (x/x + y/y); // 2
int pt3 = abs(pt2);
return pt3 + x;
}
I decided it would be easier for me to get to the solution if I broke it up into individual parts. But working from that, as a single expression;
return (abs((x - y - abs(pt1)) / (x/x + y/y))) + x;
Martyn Shutt
But as highlighted in other posts, it doesn't work if either number is zero. It actually crashes my compiled application. Probably due to dividing by zero.
Martyn Shutt
Correction; as a single return statement it should be
return (abs((x - y - abs(x - y)) / (x/x + y/y))) + x;
I accidentally left variable pt1 in there.
วาเลนติน
ดีค่ะ สนุกกับการคิดเกี่ยวกับเรื่องนี้อย่างใดอย่างหนึ่ง .
sincerekamal ว่า b จะผิด . . . . . . . แต่ผมลองทำ . . . . . . .
= int RES ( A-B ) > 0 ? A : B ;
มันไม่มี if()
นั่นดี Aakash จุด แต่ผมมีไอเดียที่จะหลีกเลี่ยงเงื่อนไขทั้งหมด คอมไพเลอร์ของคุณกลายเป็นไตรภาค ( ? : ) ผู้ประกอบการในรหัสเดียวกันนั้นมันกลายเป็นถ้างบลงไป ( เช่นสาขาเงื่อนไขสองบล็อก ) .
wyverald คุณไม่ได้บอกว่าคุณสามารถใช้ 2 ในนั้น
ดังนั้นคุณจะต้องให้กับ ( A / B / B ) หรือ อะไรทำนองนั้น
นั่นดีราวีจุด มากขึ้น ซึ่งใช้ถ้อยคำมากเกินไป { B ABS ( A-B ) } / [ { B } { ABS ( A-B ) / ABS ( A-B ) } ]
ดี moralmachine จุด พยักหน้า แต่เราต้องไม่รวม 0 .
อะไรก็ตามคำถามที่ควรเน้น เรื่อง " คณิตศาสตร์บริสุทธิ์ " หรือสอบถามนิพจน์พีชคณิต .
หรือวาเลนติน : { B ABS ( A-B ) } / ( / / ) รุ่นที่สั้นกว่าเล็กน้อย
วาเลนไทน์นี้ อย่างไรก็ตาม แน่นอน ไม่ทำงาน ถ้าเป็น 0 ดังนั้น บางทีพ่อแม่ตอบเป็นทั่วไปมากขึ้น
แหม วาเลนติน ผมเดินวนที่นี่ ราวี่ตอบกลับของไม่ถูกต้อง ทั้งการแสดงออก ก็เท่ากับ 1 / 2ตราบใดที่ทั้ง A และ B ไม่เท่ากับ 0 , ต่อไปนี้จะพอเพียง : { B ABS ( A-B ) } / { ( B ) B / ( b ) }
เห็นด้วย Aakash , นอกจากทั้ง A และ B เป็นศูนย์ . . . . . . .
aashka kotecha นี้ วอน ไม่ถือจริง ถ้า A และ B เป็นทั้งแปลก
เจเอลเลียต
ไม่ที่เพียง 1 / 2
คิดถึงโอลิเวอร์ แมทธิว ผมค่อนข้างมั่นใจว่ามันเป็นไปไม่ได้จนกว่าคุณจะเป็นหนึ่งในต่อไปนี้ : 1 .ให้ตัวเลขในขั้นตอนวิธี ( ตามโซลูชั่น ) 2 . ถือว่าอย่างน้อยหนึ่งค่าเป็น 0 ( ตามด้านล่าง ) 3 . ให้
4 ใช้ระบบที่กำหนด 0 / 0 เป็น 1 ( คือผิด )
ช่วงสองจะช่วยให้คุณได้รับ 1 โดยปริยายทำ (
) / (
) หรือ / ตามลำดับ ( และดังนั้นจึง 2 ผ่านนอกจาก )
= ( ABS jijiyangyu ( a1-b1 ) A1 B1 ) / 2
ให้กู๊ดแมนที่นี่เป็นแหล่งที่มาได้ง่ายของการแก้ปัญหา :
( b ) / 2 ให้คุณเฉลี่ย ซึ่งตรงระหว่าง A และ B , และมันเป็น 1 / 2 แตกต่างจากอย่างใดอย่างหนึ่ง หรือ B . ดังนั้นจะได้รับมีขนาดใหญ่ เพียงแค่เพิ่มความแตกต่างของทั้งสองหารสอง , ให้ คุณ ( B ) / ABS ( A-B ) 2 หรือ ( บีเอบี ( AB ) / 2
นั่นน่ากลัวราวี่อธิบาย moralmachine ผมแนะนำให้เพิ่มให้
ปริศนาแก้
wohooo เอดูอาร์ด nicodei ท่องเที่ยวสมบูรณ์ได้อีกครั้ง เวลาเล่นกับนี้ใน c .
anurag poddar แม็กซ์ ( a , b ) = ( ( B ) | A-B | ) / 2
ด๊อกเตอร์ชัต
นี้เป็นโซลูชั่นที่ฉันมากับ C ;
largernum int ( int x , int y ) {
! PT1 = ( x - y ) = ;
1 ชนิด ( PT1 - ABS ( PT1 ) ) / ( X / X Y / y ) ; / / 2
1 PT3 = ABS ( ชนิด ) ;
กลับ PT3 x ;
}
ฉันตัดสินใจจะง่ายสำหรับผมที่จะได้รับการแก้ไข ถ้ามันแตกขึ้นเป็นส่วนบุคคล แต่การทำงานจากที่สีหน้าเดียว ;
กลับ ( ABS ( X - Y - ABS ( PT1 ) ) / ( X / X Y / Y ) X ;
แต่ด๊อกเตอร์ชัตเป็นไฮไลต์ในโพสต์อื่น ๆ มันคงไม่เวิร์คถ้าหมายเลขศูนย์ มันเกิดปัญหาการรวบรวมของฉัน อาจจะเนื่องจากการหารด้วยศูนย์
ด๊อกเตอร์ ชัทแก้ไข อย่างเดียวคืนงบควร
กลับ ( ABS ( X - Y - ABS ( x - y ) ) / ( X / X Y / Y ) X ;
ผมลืมตัวแปร PT1 ในนั้น
การแปล กรุณารอสักครู่..
