ACM HDU 2015
偶数求和
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 91317 Accepted Submission(s): 38494
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
两种方法:
1:按正常思路来
#include <stdio.h> int main() { int n, m, i, sum, num, num2, j, s,s1; while (scanf_s("%d%d", &n, &m) != EOF) { sum = 0; int a[10001]; s1=num = n / m; s = num2 = n%m; j = 0; for (i = 2; i <= n * 2; i += 2, j++) a[j] = i; j = 0; while (num--) { for (i = 0; i < m; i++) { sum += a[j]; j++; } printf("%d", sum / m); if (num>0) printf(" "); sum = 0; } if (num2 == 0) printf("\n"); else { while (num2--) { sum += a[j]; j++; } if (sum != 0 && s == 0) printf(" %d\n", sum / (s + 1)); if (sum != 0 && s != 0) printf(" %d\n", sum / s); } } return 0; }
2:简单很多,主要我也不知道我咋想到的!!!
#include <stdio.h> int main(void) { int a, i, n, m, b, s; while (scanf("%d %d", &n, &m) != EOF && n<=100) { a = 2; b = n%m; s = n / m; for (i = 0; i < s; i++) { printf("%d", a + m - 1); if (i < s - 1) printf(" "); a = a + m * 2; } if (b != 0) { printf(" %d", a); } printf("\n"); } return 0; }