繁体   English   中英

Java项目Euler#8在1000位数字中找到五个连续数字的最大乘积

[英]Project Euler #8 in Java Find the greatest product of five consecutive digits in the 1000-digit number

如果我运行下面的程序,它给出的输出为40824,这是绝对正确和预期的。

public class EulerProblem8 {

public static void main(String args[]) {
    String thousandNumbers = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    int limit = 5, count = 0, prod = 1, large = 0;
    for (int i = 0; i < thousandNumbers.length(); i++) {
        count++;
        if (count == limit) {
            count = 0; 
            if (prod > large) {
                large = prod;
            }
            prod = 1;
        }
          prod *= Integer.parseInt(thousandNumbers.charAt(i)+"");
    }
    System.out.println("Large no is :" + large);
 }
}

如果我按如下所示更改代码,则会得到不同的输出? 31752为什么?

public class EulerProblem8 {

public static void main(String args[]) {
    String thousandNumbers = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    int limit = 5, count = 0, prod = 1, large = 0;
    for (int i = 0; i < thousandNumbers.length(); i++) {
        count++;
        prod *= Integer.parseInt(thousandNumbers.charAt(i)+"");
         if (count == limit) {
            count = 0; 
            if (prod > large) {
                large = prod;
            }
            prod = 1;
        }
    }
    System.out.println("Large no is :" + large);
}

}

您不会考虑所有可能的5位数字,而是仅考虑从第4种开始的第5种数字,从第一种情况开始的第二种情况。 第二种情况以不同的方式分割字符串,因此给出了不同的答案。

注意:您可以更快地将字符转换为小数。

prod *= thousandNumbers.charAt(i) - '0';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM