Pagini recente » Cod sursa (job #2146775) | Cod sursa (job #2885355) | Cod sursa (job #178769) | Cod sursa (job #1420852) | Cod sursa (job #62818)
Cod sursa(job #62818)
// Secventa de suma maxima
#include <fstream>
#define MAX 100
using namespace std;
int a[MAX];
int n;
int INFINIT = 32000;
void Read();
void Secv();
int main()
{
Read();
Secv();
return 0;
}
void Read()
{
ifstream fin("secv.in");
fin >> n;
for (int i = 0; i < n; i++)
fin >> a[i];
fin.close();
}
void Secv()
{
ofstream fout("secv.out");
int Smax = -INFINIT;
int i1, i2, j;
int S = -INFINIT;
for (int i = 0; i < n; i++)
{
if (Smax == -INFINIT && a[i] > 0)
i1 = i, i2 = i, S = Smax = a[i];
else
{
if ( S + a[i] > 0 )
S += a[i];
else
S = 0, j = i + 1;
if ( S > Smax )
{
Smax = S;
i2 = i;
i1 = j;
}
}
}
fout << i1 << " " << i2 << '\n' << Smax << '\n';
for (int i = i1; i <= i2; i++)
fout << a[i] << " ";
fout << '\n';
fout.close();
}