HDU 2015 偶数求和

http://acm.hdu.edu.cn/showproblem.php?pid=2015

Problem Description
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。
 
Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。
 
Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。
 
Sample Input
3 2
4 2
 
Sample Output
3 6
3 7
 
代码:
#include <bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;
int a[111];

int main()
{
    for(int i=1; i<=101; i++)
    {
        a[i]=2*i;
    }
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        double b[111]={0};
        int sum=0;
        if(m>n)
        {
            for(int i=1; i<=n; i++)
                sum+=a[i];
            printf("%d
",sum/n);
        }
        int cnt=0;
        for(int i=1; i<=n; i+=m)
        {
            cnt++;
            for(int j=i; j<i+m; j++)
            {
                b[cnt]+=a[j];
            }
        }
        if(n%m!=0)
        {
            for(int i=n+1; i<=(n/m+1)*m; i++)
                b[cnt]=b[cnt]-a[i];
        }
        for(int i=1; i<=cnt; i++)
        {
            if(n%m==0)
            {
                if(i!=cnt)
                    cout<<b[i]/m<<" ";
                else
                    cout<<b[i]/m<<endl;
            }
            if(n%m!=0)
            {
                if(i!=cnt)
                    cout<<b[i]/m<<" ";
                else
                    cout<<b[i]/(n-(n/m)*m)<<endl;
            }
        }
    }
    return 0;
}
[

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9343843.html