题解

14 条题解

  • 2
    @ 2018-10-30 15:04:20

    sort排序后可以扫一遍求出每个数的个数,注意强制转换

    #include<iostream>
    #include<algorithm>
    using namespace std;
    long long a[50010],z[50010];
    int main()
    {
        int n,i,maxn=0,now=0,top=0;
        long long sum=0;
        cin>>n;
        for(i=1;i<=n;i++)
         cin>>a[i];
        sort(a+1,a+n+1);
        for(i=1;i<=n;i++)
         {
            sum+=a[i];
            if(a[i]==a[i-1])
             now++;
            else
             now=1;
            if(now==maxn)
            {
                top++;
                z[top]=a[i];
            }
            if(now>maxn)
            {
                maxn=now;
                top=1;
                z[top]=a[i];
            }
         }
        printf("%.2lf\n",double(sum)/double(n));
        if(n&1)
         printf("%.2lf\n",double(a[n/2+1]));
        else
         printf("%.2lf\n",double(a[n/2]+a[n/2+1])/2.0);
        for(i=1;i<=top;i++)
         cout<<z[i]<<" ";
        return 0;
    }
    
  • 1
    @ 2017-06-29 18:01:08
    #include<stdio.h>
    #include<map>
    #include<algorithm>
    using namespace std;
    int n,s[50010],x=0;
    map<int,int> q;
    double v,m;
    int main(){
        scanf("%d",&n);
        for(int i=0;i<n;++i) scanf("%d",s+i),v+=s[i],q[s[i]]++;
        sort(s,s+n);
        printf("%.2lf\n",v/n);
        printf("%.2lf\n",(s[n/2]+s[(n-1)/2])/2.);
        for(map<int,int>:: iterator it=q.begin();it!=q.end();it++)
            if(it->second>x) x=it->second;
        for(map<int,int>:: iterator it=q.begin();it!=q.end();it++)
            if(x==it->second) printf("%d ",it->first);
    }
        
    
  • 0
    @ 2017-07-30 21:25:38

    我竟然傻成给int的map重载比较符号???sort也不会用了。。。唉,下学期怕是要挂科。

    #include<iostream>
    #include <algorithm>
    #include <map>
    using namespace std;
    map<int,int>num;
    int data[50005];
    int main(void){
        int m;
        cin>>m;
        double sum=0;
        for(int i=0;i<m;i++){
            cin>>data[i];
            sum+=data[i];
            num[data[i]]++;
        }
        printf("%.2lf\n",sum/m);
        sort(data,data+m);
        if(m%2){
            printf("%.2lf\n",(double)data[m/2]);
        }
        else{
            int temp=data[m/2]+data[(m-1)/2];
            printf("%.2lf\n",temp/2.);
        }
        //auto iter=num.begin();
        int max=0;
        for(auto iter=num.begin();iter!=num.end();iter++){
            if(iter->second>max){
                max=iter->second;
            }
        }
        for(auto iter=num.begin();iter!=num.end();iter++){
            if(iter->second==max){
                cout<<iter->first<<' ';
            }
        }
    }
    
  • 0
    @ 2016-08-16 18:53:43

    无比朴素的做法
    ```c++
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<iomanip>
    #include<map>
    using namespace std;

    const int maxn = 50000 + 10;
    int n;
    int num[maxn];
    map<int, int> cnt;
    vector<int> vec, modes;
    double median, mean;

    int main () {
    cin >> n;
    for (int i = 0; i < n; i++) {
    cin >> num[i];
    if (!cnt.count(num[i])) {
    cnt[num[i]] = 1;
    vec.push_back(num[i]);
    }
    else cnt[num[i]]++;
    mean += (double)num[i]/n;
    }
    sort(num, num+n);
    if (n % 2 == 0) median = ((double)num[n/2-1]+num[n/2]) / 2;
    else median = num[n/2];

    int max_cnt = 0;
    for (int i = 0; i < vec.size(); i++)
    max_cnt = max(max_cnt, cnt[vec[i]]);
    for (int i = 0; i < vec.size(); i++)
    if (cnt[vec[i]] == max_cnt) modes.push_back(vec[i]);

    sort(modes.begin(), modes.end());

    cout << fixed << setprecision(2) << mean << "\n";
    cout << median << "\n";
    cout << modes[0];
    for (int i = 1; i < modes.size(); i++)
    cout << " " << modes[i];
    return 0;
    }
    ```

  • 0
    @ 2016-06-27 12:24:15

    C++ STL 版
    c++
    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
    //
    #include<bits/stdc++.h>
    using namespace std;
    int n;
    int nums[50010];
    multiset<int> ms;
    set<int> uniq;
    int maxhit = -1;
    int main() {
    scanf("%d", &n);
    double avg = 0;
    for (int i = 0;i < n;i++)
    {
    scanf("%d", nums + i);
    avg += nums[i];
    ms.insert(nums[i]);
    uniq.insert(nums[i]);
    maxhit = max(maxhit,(int) ms.count(nums[i]));
    }
    avg /= n;
    printf("%.2lf\n", avg);
    sort(nums, nums + n);
    if (n & 0x1)
    {
    printf("%.2lf\n", (double)nums[n >> 1]);
    }
    else
    {
    printf("%.2lf\n", ((double)nums[n >> 1] + nums[(n >> 1) - 1]) / 2);
    }
    for (set<int>::iterator ite = uniq.begin();ite !=uniq.end();ite++)
    {
    if (ms.count(*ite) == maxhit)printf("%d ", *ite);
    }
    }

  • 0
    @ 2016-02-29 12:06:23

    Pascal AC
    var n,m,i,j,t:longint;
    k,s:double;
    a,b:array[1..50001]of longint;
    procedure qsort(l,r:longint);
    var i,j,mid,p:longint;
    begin
    i:=l; j:=r;
    mid:=a[(l+r)div 2];
    repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
    begin
    p:=a[i];
    a[i]:=a[j];
    a[j]:=p;
    inc(i);
    dec(j);
    end;
    until i>j;
    if l<j then qsort(l,j);
    if i<r then qsort(i,r);
    end;
    begin
    readln(n);
    k:=0;
    t:=0;
    m:=0;
    for i:=1 to n do
    begin
    read(a[i]);
    k:=k+a[i];
    end;
    k:=k/n;
    qsort(1,n);
    if n mod 2=0 then
    s:=(a[n div 2]+a[(n div 2)+1])/2
    else s:=a[(n div 2)+1];
    j:=a[1];
    for i:=1 to n+1 do
    if a[i]=j then inc(t)
    else begin
    if t=m then
    b[i]:=j;
    if t>m then
    begin
    m:=t;
    fillchar(b,sizeof(b),0);
    b[i]:=j;
    end;
    t:=1;
    j:=a[i];
    end;
    writeln(k:0:2);
    writeln(s:0:2);
    for i:=1 to n+1 do
    if b[i]<>0 then write(b[i],' ');
    end.

  • 0
    @ 2015-08-09 09:47:15

    本人方法还可以,顺大溜吧……@……
    program exam;
    var o:int64;
    i,j,n,k,l,max:longint;
    a,b,x:double;
    d,e,f,h:array[0..100000] of longint;
    procedure qt(l,r:longint);
    var i,j,m,p:longint;
    begin
    i:=l; j:=r;
    m:=d[(l+r) div 2];
    repeat
    while d[i]<m do inc(i);
    while d[j]>m do dec(j);
    if i<=j then
    begin
    p:=d[i]; d[i]:=d[j]; d[j]:=p;
    inc(i); dec(j);
    end;
    until i>j;
    if i<r then qt(i,r);
    if l<j then qt(l,j);
    end;

    begin
    readln(n);
    o:=0;
    for i:=1 to n do
    begin
    read(d[i]);
    o:=o+d[i];
    end;
    a:=o/n;
    for i:=1 to n do
    f[i]:=1;
    qt(1,n);
    if n mod 2=1 then
    x:=d[(n div 2)+1]
    else
    x:=(d[(n div 2)]+d[(n div 2)+1])/2;
    e[1]:=d[1];
    k:=1;
    for i:=2 to n do
    begin
    if d[i]<>e[k] then
    begin
    inc(k);
    e[k]:=d[i];
    end
    else
    inc(f[k]);
    end;
    max:=0;
    for i:=1 to k do
    if f[i]>max then max:=f[i];
    for i:=1 to k do
    if f[i]=max then begin inc(l); h[l]:=e[i]; end;
    writeln(a:0:2);
    writeln(x:0:2);
    for i:=1 to l-1 do
    write(h[i],' ');
    write(h[l]);
    end.

  • 0
    @ 2015-02-13 22:18:08

    测试数据 #0: Accepted, time = 15 ms, mem = 1100 KiB, score = 10
    测试数据 #1: Accepted, time = 15 ms, mem = 1100 KiB, score = 10
    测试数据 #2: Accepted, time = 15 ms, mem = 1100 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 1104 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 1100 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 1100 KiB, score = 10
    测试数据 #6: Accepted, time = 15 ms, mem = 1104 KiB, score = 10
    测试数据 #7: Accepted, time = 0 ms, mem = 1100 KiB, score = 10
    测试数据 #8: Accepted, time = 78 ms, mem = 1104 KiB, score = 10
    测试数据 #9: Accepted, time = 62 ms, mem = 1096 KiB, score = 10
    Accepted, time = 200 ms, mem = 1104 KiB, score = 100
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=50005;
    double k[maxn]={0};int f[maxn]={0};
    int numk;
    double ave=0,mid,peo;
    int main()
    {
    k[0]=-1.2134;
    scanf("%d",&numk);
    for(int i=1;i<=numk;i++)f[i]=1;
    for(int i=1;i<=numk;i++){scanf("%lf",&k[i]);ave+=k[i];}ave/=numk;
    sort(k+1,k+1+numk);
    int maxl=1,maxst;
    for(int i=1;i<=numk;i++)
    { int c=0;
    if(k[i]==k[i-1])
    for(int j=i-1;j<=numk;j++)
    if(k[j]==k[i-1])c++;else break;
    if(c>1)f[i]=c;maxl=max(maxl,c);
    }printf("%.2lf\n",ave);
    if(!(numk%2))printf("%.2lf\n",(k[numk/2]+k[numk/2+1])/2);
    else printf("%.2lf\n",k[numk/2+1]);
    for(int i=1;i<=numk;i++)
    if(f[i]==maxl) printf("%.0lf ",k[i]);printf("\n");
    return 0;
    }

  • 0
    @ 2014-06-30 16:14:31

    41 C:\Documents and Settings\402\My Documents\未命名1.cpp [Warning] converting to int' fromdouble'

  • 0
    @ 2013-09-24 20:43:09

    求大牛改动
    program p1742;
    var a,b,c:array[1..50001] of longint;
    n,time,m,e,max:longint;
    q,d:real;

    procedure swap(var a,b:longint);
    var t:longint;
    begin
    t:=a;
    a:=b;
    b:=t;
    end;
    procedure qsort(s,t:longint);
    var i,j,mid:longint;
    begin
    i:=s;j:=t; mid:=a[(s+t)div 2];
    while i<=j do
    begin
    while (a[i]<mid) do inc(i);
    while (a[j]>mid)do dec(j);
    if i<=j then
    begin
    swap(a[i],a[j]);
    inc(i);
    dec(j);
    end;
    end;
    if i<t then qsort(i,t);
    if j>s then qsort(s,j);
    end;

    procedure getdata_and_calc;
    var i:longint;
    begin
    readln(n);
    for i:=1 to n do
    begin
    read(a[i]);
    end;
    qsort(1,n);
    end;

    procedure average;
    var i:longint;
    begin
    for i:= 1 to n do
    begin
    e:=e+a[i];
    end;
    q:=e/n;
    writeln(q:0:2);
    end;

    procedure mid;
    begin
    if n mod 2=1 then
    writeln(a[n div 2 +1],'.00')
    else
    writeln((a[n div 2+1]+a[n div 2])/2:0:2);
    end;

    procedure max1;
    var i,j,temp,k,sum,l,o:longint;

    begin
    j:=0;
    k:=0;
    sum:=0;
    fillchar(b,sizeof(b),0);
    for i:=1 to n do
    if a[i]=a[i-1] then
    inc(b[j])
    else begin
    inc(j);
    inc(b[j]);
    end;
    for l:=1 to j do
    if b[l]>max then
    max:=b[l];
    for o:=1 to j do
    if b[o]=max then
    begin
    inc(k);
    for l:=1 to o-1 do
    sum:=sum+b[l];
    inc(sum);
    c[k]:=a[sum];
    sum:=0;
    end;
    for i:=1 to k-1 do
    write(c[i],' ');
    write(c[k]);
    end;

    begin
    getdata_and_calc;
    average;
    mid;
    max1;
    end.
    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 1408 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 1412 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 1416 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 1416 KiB, score = 10
    测试数据 #4: WrongAnswer, time = 11 ms, mem = 1412 KiB, score = 0
    测试数据 #5: WrongAnswer, time = 11 ms, mem = 1408 KiB, score = 0
    测试数据 #6: WrongAnswer, time = 0 ms, mem = 1412 KiB, score = 0
    测试数据 #7: WrongAnswer, time = 0 ms, mem = 1416 KiB, score = 0
    测试数据 #8: WrongAnswer, time = 421 ms, mem = 1416 KiB, score = 0
    测试数据 #9: WrongAnswer, time = 31 ms, mem = 1412 KiB, score = 0
    WrongAnswer, time = 474 ms, mem = 1416 KiB, score = 40

  • 0
    @ 2013-08-30 20:48:21

    众数:排序后,记录每个数出现的个数a[i],和个数的最大值max,相同的数第一个出现的位置bj[i];枚举,a[i]=max就输出,max=1还要输出最后一个数

  • 0
    @ 2012-10-31 20:36:49

    {

    ID:darkgod-z

    PROG:vijos P1742

    HANG:PASCAL

    }

    var

    n:longint;

    a:array [1..50000] of longint;

    procedure qsort(l,h:integer);

    var

    i,j,t,m:integer;

    begin

    i:=l;

    j:=h;

    m:=a[(i+j) div 2];

    repeat

    while a[i]

  • 0
    @ 2012-10-30 19:33:23

    对于平均数,我们只需要对所有数求和再除以n即可。注意保存和的变量需要用long long或double,而不能用int(否则会越界)。若用long long的话,最后除以n的时候需要强制类型转换成double(否则"/"被编译器视为整除)。

    对于中位数,我们只需要对所有数进行排序。C ++的库中提供了快速排序函数。举个例子,如果我们将n个数存在了一个int类型的数组a的a[0]~a[n-1]的位置。那么调用"sort(a, a + n);"后,n个数将从小到大排好序。然后如果n是奇数,就输出a[(n + 1) / 2];否则输出a[n/2]和a[n/2+1]的平均值。

    对于众数。我们将n个数排序后,相同的数形成连续的若干段。只需要一遍循环,统计每个数出现的次数,并且记录次数的最大值。之后再循环一遍,将所有次数等于最大值的元素输出即可。

    时间复杂度:O(nlogn)

  • -1
    @ 2012-11-02 10:48:01

    超详细题解传送门:

    http://user.qzone.qq.com/1304445713/blog/1351821644

    不设访问权限,没有动画,没有音乐,很整洁就一个博客而已!

  • 1

信息

ID
1742
难度
6
分类
模拟 点击显示
标签
(无)
递交数
921
已通过
255
通过率
28%
被复制
2
上传者