Java 在1000位数字中找出五个连续数字的最大乘积
我想知道如何解决这个问题(这不是学校的事,我编程是一种爱好,我在做欧拉练习)。 我代码中的问题是:char c=grest.charAt(i); 我想知道我怎样才能在《最伟大的我》中看到下一个我。 我张贴我的代码,我提前感谢谁会帮助我Java 在1000位数字中找出五个连续数字的最大乘积,java,Java,我想知道如何解决这个问题(这不是学校的事,我编程是一种爱好,我在做欧拉练习)。 我代码中的问题是:char c=grest.charAt(i); 我想知道我怎样才能在《最伟大的我》中看到下一个我。 我张贴我的代码,我提前感谢谁会帮助我 // Find the greatest product of five consecutive digits in the 1000-digit number import java.util.*; import java.io.*; public clas
// Find the greatest product of five consecutive digits in the 1000-digit number
import java.util.*;
import java.io.*;
public class FIVE
{
public static void main(String args[])
{
try{
String greatest = "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long TheNUMBER = 0;
long n = 0;
long count = 0;
long provvisoryBiggest = 0;
long countZERO = 0;
long countBLOCK = 0;
long X = 0;
for ( int i=0; i < greatest.lenght(); i++)
{
for(int j=0; j<5; j++)
{
if(X == 0)
{
X = 1;
}
char c = greatest.charAt(i);
n = Character.getNumericValue(c);
count++;
System.out.println(count + "th number: " + n);
X = (X * n );
System.out.println("The product is: " + X);
while (provvisoryBiggest < X)
{
provvisoryBiggest = X;
}
while(count == 5)
{
System.out.println("Now the biggest one is: " + provvisoryBiggest);
count = countZERO;
X = countZERO;
countBLOCK++;
System.out.println("Block number: "+ countBLOCK);
System.out.println("------------------------------------");
}
}
}
}
catch(ArrayIndexOutOfBoundsException ex) { System.out.println("ARRAY ERROR"); }
catch(ArithmeticException ex) { System.out.println("MATH ERROR");}
catch(NumberFormatException ex) { System.out.println("NUMBER FORMAT EXCEPTION");}
catch(StringIndexOutOfBoundsException ex) { System.out.println("INDEX ERROR"); }
}}
//在1000位数字中找到五个连续数字的最大乘积
导入java.util.*;
导入java.io.*;
公共五班
{
公共静态void main(字符串参数[])
{
试一试{
字符串最大="73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
长编号=0;
长n=0;
长计数=0;
长PROVISORYMAX=0;
长countZERO=0;
长countBLOCK=0;
长X=0;
对于(int i=0;i 对于(int j=0;j查看程序的输出,我发现它有如下块:
1th number: 8
The product is: 8
2th number: 8
The product is: 64
3th number: 8
The product is: 512
4th number: 8
The product is: 4096
5th number: 8
The product is: 32768
Now the biggest one is: 59049
Block number: 684
这说明了对于五个数的子序列,实际上只是将其中一个数乘以五倍
您需要确保迭代该子序列以获得产品
问题似乎出在您的行grest.charAt(i)
中。您实际上需要添加偏移量j
,以获得五位数子序列中的每个数字。查看程序的输出,我发现它有如下块:
1th number: 8
The product is: 8
2th number: 8
The product is: 64
3th number: 8
The product is: 512
4th number: 8
The product is: 4096
5th number: 8
The product is: 32768
Now the biggest one is: 59049
Block number: 684
这说明了对于五个数的子序列,实际上只是将其中一个数乘以五倍
您需要确保迭代该子序列以获得产品
问题似乎出在您的行中。字符(i)
。您实际上需要添加偏移量j
,以获得五位数子序列中的每个数字。解决方案是:
public class Five {
public static void main(String args[]) {
try {
String greatest = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188379087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397 5369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248360082325753042075296345099879";
int n = 0;
int count = 0;
long biggest = 0;
int countBlock = 1;
int biggestBlock = 1;
long X = 1;
for (int i = 0; i < greatest.length()-4; i++) {
for (int k = i; k < i + 5; k++) {
char c = greatest.charAt(k);
n = Character.getNumericValue(c);
count++;
X = (X * n);
System.out.println("Product is:" + X);
}
if(biggest < X){
biggest = X;
biggestBlock = countBlock;
}
System.out.println("Now the biggest one is: "+ biggest);
count = 0;
X = 1;
countBlock++;
System.out.println("Block number: " + biggestBlock);
System.out.println("Block number: " + countBlock);
System.out.println("------------------------------------");
}
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ARRAY ERROR");
} catch (ArithmeticException ex) {
System.out.println("MATH ERROR");
} catch (NumberFormatException ex) {
System.out.println("NUMBER FORMAT EXCEPTION");
} catch (StringIndexOutOfBoundsException ex) {
System.out.println("INDEX ERROR");
}
}
}
公共五级{
公共静态void main(字符串参数[]){
试一试{
字符串最大值=”731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188379087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397 5369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248360082325753042075296345099879";
int n=0;
整数计数=0;
长最大值=0;
int countBlock=1;
int biggestBlock=1;
长X=1;
对于(int i=0;i
解决方案是:
public class Five {
public static void main(String args[]) {
try {
String greatest = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188379087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397 5369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248360082325753042075296345099879";
int n = 0;
int count = 0;
long biggest = 0;
int countBlock = 1;
int biggestBlock = 1;
long X = 1;
for (int i = 0; i < greatest.length()-4; i++) {
for (int k = i; k < i + 5; k++) {
char c = greatest.charAt(k);
n = Character.getNumericValue(c);
count++;
X = (X * n);
System.out.println("Product is:" + X);
}
if(biggest < X){
biggest = X;
biggestBlock = countBlock;
}
System.out.println("Now the biggest one is: "+ biggest);
count = 0;
X = 1;
countBlock++;
System.out.println("Block number: " + biggestBlock);
System.out.println("Block number: " + countBlock);
System.out.println("------------------------------------");
}
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ARRAY ERROR");
} catch (ArithmeticException ex) {
System.out.println("MATH ERROR");
} catch (NumberFormatException ex) {
System.out.println("NUMBER FORMAT EXCEPTION");
} catch (StringIndexOutOfBoundsException ex) {
System.out.println("INDEX ERROR");
}
}
}
公共五级{
公共静态void main(字符串参数[]){
试一试{
字符串最大值=”7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930