偶数求和
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 95563 Accepted Submission(s): 40086
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
import java.util.Scanner;
public class P2015 {
private static Scanner scanner;
public static void main(String[] args) {// 2 4 ,6 8 ,10
scanner = new Scanner(System.in);
while(scanner.hasNext()){
int n = scanner.nextInt();
int m = scanner.nextInt();
int v = 0,s = 0,flag = 0;
for(int i = 1;i<=n;i++){//1 2 3 4
s = s+i*2;//
v++;
if(v == m){//每m个
if(flag == 0){
System.out.print(s/m);
flag = 1;
}else {
System.out.print(" "+s/m);
}
v = 0;
s = 0;
}else if(i == n) {//到达末尾
System.out.print(" "+s/(n%m));
}
}
System.out.println();
}
}
}