Ip address aiuto

Non capisco perchè mi dà 70/100 non mi risolve gli ultimi 3 testcase del subtask4 .
Mi aiutereste ad arrivare a 100 dicendomi cosa sbaglio.Graie in anticipo.

/*
 * This template is valid both in C and in C++,
 * so you can expand it with code from both languages.
 * NOTE: it is recommended to use this even if you don't
 * understand the following code.
 */

#include <stdio.h>
#include <assert.h>

/*
 * This template is valid both in C and in C++,
 * so you can expand it with code from both languages.
 * NOTE: it is recommended to use this even if you don't
 * understand the following code.
 */

#include <stdio.h>
#include <assert.h>

// input data
int A[4], B[4];

int main() {
//  uncomment the following lines if you want to read/write from files
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    assert(4 == scanf("%d.%d.%d.%d", &A[0], &A[1], &A[2], &A[3]));
    assert(4 == scanf("%d.%d.%d.%d", &B[0], &B[1], &B[2], &B[3]));

    // at this point, the arrays A and B contain four elements each with the individual IP octects
    // for example: 192.168.1.1 creates an array [192, 168, 1, 1] with 192 at index 0

    // insert your code here

    long long result = ((B[0]*256*256*256)+(B[1]*256*256)+(B[2]*256)+B[3]) - ((A[0]*256*256*256)+(A[1]*256*256)+(A[2]*256)+A[3]) + 1;

    printf("%lld\n", result);  // print the result
    return 0;
}

Ciao, il problema è nella dichiarazione degli array, infatti quando moltiplichi B[0] e A[0] per 256³ il risultato non rientra negli int e la moltiplicazione può restituire un numero errato. Per evitare ciò dovresti dichiarare entrambi gli array come long long.

1 Mi Piace

grazie mille. ho usato 'allegato e di solito è giusto quindi non ho controllato se superava o no il limite.
grazie mille

1 Mi Piace

L’allegato può anche andare bene, basta solo che nella riga che calcola result venga fatto il cast di B[0] e A[0] a long long e va.

long long result = (((long long)B[0]* 256* 256*256)+(B[1]*256 * 256)+(B[2] * 256)+B[3]) - (((long long)A[0] * 256 * 256 *256)+(A[1]*256 *256)+(A[2]*256)+A[3]) + 1;