1

root 站长 2022-11-15 14:01:36 2022-11-17 15:31:56 0

1111

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int a[110], b[110];
double sum = 0; //当前数字的总和
int x2[110];    //数字的平方
double sum2 = 0; //平方和
int cur = 0;    // 当前最后一个数字的位置
int n, k;       
int op, x; 
bool full = false;
 
int main()
{
	scanf("%d%d", &n, &k);
	while (n--)
	{		
		scanf("%d", &op);
		if (op == 1)
		{
			scanf("%d", &x);
			sum = sum - a[cur] + x;   
			a[cur] = x;
			sum2 = sum2 - x2[cur] + x*x; 
			x2[cur] = x*x;    //平方 
			cur++;
			if (cur == k) cur = 0, full = true; 
		}	
		else if (op == 2)
		{
			int t = k;     //设窗口是满的 
			if (!full)  t = cur; //如果窗口没满,那就设成当前位置
			printf("%lld.00\n", (ll)(sum/t)); 
		}
		else if (op == 3)
		{
			int t = k;
			if (!full) t = cur;
			printf("%.2lf\n", sum2/t-(sum/t)*(sum/t));
		}
		else 
		{
			int t = k;
			if (!full) t = cur;
			for (int i = 0; i < t; i ++ ) b[i] = a[i];
			sort(b, b+t);
			if (t % 2 != 0)
				printf("%.2lf\n", 1.0*a[t/2]);
			else
				printf("%.2lf\n", 1.0*(a[t/2]+a[t/2-1])/2);
		}
	} 
	return 0;
}
{{ vote && vote.total.up }}

共 5 条回复

Eil5

什么东西?

nr0728

CPP 刷题王

???

root 站长

<i title="刷新" aria-hidden="true" class="redo icon"></i>

root 站长
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n,sum,p,num,k,now,cnt,s[105],a[1000050];
int read()
{
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int main()
{
    n=read();k=read();
    while(n--)
    {
        p=read();
        if(p==1)
        {
            num=read();
            a[++cnt]=num;
            if(now==k)sum=sum-a[cnt-k]+num,s[a[cnt-k]]--;
            else now++,sum+=num;
            s[num]++;
        }
        else if(p==2)printf("%.2lf\n",floor((double)sum/now));
        else if(p==3)
        {
            double w=(double)sum/now,ans=0;
            for(int i=cnt-now+1;i<=cnt;i++)ans+=((double)a[i]-w)*((double)a[i]-w);
            printf("%.2lf\n",ans/now);
        }
        else
        {
            if(now%2)
            {
                int pos=now/2+1,ans=0;
                for(int i=0;i<=100;i++)
                {
                    ans+=s[i];
                    if(ans>=pos)
                    {
                        printf("%.2lf\n",(double)i);
                        break;
                    }
                }
            }
            else
            {
                int pos1=now/2,pos2=now/2+1,st=-1,ed=-1,ans=0;
                for(int i=0;i<=100;i++)
                {
                    ans+=s[i];
                    if(ans>=pos1&&st==-1)st=i;
                    if(ans>=pos2&&ed==-1)ed=i;
                    if(st!=-1&&ed!=-1)
                    {
                        printf("%.2lf\n",(double)(st+ed)/2);
                        break;
                    }
                }
            }
        }
    }
    return 0;
}