#include <stdio.h>
#include <stdlib.h>
char *input_argv[] = { "prog.c", "11", "5", "7", "11", "22" }; // al posto di argv[]
int input_argc = sizeof (input_argv) / sizeof (input_argv[0]); // al posto di argc
int
ricercaseq (int lista[], int x, int a, int z)
{
if (a > z)
{
// La corrispondenza non รจ stata trovata.
return -1;
}
else if (x == lista[a])
{
return a;
}
else
{
return ricercaseq (lista, x, a+1, z);
}
}
int
main (int argc, char *argv[])
{
int *lista
= (int *) malloc ((input_argc
- 2) * sizeof (int)); int x;
int i;
// Acquisisce il primo argomento come valore da cercare.
sscanf (input_argv
[1], "%i", &x
);
// Considera gli argomenti successivi come gli elementi
// dell'array da scandire.
for (i = 2; i < input_argc; i++)
{
sscanf (input_argv
[i
], "%i", &lista
[i
-2]); }
// Esegue la ricerca.
i = ricercaseq (lista, x, 0, input_argc - 2);
// Emette il risultato.
printf ("%i si trova nella posizione %i\n", x
, i
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmNoYXIgKmlucHV0X2FyZ3ZbXSA9IHsgInByb2cuYyIsICIxMSIsICI1IiwgIjciLCAiMTEiLCAiMjIiIH07ICAgICAgLy8gYWwgcG9zdG8gZGkgYXJndltdCmludCAgIGlucHV0X2FyZ2MgICA9IHNpemVvZiAoaW5wdXRfYXJndikgLyBzaXplb2YgKGlucHV0X2FyZ3ZbMF0pOyAgLy8gYWwgcG9zdG8gZGkgYXJnYwoKaW50CnJpY2VyY2FzZXEgKGludCBsaXN0YVtdLCBpbnQgeCwgaW50IGEsIGludCB6KQp7CiAgICBpZiAoYSA+IHopCiAgICAgIHsKICAgICAgICAvLyBMYSBjb3JyaXNwb25kZW56YSBub24gw6ggc3RhdGEgdHJvdmF0YS4KCiAgICAgICAgcmV0dXJuIC0xOwogICAgICB9CiAgICBlbHNlIGlmICh4ID09IGxpc3RhW2FdKQogICAgICB7CiAgICAgICAgcmV0dXJuIGE7CiAgICAgIH0KICAgIGVsc2UKIC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