|
发表于 2020-12-19 01:48:52
|
显示全部楼层
本帖最后由 jackz007 于 2020-12-19 02:01 编辑
- #include <stdio.h>
- int main(void)
- {
- int b , d[100] , c , e , i , j , k , n , m , t ;
- double r1 , r2 , r3 ;
- scanf("%d" , & n) ;
- for(i = 0 ; i < n ; i ++) scanf("%d" , & d[i]) ;
- for(i = 0 ; i < n - 1 ; i ++) {
- for(j = i ; j < n ; j ++) {
- if(d[j] < d[i]) {
- t = d[i] ;
- d[i] = d[j] ;
- d[j] = t ;
- }
- }
- }
- for(m = 0 , k = b = e = d[0] , c = i = 1 ; i < n ; i ++) {
- k += d[i] ;
- if(d[i] == e) {
- c ++ ;
- } else {
- if(c > m) {
- b = e ;
- m = c ;
- }
- e = d[i] ;
- c = 1 ;
- }
- }
- r1 = k * 1.0 / n ;
- r2 = (n % 2) ? d[n / 2] : (d[n / 2 - 1] + d[n / 2]) / 2.0 ;
- r3 = b ;
- printf("%.2f\t%.2f\t%.2f\n" , r1 , r2 , r3) ;
- }
复制代码
编译、运行实况
- D:\00.Excise\C>cl x.c
- 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
- 版权所有(C) Microsoft Corporation。保留所有权利。
- x.c
- Microsoft (R) Incremental Linker Version 14.28.29334.0
- Copyright (C) Microsoft Corporation. All rights reserved.
- /out:x.exe
- x.obj
- D:\00.Excise\C>x
- 80
- 18 20 1 3 5 7 9 2 4 6
- 23 25 36 8 7 6 5 4 3 2
- 89 88 87 32 33 34 56 67 78 99
- 56 77 99 82 84 86 38 48 58 68
- 39 3 9 8 5 2 6 18 38 98
- 23 25 36 8 7 6 5 4 3 2
- 39 3 9 8 5 2 6 18 38 98
- 89 88 87 32 33 34 56 67 78 99
- 34.84 24.00 2.00
- D:\00.Excise\C>
复制代码
最后的结果分别是:平均数、中位数、众数 |
|