我就是个种田的,只有两亩地,爱咋咋地~!
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <process.h> void main(void) { int answer[4],guess,a=0,b=0,times=0,i,j,guess1[4]; for(i=0;i<=3;i++) { randomize(); answer[i]=rand()%10; for(j=0;j<i;j++) { while(answer[i]==answer[j]) { answer[i]=rand()%10; j=-1; } } } printf("please input four numbers e.g. 1234 and guess\n"); while(a!=4) { a=0; b=0; times++; do { printf("%d ",times); scanf("%d",&guess); if(guess==0) exit(0); }while(guess<1000||guess>9999); for(i=3;i>0;i--) guess1[i]=guess%10,guess/=10;
for(i=0;i<4;i++) { if(guess1[i]==answer[i])b++; for(j=0;j<4;j++) if(guess1[i]==answer[j])a++;//我想你该看得懂,至于这里是a,还是b你自己看着办 } /*for(i=3;i>-1;i--) { for(i=0;j<4;j++) { if((int)(guess/pow10(i))==answer[j]) { if(i+j==3) a=a+1; else b=b+1; } } guess=guess-(int)(guess/pow10(i))*pow10(i); } */ printf("%dA%dB\n",a,b); } }