模拟算法(Simulation algorithm)

模拟算法,就是指程序完全按照题目描述的方式运行,从而获得相应的结果。它的特点就是不需要去想怎么样去解决问题,而是有现成的解决方案,只要按照方案一步步去实现。

例一:求一组数的平均数(mean),中位数(median)、众数(mode)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**************************************************************** 
 * Description: mean  median   mode
 * Author: Alex Li
 * Date: 2024-06-10 17:43:49
 * LastEditTime: 2024-06-10 20:08:18
****************************************************************/
#include <iostream> 
#include <algorithm>
using namespace std;

//找平均数
float mean(float arr[], int n){
    float sum = 0;
    for(int i = 0;i < n; i++)
        sum += arr[i];
    return sum/n;
}

//找中位数
float median(float arr[], int n){
    //对数组排序
    sort(arr, arr + n);
    if(n % 2 == 0)
        return (arr[n/2 - 1] + arr[n/2])/2;
    return arr[n/2];
}

//找众数
float mode( float arr[], int n){
    // 排序 
    sort(arr, arr + n); 
    //找最大的序列 
    int max_count = 1, res = arr[0], count = 1; 
    for (int i = 1; i < n; i++) { 
        if (arr[i] == arr[i - 1]) 
            count++; 
        else { 
            if (count > max_count) { 
                max_count = count; 
                res = arr[i - 1]; 
            } 
            count = 1; 
        } 
    } 
  
    // 如果最后一个是众数
    if (count > max_count){ 
        max_count = count; 
        res = arr[n - 1]; 
    } 
    
    return res;
}
int main(){
    int n;
    float arr[50];
    cout<<"Enter the size of array: ";
    cin>>n;
    //input in the array
    cout<<"Enter the elements of array: ";
    for(int i = 0; i < n; i++)
        cin>>arr[i];
    //print mean, median and mode of ungrouped data in array
    cout<<"\nMean = "<<mean(arr, n);
    cout<<"\nMedian = "<<median(arr, n);
    cout<<"\nMode = "<<mode(arr, n);
    
    return 0;
}

例二、如果众数不唯一

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//计算众数,输出众数及其个数

#include <iostream>
using namespace std;
int a[100],b[100][100]={0};
int main(){
    int n,q=0,zero=0,max_number=0;
    bool m=true;
    cin>>n;
    for (int i = 0; i <n ; i++){
        cin>>a[i];
    }
    for (int i = 0; i < n; i++){
          m=true;
        for (int j = 0; j <= q; j++){
            if(b[j][0]==a[i]){
                b[j][1]++;
                m=false;
            }
        }
        
        if(m){
           b[q][0]=a[i];
           b[q][1]=1;
         q++;
        }
    }
    for (int i = 0; i < n; i++) {
        if(a[i]==0)zero++;
    }
   
    for (int i = 0; i < n; i++){
       if(max_number<b[i][1])max_number=b[i][1];
    }
    if(max_number>zero){
    cout<<"max number is: "<<max_number<<endl;
    cout<<"mode index is: ";
   for (int i = 0; i < q; i++){        
       if(b[i][1]==max_number)cout<<b[i][0]<<' ';
   }
    }
    else cout<<"max number is: "<<zero<<"mode index is : "<<"zero";   
}

NOIP2010普级组接水问题 (P1190)

Scroll to Top