LeetCode, LCP 01: Guess the number

Little A and Little B are playing guessing numbers. Little B randomly chooses one from 1, 2, 3 each time, and Little A also
chooses one guess from 1, 2, 3 each time . They played this game three times in total. Please go back to Little A. How many times did you guess correctly?
The input guess array is the guess of small A each time, and the answer array is the choice of small B each time.
The length of guess and answer are both equal to 3.
Example 1:
Input: guess = [1,2,3], answer = [1,2,3]
Output: 3
Explanation: Little A guessed right every time.

Example 2:
Input: guess = [2,2,3], answer = [3,2,1]
Output: 1
Explanation: Little A only guessed correctly the second time.

Idea: The same number of elements in the guess and answer arrays with the same subscript

//方法1
int game1(int* guess, int guessSize, int* answer, int answerSize){
    
    
    return(*guess==*answer)+(*++guess==*++answer)+(*++guess==*++answer);
}

//方法2
int game2(int* guess, int guessSize, int* answer, int answerSize) 
{
    
    
	int count = 0;//统计猜对的次数
	for (int i = 0; i < guessSize; i++)
	{
    
    
		if (guess[i] == answer[i])//比较两个数组相同位置下的值是否相等
		{
    
    
			count++;
		}
	}
	return count;
}


The running results on Likou correspond to method 1 and method 2 respectively
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45824959/article/details/113107157