偶数求和
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 88210 Accepted Submission(s): 37175
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
Author
lcy
Source
Recommend
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include <set>
#include<algorithm>
#include <limits.h>
#include <ctype.h>
#include <map>
#include <stack>
typedef long long LL;
using namespace std;
const int N = 105;
int data[N][N];
int dp[N][N];
int f[35];
char c[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
float obj[5];
void fun(int n, int m)
{
int flag = 0;
if(n < 0){
flag = 1;
n = -n;
}
stack<char> s1;
while(n != 0){
int yushu = n % m;
if(yushu >= 10){
s1.push(c[yushu - 10]);
}else {
s1.push(yushu + '0');
}
n = n / m;
}
if(flag){
cout << "-";
}
while(!s1.empty()){
cout << s1.top();
s1.pop();
}
cout << endl;
}
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
int lcm(int a, int b)
{
return a / gcd(a, b) * b;
}
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
// int n, sum;
// while(scanf("%d %d", &n, &sum) != EOF){
// if(n == 0 && sum == 0) break;
// for(int i = 1; i <= n; i++){
// int T = 0;
// for(int j = i; j <= n; j++){
// T += j;
// if(T == sum ){
// cout << "["<< i << "," << j <<"]"<< endl;
// break;
// }else if(T < sum){
// continue;
// }else {
// break;
// }
// }
// }
// cout << endl;
// }
int n, m;
while(scanf("%d %d", &n,&m) != EOF){
int k = n / m;
int ans = 0, temp = 2;
for(int i = 0; i < k; i++){
int ans = 0;
for(int j = 0; j < m; j++){
ans += temp;
temp += 2;
}
if(i == 0)
cout << ans / m;
else {
cout << " "<< ans / m ;
}
}
ans = 0;
if(n % m != 0){
for(int i = 0; i < n % m; i++){
ans += temp;
temp += 2;
}
cout << " " <<ans / (n % m);
}
cout << endl;
}
return 0;
}