其他分享
首页 > 其他分享> > 2.从一堆数种取出所有众数,输出中间的那个,如果有偶数个众数,输出中间两个的平均值

2.从一堆数种取出所有众数,输出中间的那个,如果有偶数个众数,输出中间两个的平均值

作者:互联网

#include<bits/stdc++.h>
using namespace std;
int  num, count1,count2,sum;
int temp  = 0,temp1 = 0;
int a[100],b[100];
int* p;
void fun(int b[]) {
    for (int i = 0; i < 100; i++) {
        b[i] = 0;
    }
    
}
vector<int>vec;
vector<int>::iterator it;
int main() {
    while (cin >> num) {
        a[num]++;
        if (cin.get() == '\n') break;
    
    }
    for (int i = 0; i < 100; i++) {
        if (a[i] != 0) {
            if (temp < a[i]) {
                temp = a[i];
                fun(b);
                b[i] = a[i];
            }
            else if (temp == a[i]) {
                b[i] = a[i];
            }
            else if (temp > a[i]) {
                b[i] = 0;
            }
        }
    }
        for(int i =0;i<100;i++){
            if (b[i] != 0) {
                count1++;
                cout << i << "*" <<b[i] <<" ";
            }
        }
        if (count1 % 2 == 0) {
            for (int i = 0; i < 100; i++) {
                if (b[i] != 0) {
                    sum += i;
                }
            }
            cout << sum / count1 << endl;
        }
        else {
            for (int i = 0; i < 100; i++) {
                if (b[i] != 0) {
                    count2++;
                    if (count2 == count1 / 2 + 1) {
                        cout << i << endl;
                    }
                    
                }
            }
        }


}

 

标签:输出,temp,int,++,else,中间,num,众数,100
来源: https://www.cnblogs.com/q1231/p/16026046.html