Pagini recente » Cod sursa (job #2968365) | Cod sursa (job #1986842) | Cod sursa (job #2962652) | Cod sursa (job #633609) | Cod sursa (job #2676300)
// SCmax.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
using namespace std;
int* maxSeq(int n, int vec[], int* maxL) {
int indStart = 0;
int indEnd = 0;
int crrStart = -1;
int crrEnd = -1;
int maxLoc = -1;
for (int i = 1; i < n; i++)
{
if (vec[i - 1] < vec[i])
{
if (crrStart == -1)
{
crrStart = i - 1;
}
crrEnd = i;
}
else
{
if (maxLoc < crrEnd - crrStart + 1)
{
maxLoc = crrEnd - crrStart + 1;
indStart = crrStart;
indEnd = crrEnd;
crrStart = -1;
crrEnd = -1;
}
}
}
//orderd array
if (maxLoc < crrEnd - crrStart + 1)
{
maxLoc = crrEnd - crrStart + 1;
indStart = crrStart;
indEnd = crrEnd;
}
//*seq = (int*)malloc(n * sizeof(int));
int *seq = new int[maxLoc];
int j = -1;
for (int i = indStart; i <= indEnd; i++) {
seq[++j] = vec[i];
}
*maxL = maxLoc;
return seq;
}
int main() {
int n = 0;
int* vec = NULL;
ifstream file("scmax.in");
file >> n;
int value = 0;
vec = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n;i++) {
file >> value;
vec[i] = value;
}
file.close();
int max = 0;
int *seq = maxSeq(n, vec, &max);
ofstream output_file("scmax.out");
output_file << max << "\n";
for (int i = 0; i < max;i++) {
output_file << seq[i]<< " ";
}
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file