厨师和前缀和后缀的总和(得到一些测试用例失败) [英] Chef and sum with prefix and suffix(getting some test case failed)

查看:129
本文介绍了厨师和前缀和后缀的总和(得到一些测试用例失败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Our little chef is fond of doing additions/sums in his free time. Today, he has an array A consisting of N positive integers and he will compute prefix and suffix sums over this array.

He first defines two functions prefixSum(i) and suffixSum(i) for the array as follows. The function prefixSum(i) denotes the sum of first i numbers of the array. Similarly, he defines suffixSum(i) as the sum of last N - i + 1 numbers of the array.

Little Chef is interested in finding the minimum index i for which the value prefixSum(i) + suffixSum(i) is the minimum. In other words, first you should minimize the value of prefixSum(i) + suffixSum(i), and then find the least index i for which this value is attained.





我尝试过:





What I have tried:

<pre>import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	int t;
	Scanner sc=new Scanner(System.in);
	t=sc.nextInt();
	for(int i=0;i<t;i++)
	{
	    int n=sc.nextInt();
	    int sum=0,count=0,min=Integer.MAX_VALUE;
	    int[] arr=new int[n+1];
	    int[] prefix=new int[n+1];
	    int[] suffix=new int[n+1];
	    int[] array=new int[n];
        int[] array1=new int[n];
	    for(int j=0;j<n;j++)
	    {
	    arr[j]=sc.nextInt();
	    sum+=arr[j];
	    }
	    suffix[0]=sum;
	   for(int j=1;j<n;j++)
	   {
	       sum-=(arr[j-1]);
	       suffix[j]=sum;
	   }
	     prefix[0] = arr[0];
	     for (int j = 1; j < n; j++)
	     {
	         prefix[j] = prefix[j-1] + arr[j];
	        
	     }
	     int max=Integer.MAX_VALUE;
	     for(int j=0;j<n;j++)
	     {
	         array[j]=suffix[j]+prefix[j];
             if(min>=array[j])
                 min=array[j];
           //  array1[j]=array[j];
	        //System.out.print(array[j] +" ");
	    // min=Math.min(min,array[j]);
	     }
      
	   
//	     max=array[0];
	   for(int j=0;j<array.length;j++)
	   {
	       if(array[j]==min)
           {
               if(max>=j)
                   max=j;
           }
	     //  System.out.println(j);
	    // System.out.print(array[j]+" ");
	      
	   }
       System.out.println(max+1);
        
	}
	}
}

推荐答案

Quote:

主厨和前缀和后缀的总和(让一些测试用例失败)

Chef and sum with prefix and suffix(getting some test case failed)



显示一个简化的失败示例(不再超过10个值)有助于理解你的问题。



你知道你的代码应该做什么,使用调试器找到你的代码不表现的地方预期。

特别注意你如何设置前缀,因为我理解这个问题,前缀的第一个元素应为0.



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Showing a reduced example of failure (no more than 10 values) would have helped to understand your problem.

You know what your code is supposed to do, use the debugger to find where your code do not behave as expected.
Pay special attention to how you set prefix, as I understand the problem, the prefix of first element should be 0.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于厨师和前缀和后缀的总和(得到一些测试用例失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆