Java 无法获得沙漏算法的精确结果
以下是沙漏问题定义的链接: 我编写了以下程序:Java 无法获得沙漏算法的精确结果,java,arrays,algorithm,array-algorithms,Java,Arrays,Algorithm,Array Algorithms,以下是沙漏问题定义的链接: 我编写了以下程序: package day11; import java.util.Scanner; public class Solution { public static void main(String ... args){ Scanner scan = new Scanner(System.in); int[][] arr = new int[6][6]; int maxHourGlass
package day11;
import java.util.Scanner;
public class Solution {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int[][] arr = new int[6][6];
int maxHourGlassValue = 0;
int temp = 0;
int currMax = 0;
int k = 0, l = 0;
for(int i = 0 ; i < 6 ; i++){
for(int j = 0 ; j < 6 ; j++){
arr[i][j] = scan.nextInt();
}
}
for(int i = 1 ; i < 5 ; i++){
for(int j = 1 ; j < 5 ; j++){
if(maxHourGlassValue < currMax){
maxHourGlassValue = currMax;
}
}
}
System.out.println(maxHourGlassValue);
}
}
packageday11;
导入java.util.Scanner;
公共类解决方案{
公共静态void main(字符串…参数){
扫描仪扫描=新扫描仪(System.in);
int[]arr=新int[6][6];
int maxHourGlassValue=0;
内部温度=0;
int currMax=0;
int k=0,l=0;
对于(int i=0;i<6;i++){
对于(int j=0;j<6;j++){
arr[i][j]=scan.nextInt();
}
}
对于(int i=1;i<5;i++){
对于(int j=1;j<5;j++){
如果(maxHourGlassValue
在给定的8个测试用例中,我只能运行6个。有什么可能出错???试试这段代码,我没有写那段代码,我只是复制它并从中修改它
import java.util.Scanner;
公开课考试{
公共静态void main(字符串…参数){
扫描仪扫描=新扫描仪(System.in);
int size=6;
int[]m=新的int[size][size];
//数字输入
for(inti=0;i下面是将在的所有测试用例中成功运行的代码
publicstaticvoidmain(字符串[]args){
尝试(扫描仪扫描=新扫描仪(System.in)){
int[]arr=新int[6][6];
int maxHourGlassValue=-63;//赋值(-9*7=)-63,这是“沙漏和”的最小可能值。
//读取输入。
对于(int i=0;i<6;i++){
对于(int j=0;j<6;j++){
arr[i][j]=scan.nextInt();
}
}
//逻辑。
/**
*i和j的索引将从1运行到4(一个小于n-1,其中n=6)
*因此,对于每个i和j迭代,计算沙漏的和。
*/
int iHGValueTemp=0;
对于(int i=1;i<5;i++){
对于(int j=1;j<5;j++){
iHGValueTemp=arr[i][j]+/*主元素*/
arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1]+/*主要元素的前三位元素*/
arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1];/*主要元素的底部三个元素*/
如果(iHGValueTemp>maxHourGlassValue){
maxHourGlassValue=iHGValueTemp;
}
}
}
//输出。
System.out.println(maxHourGlassValue);
}
}
我只在评论中用代码写了描述。如果有任何疑问,请参考这一点,并与我讨论。可能会出什么问题?很多人一直在编码错误。这就是为什么调试是整个学术研究领域的原因。到目前为止,您还没有编写任何逻辑。它甚至没有运行示例测试用例,那么它将如何运行8分之6的un。无论如何,我已经为你的沙漏问题回答了一个完整的测试代码。如果你也能提供一些描述,那就更好了。你可以将temp>MaxSum
条件而不是temp>=MaxSum
放在`if`内for
循环中。
import java.util.Scanner;
public class Test {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int size = 6;
int[][] m = new int[size][size];
//numbers input
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
m[i][j] = scan.nextInt();
}
}
int temp = 0, MaxSum = -99999;
for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
if (j+2 < size && i+2 < size) {
temp = m[i][j] + m[i][j+1] + m[i][j+2] + m[i+1][j+1] + m[i+2][j] + m[i+2][j+1] + m[i+2][j+2];
if (temp >= MaxSum) {
MaxSum = temp;
}
}
}
}
System.out.println(MaxSum);
}
}
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int[][] arr = new int[6][6];
int maxHourGlassValue = -63;//Assigning (-9*7=)-63 which is the minimum possible value of "hourglass sum".
//Reading inputs.
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = scan.nextInt();
}
}
//Logic.
/**
* Index of both i and j will run from 1 to 4 (one less than n-1 where n = 6)
* So for each i and j iteration calculating the sum of hourglass.
*/
int iHGValueTemp = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
iHGValueTemp = arr[i][j] + /*Main element*/
arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1]+ /*Top three elements of main element.*/
arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1]; /*Bottom three elements of main element.*/
if (iHGValueTemp > maxHourGlassValue) {
maxHourGlassValue = iHGValueTemp;
}
}
}
//Output.
System.out.println(maxHourGlassValue);
}
}