c 偶数求和

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<stdio.h>
int main()
{
    int i,j,n,m,x=2,sum=0,avg=0,rej=0,times=0;
    while(scanf("%d",&n)!=EOF){       
    scanf("%d",&m);
    if(n%m==0){
      times = n/m;
    }else{
      times = (n/m)+1;
    }
      for(i=0;i<times;i++){
          for(j=0;j<m;j++){
            if(x>2*n){
              rej = m-j;
              break;
            }
              sum+=x;
              x+=2;
          }
          avg = sum/(m-rej);
          printf("%d",avg);
          if(i<times-1)printf(" ");
          sum = 0;
          avg = 0;
          rej = 0;
      }
      x = 2;
      printf("\n");
}
return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_40811682/article/details/88130860