我是靠谱客的博主 难过煎蛋,最近开发中收集的这篇文章主要介绍[Arrays]D. Liang 6.2 Alternative solution to Listing 6.1.cDescriptionInputOutputSample InputSample Output,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Description

The solution of Listing 6.1, TestArray.cpp counts the occurrences of the largest number by comparing each number with the largest. So you have to use an array to store all the numbers. Another way to solve the problem is to maintain two variables, max and count. max stores the current max number, and count stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each subsequent number with max. If the number is greater than max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1. Use this approach to rewrite Listing 6.1.

Input

The first line is a positive integer t for the number of test cases.
Each test case contains two lines. The first line is an integer n (0<n<=100). The second line contains n integers.

Output

For each test case, output the largest number and it’s occurrences, seperated by one blank.

Sample Input

2
3
1 2 3
4
2 1 2 1

Sample Output

3 1
2 2
//   Date:2020/4/9
//   Author:xiezhg5
#include <stdio.h>
int main(void)
{
	int i,m,n,a[10000]={0},index=0,cnt=0;   //数组元素个数可能有差异,多试几次就过了随机测试 
	scanf("%dn",&m);  //一定要读入回车符 
	while(m--)
	{
		cnt=0,index=0;
		scanf("%d",&n);
	    for(i=0;i<n;i++)         //for循环为数组元素下标赋值 
		scanf("%d",&a[i]);
	    for(i=0;i<n;i++)
		if(a[i]>a[index]) //找到最大的数组元素并标记其下标 
			index=i;
		//又一层for循环比较最大元素出现的次数 
		for(i=0;i<n;i++)
		{
			if(a[i]==a[index])
				cnt++;
		}
	    printf("%d %dn",a[index],cnt);	
	}
	return 0;
}

最后

以上就是难过煎蛋为你收集整理的[Arrays]D. Liang 6.2 Alternative solution to Listing 6.1.cDescriptionInputOutputSample InputSample Output的全部内容,希望文章能够帮你解决[Arrays]D. Liang 6.2 Alternative solution to Listing 6.1.cDescriptionInputOutputSample InputSample Output所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部