题目:
题解:
#define MIN(a, b) ((a) < (b) ? (a) : (b))
char * getHint(char * secret, char * guess){
int A = 0;
int B = 0;
int dicS[10] = {0};
int dicG[10] = {0};
int i = 0;
while(secret[i]){
if(secret[i] == guess[i]){
//同位置且相等(完全匹配),公牛数+1
A++;
}else{
//同位置不相等,分别配统计 secret 和 guess 中不能完全匹配的数字的个数
dicS[(int)(secret[i]-'0')]++;
dicG[(int)(guess[i]-'0')]++;
}
i++;
}
int j = 0;
for(j = 0; j < 10; j++){
//统计奶牛数
B += MIN(dicS[j], dicG[j]);
}
char* res = malloc(sizeof(char)*9);
sprintf(res, "%dA%dB", A, B);
return res;
}