Pagini recente » Cod sursa (job #1481261) | Istoria paginii runda/oji200424234 | Cod sursa (job #1243475) | Cod sursa (job #2734502) | Cod sursa (job #1209317)
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <string.h>
#include <wchar.h>
#include <iostream>
using namespace std;
//I'll retain into an array only the ascending subarrays from the given input
int wmain (DWORD argc, WCHAR** argv)
{
int k;
int i;
int nrValues;
int nrElements;
int nr1;
int nr2;
int startIndex;//the index from wich the longest subarray starts
int maxStartIndex;
int maxLength;
int auxMaxLength;
int *maxAscendingSubarray = (int *)malloc(100 * sizeof(int));// can be bigger than 100
int j;//index for maxAscendingSubarray
FILE *infile;
FILE *outfile;
maxStartIndex =-1;
auxMaxLength = -1;
maxLength = 1;
j=0;
infile = fopen ("scmax.in","r");
if (infile != NULL)
{
fscanf_s(infile, "%d", &nrValues);
for( i =0; i< nrValues; i++)
{
fscanf_s(infile, "%d", &nr1);
maxAscendingSubarray[i] = nr1;
}
fclose(infile);
}
i = 0;
while(i < nrValues - 1)
{
if(maxAscendingSubarray[i]<= maxAscendingSubarray[i + 1])
{
startIndex = i;
while(maxAscendingSubarray[i] <= maxAscendingSubarray[i + 1])
{
maxLength++;
i++;
}
if( maxLength > auxMaxLength )
{
auxMaxLength = maxLength;
}
maxLength = 1;
}
else
{
i++;
}
}
maxLength = auxMaxLength;
k = 0;
nrElements = startIndex + maxLength;
while( k < nrElements-1 )
{
if( maxAscendingSubarray[k] == maxAscendingSubarray[k+1] )
{
maxLength--;
for( i = k; i < nrElements-1; i++)
{
maxAscendingSubarray[i] = maxAscendingSubarray[ i + 1 ];
}
nrElements--;
}
k++;
}
outfile = fopen ("scmax.out","w");
if (outfile != NULL)
{
fprintf_s(outfile, "%d\n", maxLength);
for( i = startIndex; i< nrElements; i++)
{
fprintf(outfile, "%d ", maxAscendingSubarray[i]);
}
fclose(outfile);
}
free(maxAscendingSubarray);
_getch();
return 0;
}