英文:
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<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);
}
}
}
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<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;
}
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'd create a class with the start- and end index but
// I'll just return a string for now
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) {
// return 1 based index
return String.format("(%d, %d)", startIndex + 1, endIndex + 1);
} else if (sum > sumToFind) {
// end this check early, as we cannot add more values and get to a smaller 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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论