C语言游戏--猜数字

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int answer[4],guess,a,b,times,i,j,r;
    int source[10]={0,1,2,3,4,5,6,7,8,9};
    a=b=times=0;
    srand(time(NULL));
    for(i=0;i<4;i++)
    {
        r=rand()%(10-i);
        answer[i]=source[r];
        for(j=r;j<10;j++)
            source[j]=source[j+1];
    }
    printf("please guess \n");
    while(a!=4)
    {
       a=0;
       b=0;
       times++;
       printf("%d    ",times);
       scanf("%d",&guess);
       for(i=3;i>-1;i--)
       {
         for(j=0;j<4;j++)
         {
           if(answer[j]==guess/(int)pow10(i))
           {
             if(i+j==3)
                a+=1;
             else
                b+=1;                             
           }
         }
         guess=guess%(int)pow10(i);
       }
       printf("     %dA%dB\n",a,b);
       if(times==10)
       {
           printf("Sorry, please try again!\n");
           exit(0);
       }           
    }
    return 0;    
}

你可能感兴趣的:(职场,休闲)