So I recently started training and created a code for a problem online:
UVa Online Judge
And this is the code for it:
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int number[5000], resultados[5000], ChainG = 0, FV = 0;
char temp[10], desc[10], asc[10];
void OrganizarDesc(int tamanho)
{
int idx, idx2, aux;
for (idx = 0; idx<tamanho - 1; idx++)
{
for (idx2 = idx + 1; idx2<tamanho; idx2++)
{
if (desc[idx] < desc[idx2])
{
aux = desc[idx];
desc[idx] = desc[idx2];
desc[idx2] = aux;
}
}
}
}
void OrganizarAsc(int tamanho)
{
int idx, idx2, aux;
for (idx = 0; idx < tamanho - 1; idx++)
{
for (idx2 = idx + 1; idx2 < tamanho; idx2++)
{
if (asc[idx] > asc[idx2])
{
aux = asc[idx];
asc[idx] = asc[idx2];
asc[idx2] = aux;
}
}
}
}
int IntToString(int num)
{
int idx = 0;
while (num > 0)
{
temp[idx] = ('0' + (num % 10));
num /= 10;
idx++;
}
temp[idx] = '\0';
return idx;
}
int Procura(int Dma)
{
int idx;
for (idx = 0; idx < ChainG; idx++)
if (Dma == resultados[idx])
return 1;
return 0;
}
void Analise(int num)
{
int n;
int Dma;
int a, d;
n = IntToString(num);
memset(desc, '\0', sizeof(desc));
memset(desc, '\0', sizeof(asc));
strcpy(desc, temp);
OrganizarDesc(n);
strcpy(asc, temp);
OrganizarAsc(n);
d = atoi(desc);
a = atoi(asc);
Dma = d - a;
printf("%d - %d = %d\n", d, a, Dma);
if (Procura(Dma))
{
ChainG++;
}
else
{
resultados[ChainG] = Dma;
ChainG++;
Analise(Dma);
}
}
int main()
{
int linhas = -1, idx;
do
{
linhas++;
scanf("%d", &number[linhas]);
} while (number[linhas] != 0);
for (idx = 0; idx < linhas; idx++)
{
ChainG = 0;
printf("Original number was %d\n", number[FV]);
Analise(number[idx]);
FV++;
printf("Chain length %d\n", ChainG);
printf("\n");
}
return 0;
}
----------------------------------------------------------------------------
It says the answers correctly. I've tried several numbers, did the math, it actually matches the one on the website but it still accuses wrong answer. Could anyone help?