ZZULIOJ 1123 Best Alumni (C language implementation)

insert image description hereTo solve the problem, we can use an array. Whenever an element is entered, the corresponding subscript will be added. It may be a bit difficult to understand here, so let's give a small example to understand.
eg: insert image description hereThe array is all initialized to 0, if n=3, then arr[n]++, then arr[3] becomes 1, if arr[n]+=2, then arr[3] ] into 3, we use this relationship to store the number of times each number is entered.
Note: The title requires the output with the most occurrences, and there may be multiple inputs with the most repetitions. So pay attention to traversing the array.
Compiler: VS2019;
Source code:
int main()
{ int arr[100] = { 0 }; int i = 0; int max = arr[0]; while (scanf_s(“%d”,&i)&&i >= 0)//Input -1 to end, every time i is input, arr[i]++ will be generated; { arr[i]++;





}

for (i = 0; i < 100; i++)//找到最大值
{
	if (arr[i] > max)

	{
		max = arr[i];
	}

}
for ( i = 0; i < 100; i++)//将最大值打印输出,可能会有重复的,所以对100个进行遍历。
{
	if (arr[i] == max)
	{
		printf("%d ", i);
	}


}

return 0;

}

Guess you like

Origin blog.csdn.net/Kirihara_Yukiho/article/details/123928698