Cod sursa(job #1284738)

Utilizator AlexTAlexandru AlexT Data 6 decembrie 2014 19:35:30
Problema Subsecventa de suma maxima Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include<stdio.h>
#include<stdlib.h>


main()
{
FILE *f,*g;
f=fopen("ssm.in","r");
g=fopen("ssm.out","w");
int pozneg,neg,poz,pozmax,inceputmax,s,smax,inceput,i,j,n,*a;
fscanf(f,"%d ",&n);
a=(int*)malloc(n*sizeof(int));
poz=0;
smax=0;
s=0;
inceputmax=0;
inceput=0;
pozmax=0;

for(i=0;i<n;i++)
{
fscanf(f,"%d ",&a[i]);
if(a[i]>0){
s+=a[i];
if(s<0)
{
s=0;
inceput=i+1;
}
if(s>smax)
{
smax=s;
inceputmax=inceput;
pozmax=i;
}
}else
if(a[i]>neg){
	neg=a[i];
	pozneg=i;
}
}
if(smax>0){

fprintf(g,"%d %d %d ",smax,inceputmax+1,pozmax+1);
}
else{
	fprintf(g,"%d %d %d ",neg,pozneg,pozneg);
}
free(a);
return 0;
}