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

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

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

问题

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

> 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

  1. 我认为你可以在不使用递归和for循环的情况下解决这个问题
  2. import java.util.Scanner;
  3. public class theatre_square
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner in = new Scanner(System.in);
  8. long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
  9. theatre_square ob = new theatre_square();
  10. long x = ob.cal(m,s), y = ob.cal(n,s);
  11. System.out.println(x*y);
  12. }
  13. long cal(long m, long a) {
  14. long count = m / a;
  15. long rem = m % a;
  16. if(rem &gt; 0){
  17. count++;
  18. }
  19. return count;
  20. }
  21. }
英文:

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

  1. import java.util.Scanner;
  2. public class theatre_square
  3. {
  4. public static void main(String args[])
  5. {
  6. Scanner in = new Scanner(System.in);
  7. long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
  8. theatre_square ob = new theatre_square();
  9. long x = ob.cal(m,s), y = ob.cal(n,s);
  10. System.out.println(x*y);
  11. }
  12. long cal(long m, long a) {
  13. long count = m / a;
  14. long rem = m % a;
  15. if(rem &gt; 0){
  16. count++;
  17. }
  18. return count;
  19. }
  20. }

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:

确定