我总是记不住电话号码。我需要一个记忆规则。


当前回答

我能想到的最正确的答案是Int32.MaxValue。

其他回答

最简单的方法是查看std::numeric_limits< int >::max()

例如(来自MSDN),

// numeric_limits_max.cpp

#include <iostream>
#include <limits>

using namespace std;

int main() {
   cout << "The maximum value for type float is:  "
        << numeric_limits<float>::max( )
        << endl;
   cout << "The maximum value for type double is:  "
        << numeric_limits<double>::max( )
        << endl;
   cout << "The maximum value for type int is:  "
        << numeric_limits<int>::max( )
        << endl;
   cout << "The maximum value for type short int is:  "
        << numeric_limits<short int>::max( )
        << endl;
}

大约是2.1 * 10^9。不需要知道确切的2^{31}- 1 = 2,147,483,647。

C

你可以在C语言中找到它:

#include <stdio.h>
#include <limits.h>

main() {
    printf("max int:\t\t%i\n", INT_MAX);
    printf("max unsigned int:\t%u\n", UINT_MAX);
}

给出(好吧,没有,)

max int:          2,147,483,647
max unsigned int: 4,294,967,295

C + 11 +

std::cout << std::numeric_limits<int>::max() << "\n";
std::cout << std::numeric_limits<unsigned int>::max() << "\n";

Java

你也可以用Java得到这个:

System.out.println(Integer.MAX_VALUE);

但是请记住,Java整数总是有符号的。

Python 2

Python有任意的精确整数。但在python2中,它们被映射为C整数。所以你可以这样做:

import sys
sys.maxint
>>> 2147483647
sys.maxint + 1
>>> 2147483648L

所以当整数大于2^31 -1时,Python会切换为long

首先写出两次47(你喜欢47号特工,对吧?),保持如图所示的空格(每个破折号是一个数字的槽位。先2个,然后4个)

--47----47

认为你手里有12个(因为12 =一打)。将其乘以4,47号特工号的第一位数字,即47,并将结果放在你已经拥有的第一对的右边

12 * 4 = 48
--4748--47 <-- after placing 48 to the right of first 47

然后将12乘以3(为了得到47号特工的数字的第二个数字,即7,你需要7 - 4 = 3),并将结果放在前两对的右边,即最后一个对槽

12 * 3 = 36
--47483647 <-- after placing 36 to the right of first two pairs

最后,从最右边的数字(本例中为2)开始,一个接一个地从您的手中拖动数字,并将它们放在您获得的第一个空槽中

2-47483647 <-- after placing 2
2147483647 <-- after placing 1

你知道了!对于负极限,你可以认为它的绝对值比正极限大1。

练习几次,你就会掌握窍门的!

这就是我记住2147483647的方法

在遥远的大草原上,擎天柱三人对着四十个七分咒

2 - To
1 - A
4 - Far
7 - Savannah
4 - Quarter
8 - Optimus
3 - Trio
6 - Hexed
4 - Forty
7 - Septenary

使用Java 9的REPL, jshell:

$ jshell
|  Welcome to JShell -- Version 9-Debian

jshell> System.out.println(Integer.MAX_VALUE)
2147483647