当返回值大于long时,如何从方法返回long?

时间:2016-09-15 08:43:44

标签: java

我正在尝试解决Topcoder中的问题,我需要在提供宽度和长度时找到矩形的数量(不包括正方形)。我的代码适用于所有测试用例,但是对于一个最终测试用例,即592X964我需要返回81508708664,其大于2^32-1,这大于长值可以hold,但编译器要求必须在long中返回该值。我在下面添加了问题陈述和我的代码,请仔细阅读,并告诉我是否可以在java中使用long返回上述值。

    /*
Problem Statement
    
Given the width and height of a rectangular grid, return the total number of rectangles (NOT counting squares) that can be found on this grid.
For example, width = 3, height = 3 (see diagram below):
 __ __ __
|__|__|__|
|__|__|__|
|__|__|__|
In this grid, there are 4 2x3 rectangles, 6 1x3 rectangles and 12 1x2 rectangles. Thus there is a total of 4 + 6 + 12 = 22 rectangles. Note we don't count 1x1, 2x2 and 3x3 rectangles because they are squares.
Definition
    
Class:
RectangularGrid
Method:
countRectangles
Parameters:
int, int
Returns:
long
Method signature:
long countRectangles(int width, int height)
(be sure your method is public)
Limits
    
Time limit (s):
2.000
Memory limit (MB):
64
Notes
-
rectangles with equals sides (squares) should not be counted.
Constraints
-
width and height will be between 1 and 1000 inclusive.
Examples
0)

    
3
3
Returns: 22
See above
1)

    
5
2
Returns: 31
 __ __ __ __ __
|__|__|__|__|__|
|__|__|__|__|__|
In this grid, there is one 2x5 rectangle, 2 2x4 rectangles, 2 1x5 rectangles, 3 2x3 rectangles, 4 1x4 rectangles, 6 1x3 rectangles and 13 1x2 rectangles. Thus there is a total of 1 + 2 + 2 + 3 + 4 + 6 + 13 = 31 rectangles.
2)

    
10
10
Returns: 2640

3)

    
1
1
Returns: 0

4)

    
592
964
Returns: 81508708664

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
*/
public class RectangularGrid
{
    public static void main(String[] args)
    {
        System.out.println(countRectangles(592,964));
    }

    public static long countRectangles(int m, int n)
    {
        long tot_rect = (long)(((m*m)+m)*((n*n)+n))/4;
        long tot_square = 0;
        boolean status = true;

        while(status)
        {
            if(m>0 && n>0)
            {
                tot_square+=(long)m*n;
                --m;
                --n;
            }
            else
            {
                status = false;
            }

        }


        return tot_rect-tot_square;
    }
}

2 个答案:

答案 0 :(得分:5)

  

运行此代码会返回垃圾值,我无法理解为什么如果值可以保持很长时间

因为您正在进行int乘法运算:

public static long countRectangles(int m, int n)
// Note ---------------------------^^^----^^^
{
    long tot_rect = (long)(((m*m)+m)*((n*n)+n))/4;
    // Note -----------------^^^-------^^^

如果您希望这些人使用long,您需要演员:

public static long countRectangles(int m, int n)
{
    long tot_rect = ((((long)m*m)+m)*(((long)n*n)+n))/4;
    // Note -----------^^^^^^----------^^^^^^

投出最终结果并不能为中间结果中的损失做任何事情。

答案 1 :(得分:-1)

长是8个字节,这意味着它的范围是-2 ^ 63到2 ^ 63 - 1;所以它远远大于2 ^ 32-1。整数类型为4个字节,范围为-2 ^ 31到2 ^ 31-1。因此,只要你在范围内,你就完全没问题了。