Advertisement
xDefo

Indovina numero

Oct 15th, 2020 (edited)
2,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include<stdio.h>
  2. void main(void)
  3.  {
  4.   char c;
  5.   int numero=0;
  6.   int num_max=100000;
  7.   int num_min=0;
  8.   int i=0;
  9.   int verifica=0;
  10.   printf("Pensa un numero intero tra %d e %d senza dirlo!Digita s quando lo hai pensato: ",num_min,num_max);
  11.   scanf("%c",&c);
  12.   if (c=='s')
  13.    {
  14.     c=' ';
  15.     for(;;)
  16.      {i=i+1;
  17.       numero=(num_max+num_min)/2;
  18.       printf("Il numero è %d? Scrivi s se ho indovinato altrimenti > se il numero è più grande o < se il numero è più piccolo: ",numero);
  19.       scanf(" %c",&c);
  20.       if (c=='s')
  21.        {
  22.         verifica=1;
  23.         break;
  24.        }
  25.       else if (c=='>')
  26.        {
  27.         if(numero==(num_max-1))
  28.         {
  29.          printf("Il numero è %d!\n",num_max);
  30.          verifica=1;
  31.          break;
  32.         }
  33.         else num_min=numero;
  34.        }
  35.       else if (c=='<') num_max=numero;
  36.       else break;
  37.      }
  38.    }
  39.   if(verifica==1) printf("L'ho indovinato in %d mosse\n",i);
  40.   else printf("C'è stato un errore\n");
  41.  }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement