我如何解决这个问题?我认为这与return语句有关。

huangapple go评论133阅读模式
英文:

How do I resolve this issue ?? I think it has something to do with the return statement

问题

import java.util.Scanner;

public class theatre_square {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
        theatre_square ob = new theatre_square();
        long x = ob.cal(m, s), y = ob.cal(n, s);
        System.out.println(x * y);
    }

    long cal(long a, long b) {
        if (a <= 0)
            return 0;
        else
            return (1 + cal(a - b, b));
    }
}
> 输入:1000000000 1000000000 1
> 
> 错误信息:
> 
> Exception in thread "main" java.lang.StackOverflowError
> 	at theatre_square.cal(theatre_square.java:17)
> 	at theatre_square.cal(theatre_square.java:17)
> 	at theatre_square.cal(theatre_square.java:17)
> 	... 以此类推 ...
> 
> 我认为问题与递归函数内的返回语句有关。
英文:
import java.util.Scanner;
public class theatre_square
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
        theatre_square ob = new theatre_square();
        long x = ob.cal(m,s), y = ob.cal(n,s);
        System.out.println(x*y);
    }
    long cal(long a, long b)
    {
        if(a&lt;=0)
            return(0);
        else
            return (1+cal(a-b,b));
    }
}

> Input: 1000000000 1000000000 1
>
> Error:
>
> Exception in thread "main" java.lang.StackOverflowError at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17) at
> theatre_square.cal(theatre_square.java:17)....... and so on.
>
>
>
> I think it has to do something with the return statement inside in the
> recursion function.

答案1

得分: 0

我认为你可以在不使用递归和for循环的情况下解决这个问题

import java.util.Scanner;
public class theatre_square
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
        theatre_square ob = new theatre_square();
        long x = ob.cal(m,s), y = ob.cal(n,s);
        System.out.println(x*y);
    }
    
    
    long cal(long m, long a)    {
        long count = m / a;
        long rem = m % a;
        if(rem  &gt; 0){
            count++;
        }
        return count;
    }
}
英文:

i think you can solve the problem without recursion and for loop.

import java.util.Scanner;
public class theatre_square
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
        theatre_square ob = new theatre_square();
        long x = ob.cal(m,s), y = ob.cal(n,s);
        System.out.println(x*y);
    }
    
    
    long cal(long m, long a)    {
        long count = m / a;
        long rem = m % a;
        if(rem  &gt; 0){
            count++;
        }
        return count;
    }
}

huangapple
  • 本文由 发表于 2020年9月20日 19:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63978693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定