Chinaunix首页 | 论坛 | 博客
  • 博客访问: 76889
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-26 14:00
个人简介

有梦想的人,正在努力

文章分类

全部博文(32)

文章存档

2015年(32)

我的朋友

分类: C/C++

2015-10-01 14:04:15


  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;

  4. int main()
  5. {
  6.     int c, input, maxSum, frontPos, tempFrontPos, tailPos, tempSum;
  7.     int ct = 1;
  8.     scanf("%d", &c);
  9.     while(c--)
  10.     {
  11.         tempSum = 0;
  12.         maxSum = -1000000000;
  13.         tempFrontPos = frontPos = tailPos = 0;
  14.         int n;
  15.         scanf("%d", &n);
  16.         for(int i = 0; i < n; ++i)
  17.         {
  18.             scanf("%d", &input);
  19.             tempSum += input;
  20.             if(tempSum > maxSum)
  21.             {
  22.                 maxSum = tempSum;
  23.                 tailPos = i;
  24.                 frontPos = tempFrontPos;
  25.             }
  26.             if(tempSum < 0 && (i != n - 1)) //i != n - 1 保证 frontPos 不会指到一个不存在的位置
  27.             {
  28.                 tempSum = 0;
  29.                 tempFrontPos = i + 1;
  30.             }
  31.         }
  32.         printf("Case %d:\n", ct++);
  33.         printf("%d %d %d\n", maxSum, frontPos+1, tailPos+1);
  34.         if(c != 0) printf("\n");
  35.     }
  36.     return 0;
  37. }

阅读(619) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~