Thread: Wrong answer or just an output problem?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    Wrong answer or just an output problem?

    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?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    120
    Yes, and what seems to be the officer problem?

    Also, I am portuguese and I highly recomend not writing code in portuguese if at all possible. Besides, I don't believe many people will be able to help you if they don't understand your names.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I will not even try to understand the code with so many global variables
    Code:
        for (idx = 0; idx < linhas; idx++)
        {
            printf("Original number was %d\n", number[FV]);
    For example the 2 lines above look like bug on the first glance till I realized that FV is global variable that contains exactly same value as local var idx...
    I really do not feel inspired to run over whole code just to verify that FV has not been changed somewhere else so this condition will fail.

    I just think - naaah, this code is not worth reading...

    So I have only one advice - get rid of all globals.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting Wrong answer ???
    By Fazot in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2012, 08:45 AM
  2. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  3. Replies: 3
    Last Post: 09-06-2009, 01:48 PM
  4. Wrong answer?
    By eViLrAcEr in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2009, 06:04 AM
  5. wrong answer?
    By cman in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 02:17 AM