2 条题解

  • 0
    @ 2023-12-2 20:54:53

    #include<stdio.h> #include using namespace std; int a[10005]; /交换两个整数的值/ void swap(int *p, int *q) { int buf;

    buf = *p;
    *p = *q;
    *q = buf;
    
    return;
    

    } /查找一个有序数组中的众数/ int find_mode_number(int *arr,int len) { int many = 1,less = 1; int value = 0; for(int i = 0; i < len; i++) { for(int j = i; j < len; j++) { if(arr[j] == arr[j+1]) { less++; } else { if(many < less) { swap(&many,&less); value = arr[j]; } less = 1; break; } } }

    return value;
    

    }

    int main() { int n; scanf("%d",&n); //输入数组元素 for(int i=0; i<n; i++) { scanf("%d",&a[i]); } sort(a,a+n); //将数组排序 int sum=0; //先求和后求平均数 for(int i=0; i<n; i++) { sum+=a[i]; //数组求和 } double avg=sum1.0/n1.0;

    double mid_num;
    if(n%2==0)
    {
        mid_num=(a[(n-1)/2]+a[n/2])*1.0/2.0;
    }
    else
    
    {
        mid_num=a[n/2];
    }
    
    int count=find_mode_number(a,sizeof(a)/sizeof(int));//调用求众数函数
    
    printf("%.2lf %d %.1lf\n",avg,count,mid_num);
    return 0;
    

    }

    • 0
      @ 2023-12-2 20:53:51

      #include<stdio.h>

      #include

      using namespace std;

      int a[10005];

      void swap(int *p, int *q){

      int buf;

      buf = *p;
      
      *p = *q;
      
      *q = buf;
      
      
      return;
      

      }

      int find_mode_number(int *arr,int len)

      {

      int many = 1,less = 1;

      int value = 0;

      for(int i = 0; i < len; i++)

      {

      for(int j = i; j < len; j++)

      {

      if(arr[j] == arr[j+1])

      {

      less++;

      }

      else

      {

      if(many < less)

      {

      swap(&many,&less);

      value = arr[j];

      }

      less = 1;

      break;

      }

      }

      }

      return value;
      

      }

      int main()

      {

      int n;

      scanf("%d",&n);

      for(int i=0; i<n; i++)

      {

      scanf("%d",&a[i]);

      }

      sort(a,a+n);

      int sum=0;

      for(int i=0; i<n; i++)

      {

      sum+=a[i];

      }

      double avg=sum1.0/n1.0;

      double mid_num;

      if(n%2==0)

      {

      mid_num=(a[(n-1)/2]+a[n/2])*1.0/2.0; }

      else

      {
      
          mid_num=a[n/2];
      
      }
      
      int count=find_mode_number(a,sizeof(a)/sizeof(int));
      
      printf("%.2lf %d %.1lf\n",avg,count,mid_num);
      
      return 0;
      

      }

      • 1

      【入门】求N个整数的平均数、众数和中位数

      信息

      ID
      179
      时间
      1000ms
      内存
      16MiB
      难度
      9
      标签
      递交数
      11
      已通过
      6
      上传者