ACM题天天练 4 Max Sum
Problem DescriptionGiven a sequence a,a,a......a, your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input 2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
源自http://acm.hdu.edu.cn/ 1003题
欢迎大家踊跃参与。。。
本帖最后由 hslx111 于 2012-8-23 13:36 编辑
//hud 1003
#include <iostream>
using namespace std;
int main()
{
int i,j,T,N;
int num=1;
cin>>T;
while(T--)
{
int start,end,pos,sum,now,temp;
cin>>N>>temp;
now=sum=temp;
start=end=pos=1;
for(i=2;i<=N;i++)
{
cin>>temp;
if(now+temp<temp)
now=temp,pos=i;
else
now+=temp;
if(now>sum)
sum=now,start=pos,end=i;
}
cout<<"Case "<<num<<":"<<endl;
cout<<sum<<" "<<start<<" "<<end<<endl;
if(T!=0) cout<<endl;
num++;
}
return 0;
}
好长时间不写,手都生了啊.... 本帖最后由 yel_hb 于 2012-8-23 13:58 编辑
...看了楼上的贴自己代码有点心虚的说...{:5_156:}
#include <iostream>
using namespace std;
int main()
{
int t,n,i,k,max,sum,x1,x2,y1;
int *list;
cin >> t;
k = 1;
while(t--)
{
cin >> n;
list = new int;
for(i = 0; i < n; ++i)
{
cin >> list;
}
x1 = 0;
y1 = 0;
x2 = 0;
max = list;
sum = list;
for(i = x1 + 1; i < n; ++i)
{
if(sum >= 0)
{
sum += list;
}
else
{
x2 = i;
sum = list;
}
if(max < sum)
{
max = sum;
x1 = x2;
y1 = i;
}
}
cout << "Case " << k++ << ":" << endl;
cout << max << " " << (x1 + 1) << " " << (y1 + 1) << endl;
if(t != 0)
{
cout << endl;
}
}
return 0;
}
本帖最后由 justcx 于 2012-8-23 14:41 编辑
#include<iostream>
#include<iostream>
#define N 100000
static st,ed;
int b;
int MaxSumDP(int *a, int n);
using namespace std;
int main()
{
int n1,n2;
int a;
cin>>n1;
for(int j=0;j<n1;j++)
{
cin>>n2;
for(int i=0;i<n2;i++)
{
cin>>a;
}
int M=MaxSumDP(a,n2);
cout<<"Case :"<<j+1<<endl;
cout<<M<<' '<<st<<' '<<ed<<endl;
cout<<endl;
}
return 0;
}
int MaxSumDP(int *a, int n)
{
int i, maxSum = 0;
int count=0;
if(a >=0)
{
maxSum=b=a;
st=1;
ed=1;
count++;
}
for(i=1; i<n; ++i)
{
b = (b+a > 0) ? (b+a) : 0;
if(b==0)
count++;
while(count==0)
{
if(b==0&&b>0)
{
st=i+1;
}
}
if(maxSum < b)
{
maxSum = b;
ed=i+1;
}
}
return maxSum;
}
代码复制上去咋格式老是变化掉了~就连部分字母和字符都有变化~ hslx111 发表于 2012-8-23 13:34 static/image/common/back.gif
//hud 1003
#include
{:5_143:}谢谢支持谢谢参与,真棒。。。 yel_hb 发表于 2012-8-23 13:55 static/image/common/back.gif
...看了楼上的贴自己代码有点心虚的说...
#include
using namespace st ...
{:5_151:}。。。 justcx 发表于 2012-8-23 14:42 static/image/common/back.gif
代码复制上去咋格式老是变化掉了~就连部分字母和字符都有变化~
是用“引用代码<>”添加上去的吗 Sample Output 的结果怎么得来的? 科尔沁猎人 发表于 2012-8-23 15:14 static/image/common/back.gif
Sample Output 的结果怎么得来的?
亲,是根据Sample Input得出来的。。 无语,调试没问题,谁能告诉我为何一直wa?
package com.slk.MaxSumSubsequence;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t, n;
int pos = 1;
int spos = 1;
int epos = 1;
int caseNum = 1;
t = Integer.parseInt(sc.nextLine());
while (t-- > 0) {
n = Integer.parseInt(sc.nextLine());
int currentInput = Integer.parseInt(sc.nextLine());
int tempSum = currentInput;
int maxSum = tempSum;
for (int i = 1; i < n; i++) {
currentInput = Integer.parseInt(sc.nextLine());
if (currentInput + tempSum < currentInput) {
tempSum = currentInput;
pos = i + 1;
} else {
tempSum += currentInput;
}
if (tempSum > maxSum) {
maxSum = tempSum;
spos = pos;
epos = i + 1;
}
}
System.out.println("Case " + caseNum++ + ":");
System.out.println(maxSum + " " + spos + " " + epos + "\n");
}
sc.close();
}
}
fghhslk 发表于 2012-8-23 15:52 static/image/common/back.gif
无语,调试没问题,谁能告诉我为何一直wa?
package com.slk.MaxSumSubsequence;
...
两个case之间有个空行... yel_hb 发表于 2012-8-23 16:25 static/image/common/back.gif
两个case之间有个空行...
加了"\n"了。。。用println同样wa
fghhslk 发表于 2012-8-23 16:29 static/image/common/back.gif
加了"\n"了。。。用println同样wa
最后一个case不能有吧... fghhslk 发表于 2012-8-23 15:52 static/image/common/back.gif
无语,调试没问题,谁能告诉我为何一直wa?
package com.slk.MaxSumSubsequence;
...
最后一个case后面是没有空行的 yel_hb 发表于 2012-8-23 16:37 static/image/common/back.gif
最后一个case不能有吧...
System.out.println("Case " + caseNum++ + ":");
System.out.println(maxSum + " " + spos + " " + epos);
if (t != 0)
System.out.println();
依旧wa hslx111 发表于 2012-8-23 16:43 static/image/common/back.gif
最后一个case后面是没有空行的
问题依旧。。。真是无语 sunhongbo 发表于 2012-8-23 15:02 static/image/common/back.gif
是用“引用代码”添加上去的吗
是的呢{:6_190:} fghhslk 发表于 2012-8-23 16:47 static/image/common/back.gif
问题依旧。。。真是无语
好不容易下了个Eclipse...发现不会用了
试试这组case:
1
5 -1 -1 -1 -1 -1 yel_hb 发表于 2012-8-23 17:26 static/image/common/back.gif
好不容易下了个Eclipse...发现不会用了
试试这组case:
1
Case 1:
-1 1 1 本帖最后由 maxOrder石 于 2012-8-23 17:55 编辑
hslx111 发表于 2012-8-23 13:34 static/image/common/back.gif
//hud 1003
#include
不用检测T的值和N的值吗,我就纳闷了,每次写完结果正确就是提交不了,求改,谢谢
#include<stdio.h>
int main()
{
int T;
scanf("%d",&T);
if(T<1||T>20)
{
return 0;
}
int N;
int i;
int j;
int s,MaxSum={0};
int starP,endP={0};
for(j=0;j<T;j++)
{
scanf("%d",&N);
if(N<1||N>100000)
return 0;
starP=1;
int *number=(int *)malloc(sizeof(int)*N);
for(i=0;i<N;i++)
{
scanf("%d",number+i);
}
s=0;
for(i=0;i<N;i++)
{
s=s+*(number+i);
if(MaxSum<s)
{
MaxSum=s;
endP=i+1;
}
}
}
for(i=0;i<T;i++)
{
printf("Case %d:\n",i+1);
printf("%d %d %d\n\n",MaxSum,starP,endP);
}
return 0;
}
字体变了,最后的printf(是MaxSum中括号i,后面都是一样的额) fghhslk 发表于 2012-8-23 17:36 static/image/common/back.gif
Case 1:
-1 1 1
额...发现结果不确定。不过currentInput = Integer.parseInt(sc.nextLine());
这个输入是读取一行的吧...应用nextInt()试试... yel_hb 发表于 2012-8-23 18:57 static/image/common/back.gif
额...发现结果不确定。不过currentInput = Integer.parseInt(sc.nextLine());
nextInt已经试过,妥妥的不行,放弃了。。。多谢帮忙debug maxOrder石 发表于 2012-8-23 17:52 static/image/common/back.gif
不用检测T的值和N的值吗,我就纳闷了,每次写完结果正确就是提交不了,求改,谢谢
#include
int main() ...
这要看出题风格了,hdu的题一般对T和N这样的已知值没有严格的限制,不过写上是对的,我是懒= =
请问你的结果是WA了吗?
justcx 发表于 2012-8-23 17:18 static/image/common/back.gif
是的呢
语言什么的都正确么,AC了没? hslx111 发表于 2012-8-23 21:01 static/image/common/back.gif
这要看出题风格了,hdu的题一般对T和N这样的已知值没有严格的限制,不过写上是对的,我是懒= =
请问你的结果 ...
恩,是的,你怎么知道,哈 sunhongbo 发表于 2012-8-23 22:23 static/image/common/back.gif
语言什么的都正确么,AC了没?
都正确的,运行过了~前几次复制还没问题的,今天就出了这个问题~把符号[]变成<>或者就没有~ maxOrder石 发表于 2012-8-23 23:20 static/image/common/back.gif
恩,是的,你怎么知道,哈
首先starP是会变的,你这里默认都是1了,还有最后一个case后面只有一个\n的.
而且那个判断最大子序列的过程好像有点问题哦,你可以看看前面别人写的代码或者自己再好好想想 justcx 发表于 2012-8-23 23:20 static/image/common/back.gif
都正确的,运行过了~前几次复制还没问题的,今天就出了这个问题~把符号[]变成或者就没有~
AC了就好呀 亲。{:8_370:} 怎么天天练不更新了?
页:
[1]