BUPT-homework/pset10/2-bullet.c
2021-12-01 21:56:58 +08:00

62 lines
1.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#define LEN 100
//调整函数
void distribute(int *bullets, int size, int number);
int main() {
int bullets[LEN];
int n, m, i;
scanf("%d", &n); //读入战士总数
for (i = 0; i < n; i++) {
scanf("%d", &bullets[i]); //读入每个战士手中初始的子弹数
}
scanf("%d", &m); //读入调整的次数m>0)
distribute(bullets, n, m); //调整
for (i = 0; i < n - 1; i++) //输出调整后结果
{
printf("%d ", bullets[i]);
}
printf("%d\n", bullets[i]);
return 0;
}
/* 请在这里填写答案 */
void distribute(int *bullets, int size, int number) {
// Base case 1: every bullets are distributed
int equal = 1;
for (int i = 0; i < size - 1 && equal != 0; i++) {
if (bullets[i] != bullets[i + 1]) {
equal = 0;
}
}
if (equal == 1) {
return;
} else if (number == 0) { // Base case 2: number is zero
return;
} else { // Base case 3: not distributed
int add[LEN] = {};
for (int i = 0; i < size; i++) {
// Redistribute
if (bullets[i] % 2 == 1)
bullets[i] = (bullets[i] + 1) / 2;
else
bullets[i] = bullets[i] / 2;
if (i == size - 1) {
add[0] += bullets[i];
} else {
add[i + 1] += bullets[i];
}
}
for (int i = 0; i < size; i++) {
bullets[i] += add[i];
}
distribute(bullets, size, number - 1);
return;
}
}