英文:
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<=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 > 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 > 0){
count++;
}
return count;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论