- char* getHint(char* secret, char* guess) {
- int nBull = 0;
- int nCow = 0;
- int nlen = strlen(secret);
- int nCount[10]={0};
- int nIndex = 0;
- for(;nIndex<nlen;++nIndex)
- {
- if( secret[nIndex] == guess[nIndex] )
- {
- ++nBull;
- }
- else
- {
- nCount[secret[nIndex]-'0']++;
- if( nCount[secret[nIndex]-'0'] <= 0 )
- {
- nCow++;
- }
- nCount[guess[nIndex] -'0']--;
- if (nCount[guess[nIndex]- '0'] >= 0) {
- nCow++;
- }
- }
- }
- char buff[20];
- sprintf(buff, "%dA%dB", nBull, nCow);
- return buff;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1209201615066.html
来源: http://www.codesnippet.cn/detail/1209201615066.html