当前位置:网站首页>Sequence table - find main element

Sequence table - find main element

2022-06-13 00:48:00 Feiyichai

  • Find the main element

Examples of kingcraft : As efficient as possible , Find the main element

  • Algorithmic thought

Because it is required to be as efficient as possible , So we use the time for space algorithm , Set up an auxiliary array , Its capacity is the maximum in the sequence , Count their appearance times , If it meets the requirements, output .

  • Code implementation

#include<stdio.h>
#define MAXSIZE 100
int main(){
    
	// Find the main element  
	 int a[] ={
    0,3,3,3,3,7,5,5};
	 int temp[MAXSIZE];
	 int max=a[0];
	 int i;
	 for(i=0;i<9;i++){
    // Find the maximum value of the sequence 
	 	if(max<a[i])
	 	max=a[i];
	 }
	 printf("%d ",max);
	 for(i=0;i<max;i++){
    // Auxiliary array timer initialization 
		temp[i]=0;
	 }
	 for(i=0;i<9;i++){
    
	 	temp[a[i]]++;// Statistics 
	 }
	 int t=0;
	 for(i=0;i<max;i++){
    
	 	if(temp[i]>=n/2){
    
	 		 t=i;// Main element found 
	 		break;
		 } 
	}
	if(t!=0)// Output main element 
	printf("%d",t);
}
原网站

版权声明
本文为[Feiyichai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280559016755.html