首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArithmeticException除以零

ArithmeticException除以零
EN

Stack Overflow用户
提问于 2013-07-26 17:55:43
回答 1查看 450关注 0票数 0

我正在尝试回答Project Euler的第53个问题。我采用了一种相当暴力的方法,但在我看来,我的逻辑应该给出正确的答案,即使这需要awhile.So,在这种情况下,我做错了什么,除了效率相当低之外,这使得编译器返回的结果是ArithmeticException除以0。

代码语言:javascript
复制
import java.util.*;

public class problem53 
{
  public static int fact(int x)
  {
    int total = 0;
    if(x != 0)
    {
      for(int i=(x-1);i>0;i--)
      {
        x = x*i;
        total = x;
      }
    }
    if(x==0)
      total = 1;
    return total;
  }

  public static int combo(int y,int z)
  {
      int end = 0;
      if(y==0)
        y=2;
      if(z==0)
        z=1;
      if(y-z != 0)
      {
        end = fact(y)/(fact(z)*(fact(y-z)));
      }
      return end;
 }

public static void main(String[]args)
{
  int answer = 0;
  List<Integer> sure = new ArrayList<Integer>();
  for(int i=20;i<=100;i++)
  {
    for(int j=2;j<i;j++)
    {
      int ferNow = combo(i,j);
      if(ferNow>=1000000)
        sure.add(ferNow);
    }
  }
  answer = sure.size();
  System.out.println(answer);
  }
}

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2013-07-26 18:00:52

如果z =1或y-z =1,您将收到此错误

因为事实(1)返回0

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17878310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档