HDU_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
 1 #include <cstdio>
 2 int main()
 3 {
 4    int n,m,temp,ans;
 5    while(~scanf("%d%d",&n,&m))
 6       {
 7          ans=0;temp=0;
 8          for(int i=2;i<=2*n;i=i+2)
 9             {
10                ans=ans+i;;
11                temp++;
12                if(temp==m || i==2*n)
13                   {
14                      printf(i==2*n?"%d":"%d ",ans/temp);
15                      ans=0;temp=0;
16                   }          
17             }
18          printf("\n");     
19       }
20    return 0;   
21 }

 

posted @ 2013-06-16 17:17  瓶哥  Views(214)  Comments(0Edit  收藏  举报