2014-05-08 21:33
原题:
largest number that an int variable can fit given a memory of certain size
题目:给定特定内存大小,请问int型的变量能表示的最大整数是多少?
解法:这个“Guy”出的题目总是这样表意不清。特定大小的内存是什么意思?他要说的是字长吧?16位int占两字节,32位以后int都占四字节。这样能表示的最大整数就是(1 << sizeof(int) * 8 - 1) - 1。
代码:
1 // http://www.careercup.com/question?id=4847954317803520 2 // For n bits, signed integer can reach 2 ^ (n - 1) - 1. 3 // For n bits, unsigned integer can reach 2 ^ n - 1. 4 // My question is: is this a real Google interview question? Onsite interview? 5 // This guy named 'guy' has been posting an awful lot of questions on Careercup, which contains some difficult, tricky, trivial and stupid ones. 6 // I doubt if he's simply trying to gather more reputation, by means of spam questions. If he's not telling the truth, he's polluting Careercup and misleading other visitors here. 7 // He should be warned and punished. 8 int main() 9 { 10 return 0; 11 }