Timus1296.Hyperjump(Java代码来解决问题)

题目要求:

Time limit: 1.0 second
Memory limit: 64 MB

题目正文:

Developed in the beginning of XXI century, hyperjump remains the primary method of transportation for distances up to thousands parsecs. But physicists have recently discovered an amazing phenomenon. They believe the duration of the hyperjump alpha phase can be easily controlled. Alpha phase is the period when hyper-spacecraft accumulates its gravity potential. The larger is the gravity potential accumulated, the less energy is required to complete the hyperjump. Your task is to write a program, which would help pilots decide when to enter and when to leave the alpha-phase, in order for the hyperspacecraft to accumulate the largest possible gravity potential.

The most crude gravity field model (which you will have to use) yields the sequence of integers pi, which represent field intensities at different moments in time. According to this model, if the alpha-phase begins at moment i and ends at moment j, then the value of gravity potential accumulated will be equal to the sum of sequence elements at places from i-th to j-th inclusive.

Input

The first line of the input contains an integer N being the number of elements in the intensity values sequence (0 ≤ N ≤ 60000). Next N lines specify sequence elements, each line containing a single integer pi (−30000 ≤ pi ≤ 30000).

Output

The only line of output contains the largest possible value of the gravity potential that can be accumulated by a hyperspacecraft during the alpha phase. You should assume that the initial gravity potential of a hyperspacecraft is equal to zero.

Samples

inputoutput
10
31
-41
59
26
-53
58
97
-93
-23
84
187
3
-1
-5
-6
0

代码解释:

First, reference the scanner class to obtain the console input. After establishing the array that meets the problem setting interval, use the for loop to evaluate max. first, set the variable temp, add it one by one with the corresponding array under the for loop, compare it with the original max value in real time with the if statement, and consider whether to assign a new value to max. after completing all the traversal process, get the final answer.

Computational complexity of algorithm:n^2

具体代码(Source code):

import java.util.Scanner;
 
public class Solution1296 {
    public static void main(String[] args) {
        int maxSum = 0;
        int temp = 0;
        int[] arr = new int[60047];
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        for (int j = 0; j <n; j++) {
            temp += arr[j];
            if (temp > maxSum) {
                maxSum = temp;
            }
            else if(temp<0) {
                temp=0;
            }
        }
        System.out.print(maxSum);

        sc.close();

   }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值