hdu 2015

来源:互联网 发布:sql2008数据库置疑 编辑:程序博客网 时间:2024/04/25 09:48
New~ 欢迎参加2016多校联合训练的同学们~

偶数求和

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 74997    Accepted Submission(s): 32027


Problem Description
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。
 

Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。
 

Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。
 

Sample Input
3 24 2
 

Sample Output
3 63 7
 
#include <iostream>
#include <cstring>
#include <stdio.h>


using namespace std;
int main()
{
    int flag=0,sum=0;
    int n,m;
    while(cin>>n>>m)
    {
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            sum+=i*2;
            ans++;
            if(ans==m)
            {
                if(flag==0)
                {
                    flag=1;
                }
                else
                {
                    cout<<" ";
                }
                cout<<sum/m;
                ans=0;
                sum=0;
            }


        }
        if(ans!=0)
        {
            cout<<" "<<sum/ans<<endl;
        }
        else
        {
            cout<<endl;
        }
        sum=0;
        flag=0;
    }
    return 0;
}
0 0