错误:尝试返回arraylist时不兼容的类型

时间:2014-02-25 18:50:07

标签: java

我正在尝试解决Project Euler上的Problem 12问题。 我知道如何完成这个问题,但是我遇到了一个问题 错误。我已经搜索了如何使用不同问题的arraylist但是我 仍面临问题。

import java.util.ArrayList;

public class Level_12 {
/*
The sequence of triangle numbers is generated by adding the natural numbers. 
So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 

The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?
*/

public static ArrayList<Long> check(long num) {
    ArrayList<Long> divisors = new ArrayList<Long>();
    for (long o = 1; o <= Math.sqrt(num); o++)
     if (num % o == 0) {
         divisors.add(o);
         System.out.println(o + " is a current divisor of " + num);
     }
     for (Long m : divisors) {
     return m;
     }
} 

public static void main(String[] args) {
    long triangle = 0; //Triangle number
    long total = 500; //Total divisors
    long currenttotal = 0; //Amount of divisors
    long i = 0; //Just to itterate 

    while (currenttotal <= total) {
        if (check(i) > currenttotal) { //Finding if the 
            triangle = i;
            if (currenttotal == total) break;
        }
        i++;
    }
    System.out.println("The value of the first triangle number to have over 500 divisors is " + triangle);

  }
} 

修改

修正了最终代码为

import java.util.ArrayList;

public class Level_12 {
/*
The sequence of triangle numbers is generated by adding the natural numbers. 
So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 

The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?
*/

public static ArrayList<Long> check(long num) {
    ArrayList<Long> divisors = new ArrayList<Long>();
    for (long o = 1; o <= Math.sqrt(num); o++)
     if (num % o == 0) {
         divisors.add(o);
         System.out.println(o + " is a current divisor of " + num);
     }
     return divisors;
     }


public static void main(String[] args) {
    long triangle = 0; //Triangle number
    long total = 500; //Total divisors
    long currenttotal = 0; //Amount of divisors
    long i = 0; //Just to itterate 

    while (currenttotal <= total) {
        if (check(i).size() > currenttotal) { //Finding if the amount of divisors is larger than current divisors
            triangle = i;
            if (currenttotal == total) break;
        }
        i++;
}
    System.out.println("The value of the first triangle number to have over 500 divisors is " + triangle);

  }
}

4 个答案:

答案 0 :(得分:0)

您在Long方法中返回ArrayList<Long>而不是check()

答案 1 :(得分:0)

返回Long,而方法的返回类型为ArrayList<Long>。所以更新返回类型如下:

public static Long check(long num) {
    ArrayList<Long> divisors = new ArrayList<Long>();
    for (long o = 1; o <= Math.sqrt(num); o++)
     if (num % o == 0) {
         divisors.add(o);
         System.out.println(o + " is a current divisor of " + num);
     }
     for (Long m : divisors) {
     return m;
     }
} 

答案 2 :(得分:0)

罪魁祸首是:

  for (Long m : divisors) {
    return m;
  }

在此处返回Long值,而方法签名指定返回ArrayList

   public static **ArrayList<Long>** check(long num)

您需要更改方法签名或根据您要完成的内容返回适当的值。

答案 3 :(得分:0)

您只将m的一半除数放入列表中。如果o除以m,则m/o也会m除以。

/**
 * Return a list of the divisors of an integer.  If it is a perfect square, the square
 * root will be repeated. The list will not be in order.
 * @param num The number whose divisors to find.
 * @return A list of the number's positive divisors.
 */
public List<Long> divisors(long num) {
    final List<Long> divisors = new ArrayList<>();
    final long limit = Math.sqrt(num);
    for (long o = 1L; o <= limit; o++)
    if (num % o == 0) {
        divisors.add(o);
        divisors.add(num / o);
    }
    return divisors;
}

/**
 * Return the nth triangular number.
 * @param n The index of the desired triangular number.
 * @return The n-th triangular number 1 + 2 + ... + n
 */
public long triangle(long n) {
    return n * (n + 1) / 2;
}