解题思路:
注意事项:
参考代码: public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { //此处的hasNext很重要(如果最终显示为答案错误 50,很可能漏了hasNext)
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = new int[n];
arr[0] = 2;
int temp = 0;
int average1;
int average2;
for (int i = 1; i < n; i++) {
arr[i] = arr[i - 1] + 2;
}
for (int j = 0; j < n; j = j + m) {
if (n - j >= m) {
for (int k = 0; k < m; k++) {
temp = temp + arr[j + k];
}
average1 = temp / m;
temp = 0; //一定要记得重置临时变量
System.out.print(average1 + " ");
} else {
for (int k = 0; k <= n - j - 1; k++) {
temp = temp + arr[j + k];
}
average2 = temp / (n - j);
System.out.print(average2 + " ");
}
}
}
}}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:683 |
Hello, world! (C语言代码)浏览:916 |
矩形面积交 (C语言代码)浏览:1433 |
简单的a+b (C语言代码)浏览:542 |
1231题解(注意理解“输入多个测试实例”)浏览:830 |
1202题解浏览:689 |
三角形 (C语言代码)浏览:722 |
用筛法求之N内的素数。 (C语言代码) 详解………………浏览:1195 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:487 |
WU-C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:811 |