为什么这段Java代码计算正确,但输出的答案却是错误的?

huangapple go评论75阅读模式
英文:

Why this Java code counts correct but prints the wrong answer?

问题

以下是您提供的Java代码的翻译部分:

我已经创建了一个小型的Java程序用来检查给定的程序是否包含和以下是我的代码

import java.util.Scanner;

public class SubarraySum{

  static void find_position(Long[] array, int size, Long find_sum){
    Long sum = 0L;
    boolean b = false;
    int c=0,d=0;
    for(int i=0;i<size; i++){
      if(sum == find_sum){
        System.out.println((c+1)+" "+(d+1));
        b = true;
        break;
      }
      sum = 0L;
      for(int j=i;j<size; j++){
        sum += array[j];
        System.out.println(i+": "+sum+" : "+b);
        if(sum == find_sum){
          c = i; d = j;
          // System.out.println((c+1)+" "+(d+1));
          System.out.println((c+1)+" "+(d+1));
          b = true;
          // System.out.println(i+": "+sum+" : "+b);
          // System.exit(0);
          break;
        }
        // System.out.println(i+": "+sum+" : "+b);
        if(sum > find_sum) break;
      }
    }
    if(b == false){
      System.out.println("-1");
    }
  }

  static void takeSizeandSum(Scanner sc){
    int size = sc.nextInt();
    Long find_sum = sc.nextLong();
    Long[] array = new Long[size];
    for(int i=0;i<size;i++){
        array[i] = sc.nextLong();
    }
    find_position(array, size, find_sum);
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int TestCase = sc.nextInt();
    for(int i=0;i<TestCase;i++){
      takeSizeandSum(sc);
    }
  }
}

它在这种情况下打印出错误的输出

1
42 468
135 101 170 125 79 159 163 65 106 146 82 28 162 92 196 143 28 37 192 5 103 154 93 183 22 117 119 96 48 127 172 139 70 113 68 100 36 95 104 12 123 134

对于这个输入它打印出-1”,而正确的答案是38 42

我已经更改了我的find_position代码

static int find_position(int[] array, int size, int find_sum){
    int i, j;
    int sum;
    for(i =0;i<size; i++){
      sum = array[i];
      for(j=i+1;j<size;j++){
        if(sum == find_sum){
          int c = j-1;
          System.out.println(i +" "+c);
          return 1;
        }
        if(sum > find_sum || j==size){
          break;
        }
        sum += array[j];
      }
    }
    System.out.println("-1");
    return 0;
}

然而它仍然显示错误的输出您能帮我解决吗
英文:

I have created one small Java program to check that given programs contains the sum or not. Here is my code:

import java.util.Scanner;
public class SubarraySum{
static void find_position(Long[] array, int size, Long find_sum){
Long sum = 0L;
boolean b = false;
int c=0,d=0;
for(int i=0;i&lt;size; i++){
if(sum == find_sum){
System.out.println((c+1)+&quot; &quot;+(d+1));
b = true;
break;
}
sum = 0L;
for(int j=i;j&lt;size; j++){
sum += array[j];
System.out.println(i+&quot; : &quot;+sum+&quot; : &quot;+b);
if(sum == find_sum){
c = i; d = j;
// System.out.println((c+1)+&quot; &quot;+(d+1));
System.out.println((c+1)+&quot; &quot;+(d+1));
b = true;
// System.out.println(i+&quot; : &quot;+sum+&quot; : &quot;+b);
// System.exit(0);
break;
}
// System.out.println(i+&quot; : &quot;+sum+&quot; : &quot;+b);
if(sum &gt; find_sum) break;
}
}
if(b == false){
System.out.println(&quot;-1&quot;);
}
}
static void takeSizeandSum(Scanner sc){
int size = sc.nextInt();
Long find_sum = sc.nextLong();
Long[] array = new Long[size];
for(int i=0;i&lt;size;i++){
array[i] = sc.nextLong();
}
find_position(array, size, find_sum);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int TestCase = sc.nextInt();
for(int i=0;i&lt;TestCase;i++){
takeSizeandSum(sc);
}
}
}

It prints wrong output in this case:

1
42 468
135 101 170 125 79 159 163 65 106 146 82 28 162 92 196 143 28 37 192 5 103 154 93 183 22 117 119 96 48 127 172 139 70 113 68 100 36 95 104 12 123 134

For this input it prints -1 while correct answer is 38 42!

I have changed my find_position code :

static int find_position(int[] array, int size, int find_sum){
int i, j;
int sum;
for(i =0;i&lt;size; i++){
sum = array[i];
for(j=i+1;j&lt;size;j++){
if(sum == find_sum){
int c = j-1;
System.out.println(i +&quot; &quot;+c);
return 1;
}
if(sum &gt; find_sum || j==size){
break;
}
sum += array[j];
}
}
System.out.println(&quot;-1&quot;);
return 0;
}

However, it still displays wrong output. Can you help me out?

答案1

得分: 0

public class SubarraySum {
    // 通常你会创建一个包含起始和结束索引的类,但我现在只返回一个字符串
    public static String findPosition(long[] array, long sumToFind) {
        for (int startIndex = 0; startIndex < array.length; startIndex++) {
            long sum = 0;
            for (int endIndex = startIndex; endIndex < array.length; endIndex++) {
                sum += array[endIndex];
                if (sum == sumToFind) {
                    // 返回基于1的索引
                    return String.format("(%d, %d)", startIndex + 1, endIndex + 1);
                } else if (sum > sumToFind) {
                    // 提前结束此检查,因为我们不能再添加值以达到较小的sumToFind
                    break;
                }
            }
        }
        return "-1";
    }

    public static void takeSizeAndSum(Scanner sc) {
        int size = sc.nextInt();
        long sumToFind = sc.nextLong();
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = sc.nextLong();
        }
        System.out.println(findPosition(array, sumToFind));
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        for (int i = 0; i < testCases; i++) {
            takeSizeAndSum(sc);
        }
    }
}
英文:

For your learning experience, using long. I've also formatted the code, used more logical and Java-like identifiers, removed a lot of unnecessary ones, split lines, added some comments, removed the debugging code, removed the unnecessary size parameter (arrays carry their `.length), split the calculation and the input / output, etc.

public class SubarraySum {
// generally you&#39;d create a class with the start- and end index but
// I&#39;ll just return a string for now
public static String findPosition(long[] array, long sumToFind) {
for (int startIndex = 0; startIndex &lt; array.length; startIndex++) {
long sum = 0;
for (int endIndex = startIndex; endIndex &lt; array.length; endIndex++) {
sum += array[endIndex];
if (sum == sumToFind) {
// return 1 based index
return String.format(&quot;(%d, %d)&quot;, startIndex + 1, endIndex + 1);
} else if (sum &gt; sumToFind) {
// end this check early, as we cannot add more values and get to a smaller sumToFind
break;
}
}
}
return &quot;-1&quot;;
}
public static void takeSizeandSum(Scanner sc) {
int size = sc.nextInt();
long sumToFind = sc.nextLong();
long[] array = new long[size];
for (int i = 0; i &lt; size; i++) {
array[i] = sc.nextLong();
}
System.out.println(findPosition(array, sumToFind));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
for (int i = 0; i &lt; testcases; i++) {
takeSizeandSum(sc);
}
}
}

huangapple
  • 本文由 发表于 2020年10月10日 18:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64292378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定