【九度】题目1376:最近零子序列

108 篇文章 0 订阅
102 篇文章 5 订阅
题目1376:最近零子序列
时间限制:1 秒内存限制:32 兆特殊判题:否提交:367解决:94
题目描述:
给定一个整数序列,你会求最大子串和吗?几乎所有的数据结构与算法都会描述求最大子串和的算法。今天让大家来算算最近0子串和,即整数序列中最接近0的连续子串和。例如,整数序列6, -4, 5, 6中,连续子串{-4,5}的和为1,比其他任何连续子串的和都更接近0。该整数序列的最近0子串和就是1.
输入:
每个测试文件包含多个测试案例,每个测试案例两行,第一行包括一个整数N,代表整数序列的长度,第二行是以空格隔开的N个整数,代表该整数序列。其中我们能保证1 <= N <= 105,每个整数大于等于-230且小于230.
输出:
对于每个整数序列,输出一行,包含一个整数,即最近0子串和。如果同时存在多个解(如-1, 3, 1存在-1和1两个解),则输出最大的一个(输出1)。
样例输入:
4
6 -4 5 6
2
-1 1
样例输出:
1
0
答疑:
解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-8099-1-1.html
解题思路:
暴力搜索,求每个子串的和。注意如果单个元素的值为0,直接输出0即可。

Java AC

import java.util.Scanner;
 
public class Main {
    /*
     * 1376
     */
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int array[] = new int[n];
            int result = -1;
            for (int i = 0; i < n; i++) {
                array[i] = scanner.nextInt();
                if (array[i] == 0) {
                    result = 0;
                }
            }
            if (result == 0) {
                System.out.println(result);
                continue;
            }
            result = array[0];
            for (int i = 0; i < n; i++) {
                int sum = 0;
                for (int j = i; j < n; j++) {
                    sum += array[j];
                    int tempSum1 = Math.abs(sum);
                    int tempSum2 = Math.abs(result);
                    if (tempSum1 < tempSum2
                            || (tempSum1 == tempSum2 && sum > result)) {
                        result = sum;
                        if (result == 0) {
                            break;
                        }
                    }
                }
                if (result == 0) {
                    break;
                }
            }
            System.out.println(result);
        }
    }
}
 
/**************************************************************
    Problem: 1376
    User: wangzhenqing
    Language: Java
    Result: Accepted
    Time:1730 ms
    Memory:105284 kb
****************************************************************/
C++ AC

#include <stdio.h>
 
const int maxn = 100005;
int array[maxn];
 
int abs(int x){
    return x < 0 ? x * -1 : x;
}
int main(){
    int n,i,j;
    while(scanf("%d",&n) != EOF){
        int result = -1;
        for(i = 0; i < n; i++){
            scanf("%d",array+i);
            if(array[i] == 0){
                result = 0;
            }
        }
        if(result == 0){
            printf("%d\n",result);
            continue;
        }
        result = array[0];
        for (i = 0; i < n; i++) {
            int sum = 0;
            for (j = i; j < n; j++) {
                sum += array[j];
                int tempSum1 = abs(sum);
                int tempSum2 = abs(result);
                if (tempSum1 < tempSum2
                        || (tempSum1 == tempSum2 && sum > result)) {
                    result = sum;
                    if (result == 0) {
                        break;
                    }
                }
            }
            if (result == 0) {
                break;
            }
        }
        printf("%d\n",result);
    }
    return 0;
}
/**************************************************************
    Problem: 1376
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:140 ms
    Memory:1412 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值