W-平均值序列

有一个长度为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 <iostream>
#include <cstdio>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main() {
    int arr[105];
int n,m,dex;
int  con,sum;
dex=2;sum=0;con=0;

while(scanf("%d%d",&n,&m)!=EOF)
{
   dex=0;con=0;sum=0;
   memset(arr,0,n);         //数组清零
  for(int i=1;i<=n;i++)  //数组赋值
  {
    dex+=2;
    arr[i]=dex; 
  }
  for(int i=1;i<=n;i++)
  {
        con++;
   sum+=arr[i];
      if(con==m)
      {
          printf("%d",sum/m);
     if(i!=n)printf(" ");
     sum=0;
        con=0;
               } 
     if(i==n&con!=0)
      printf("%d",sum/con); 
    }
printf("\n");

    }
return 0;
}

猜你喜欢

转载自blog.csdn.net/lioncatch/article/details/80645147
今日推荐