hdu2015偶数求和解题报告---等差数列求和

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/83993347

偶数求和

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

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

思路:

m个数的等比数列求和

a1 = v, am = v + 2 * (m - 1) 。

Sm / m = v + m - 1。

(不足m个数也同理)

Code:

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define INF 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
int main(){
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF){
        int k = n / m, t = n % m;
        int v = 2;
        for(int i = 1; i < k; i++){
            printf("%d ", v + m - 1);   //(a1 + am) * m / (2 * m) 
            v = v + 2 * m; //am+1
        }
        printf("%d", v + m - 1);
        v = v + 2 * m;
        if(t != 0) printf(" %d", v + t - 1);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/83993347