【C++】1180 - Number of occurrences of a number

question

There are 50 numbers (0~19). What is the maximum number of times the same number appears among these 50 numbers?

Insert image description here

1. Analyze the problem

  1. Known: The number of pages is N
  2. Unknown: How many 0, 1, 2...9 are used.
  3. Relationship: count

2. Define variables

a[20]: The number of times the same number appears.
b[50]: 50 numbers.
temp: temporary worker

//二、数据定义 
	int a[20]={
    
    },b[50],temp;

3. Enter data

Enter 50 numbers and count the number of times this number appears.

	//三、数据输入
	for(int i=0;i<50;i++){
    
    
		cin>>b[i];
		temp=b[i];
		a[temp]+=1;
	}

4.Data calculation

Compare each number with the first number, and if a number is greater than temp, assign its value to temp.

//四、数据计算 
	temp=a[0];
	for(int i=0;i<20;i++){
    
    
		if(temp<a[i]){
    
    
			temp=a[i];
		}
	}

5. Output results

#include<iostream>
using namespace std;
int main(){
    
    
	//一、分析问题
	//已知:页数为N
	//未知:用了多少个0,1,2…9。
	//关系:计数 

	
	//二、数据定义 
	int a[20]={
    
    },b[50],temp;
	

	//三、数据输入
	for(int i=0;i<50;i++){
    
    
		cin>>b[i];
		temp=b[i];
		a[temp]+=1;
	}

	//四、数据计算 
	temp=a[0];
	for(int i=0;i<20;i++){
    
    
		if(temp<a[i]){
    
    
			temp=a[i];
		}
	}
	

	//五、输出结果 

	cout<<temp<<endl;

	return 0;	
}

Guess you like

Origin blog.csdn.net/qq_39180358/article/details/135384474