TOPIC

90% de erro mas não sei o que é

oalho1 asked 6 years ago

Nos meus teste da tudo certo mas quando envio da 90% de erro.

#include <stdio.h>
int main(){

    int cont=1, caixa=0, x, i;

    do{
        scanf("%d", &x);
        if(x!=0){
            int entrada[x], saida[x];

            for(i=0; i<x; i++)
                scanf("%d %d", &entrada[i], &saida[i]);

            printf("Teste %d\n", cont);

            for(i=0; i<x; i++){
                caixa+=entrada[i];
                caixa-=saida[i];
                printf("%d\n", caixa);
            }
            printf("\n");
            cont++; 
        }
    }while(x!=0);

    return 0;
}

Remember not post solutions. Your post may be reviewed by our moderators.

  • lfvtrivelatto replied 6 years ago

    Está faltando zerar a variável 'caixa' entre um teste e outro. No caso de exemplo funciona porque o primeiro teste termina com 'caixa' = 0; se a inverter a ordem dos testes, dá errado:

    4
    0 5
    12 0
    0 20
    17 1
    3
    20 25
    10 5
    10 10
    0
  • Habib671 replied 4 years ago

    try this..u have to put 0 in caixa again when 1 loop is over :

    include<bits/stdc++.h>

    using namespace std; int main(){

    int cont=1, l=0, N, i;
    
    do{
        scanf("%d", &N);
        if(N!=0){
            int J[N], Z[N];
    
            for(i=0; i<N; i++)
                scanf("%d %d", &J[i], &Z[i]);
    
            printf("Teste %d\n", cont);
    
            for(i=0; i<N; i++){
                l+=J[i];
                l-=Z[i];
                printf("%d\n", l);
            }
            printf("\n");
            cont++;
        }
        l=0;
    }while(N!=0);
    
    return 0;

    }