國立屏東大學 資訊工程學系 程式設計
—-
#include <stdio.h> #include <stdlib.h> #define codeLen 4 int answer[codeLen]; int guess[codeLen]; void generateCode(); void getUserGuess(); int checkAGuess(); int main() { int quit=0; int count=0; generateCode(); while(!quit) { printf("#%d :", ++count); getUserGuess(); if( checkAGuess() ) quit=1; } printf("Bye!\n"); } void generateCode() { answer[0]=1; answer[1]=2; answer[2]=3; answer[3]=4; } void getUserGuess() { scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3] ); } int checkAGuess() { int a=0, b=0, i, j; for(i=0; i< codeLen ; i++) { // guess[i] for(j=0;j< codeLen ;j++) { if ((guess[i]==answer[j]) && (i==j)) { a++; break; } else if (guess[i]==answer[j]) { b++; break; } } } printf("...>>>> %dA %dB\n", a, b); if(a==4) return 1; else return 0; }