04.MostFrequentNumber
Здравейте! Изпитвам затруднения със задача 04.MostFrequentNumber от упражнението за масиви. Аз написах този код за задачата:
#include <iostream>
using namespace std;
void readInput(int arr[], int& lenght)
{
std::cin>>lenght;
for(int i=0; i<lenght; i++)
{
std::cin>>arr[i];
}
}
int number0=0;
int number1=0;
int number2=0;
int number3=0;
int number4=0;
int number5=0;
int number6=0;
int number7=0;
int number8=0;
int number9=0;
void findMostFrequenceNumber(int arr[], int lenght)
{
for(int i=0; i<lenght; i++)
{
switch(arr[i])
{
case 0: number0+=1; break;
case 1: number1+=1; break;
case 2: number2+=1; break;
case 3: number3+=1; break;
case 4: number4+=1; break;
case 5: number5+=1; break;
case 6: number6+=1; break;
case 7: number7+=1; break;
case 8: number8+=1; break;
case 9: number9+=1; break;
}
}
}
void findBiggest()
{
if(number0>number1&&number0>number2&&number0>number3&&number0>number4&&number0>number5&&number0>number6&&number0>number7&&number0>number8&&number0>number9){
cout<<"0"<<endl;
}
else if(number1>number2&&number1>number3&&number1>number4&&number1>number5&&number1>number6&&number1>number7&&number1>number8&&number1>number9){
cout<<"1"<<endl;
}
else if(number2>number3&&number2>number4&&number2>number5&&number2>number6&&number2>number7&&number2>number8&&number2>number9){
cout<<"2"<<endl;
}
else if(number3>number4&&number3>number5&&number3>number6&&number3>number7&&number3>number8&&number3>number9){
cout<<"3"<<endl;
}
else if(number4>number5&&number4>number6&&number4>number7&&number4>number8&&number4>number9){
cout<<"4"<<endl;
}
else if(number5>number6&&number5>number7&&number5>number8&&number5>number9){
cout<<"5"<<endl;
}
else if(number6>number7&&number6>number8&&number6>number9){
cout<< "6 "<<endl;
}
else if(number7>number8&&number7>number9){
cout<< "7"<<endl;
}
else if(number8>number9){
cout<<"8"<<endl;
}
else if(number9>number0){cout<<"9"<<endl;}
}
int main(){
int arr[1000];
int lenghtArr=0;
readInput(arr, lenghtArr);
findMostFrequenceNumber(arr, lenghtArr);
findBiggest();
return 0;
}
Дава ми само 60 от 100. Доколкото се досещам май е, защото не покривам възможността за равен брой числа. Не ми харесва и това, че е толкова дълъг, но не мога да се досетя за друго по-ефикасно решение. Моля за малко помощ!