A hard problem! Get maximum one of two natural numbers.
Problem:
Write a function which accepts two natural numbers (unsigned int) and return the maximum one.
Requirement:
In the function body, You may not use any comparing operation, such as <, >, ?:, if, switch, etc. In other words, Only these operators of {+, -, *, /} can be used, any other operator or function call is invalid!
Who is interested in this and can solve it?
unsigned int Max(unsigned int a, unsigned int b)
{
// Add your code here!
}
[Edited by - xmllmx on February 12, 2006 8:49:49 PM]
unsigned int Max(unsigned int a, unsigned int b){ int max = 0xFFFFFFFFU; vector<vector<unsigned int> > foo(max, vector<unsigned int>(max,0)); for(unsigned int i=0;i<=max;i++) { for(unsigned int j=i+1;j<=max;j++) foo[j] = b; for(unsigned int j=0;j<i;j++) foo[j] = a; foo = a; } return foo[a];}
And since I used a lookup table, it'll be faster than any arithmetic evaluations!
The solution upstairs cannot be accepted!
1, It cannot get the correct result;
2, It is too time-consuming!
3, Any Iteration Statement(while, do-while, for, and the like) is not permitted.
1, It cannot get the correct result;
2, It is too time-consuming!
3, Any Iteration Statement(while, do-while, for, and the like) is not permitted.
Quote:Original post by sjelkjd
int max = 0xFFFFFFFFU;
vector<vector<unsigned int> > foo(max, vector<unsigned int>(max,0));
And wouldn't these two lines request 2^32 * 2^32 * 4 bytes = 73 million TB of data?
Well I'm stumped.
The answer's going to be really simple, and we'll all kick ourselves for not spotting it first.
You're an evil person, xmllmx: this thing's going to keep me at it for a while.....
The answer's going to be really simple, and we'll all kick ourselves for not spotting it first.
You're an evil person, xmllmx: this thing's going to keep me at it for a while.....
Oh, do things like std::sort fall in the comparison category? If not, my solution:
unsigned int maxTheRoughWay( unsigned int a, unsigned int b ) { std::vector<unsigned int> numbers; numbers.push_back(a); numbers.push_back(b); std::sort( numbers.begin(), numbers.end() ); return numbers[1];}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement