# Ip address aiuto

**URL:** <https://forum.olinfo.it/t/ip-address-aiuto/5992>\
**Category:** Aiuto\
**Created:** [5 Gennaio 2020, 5:08pm UTC](https://forum.olinfo.it/t/ip-address-aiuto/5992 "2020-01-05T17:08:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeppo](https://forum.olinfo.it/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jeppo](https://forum.olinfo.it/u/jeppo)\
**Post date:** [5 Gennaio 2020, 5:08pm UTC](https://forum.olinfo.it/t/ip-address-aiuto/5992/1 "2020-01-05T17:08:07Z")

</div>

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.

```auto
/*
 * 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;
}

```

---

<div class="post-metadata">

**Author:** ![Francesco\_20](https://forum.olinfo.it/letter_avatar_proxy/v4/letter/f/b487fb/32.png) [@Francesco\_20](https://forum.olinfo.it/u/Francesco_20)\
**Post date:** [5 Gennaio 2020, 7:16pm UTC](https://forum.olinfo.it/t/ip-address-aiuto/5992/2 "2020-01-05T19:16:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeppo](https://forum.olinfo.it/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jeppo](https://forum.olinfo.it/u/jeppo)\
**Post date:** [5 Gennaio 2020, 8:52pm UTC](https://forum.olinfo.it/t/ip-address-aiuto/5992/3 "2020-01-05T20:52:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![v.bizzarri](https://forum.olinfo.it/letter_avatar_proxy/v4/letter/v/7c8e57/32.png) [@v.bizzarri](https://forum.olinfo.it/u/v.bizzarri)\
**Post date:** [6 Gennaio 2020, 7:46am UTC](https://forum.olinfo.it/t/ip-address-aiuto/5992/4 "2020-01-06T07:46:20Z")

</div>

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;
