update 1 and 2

This commit is contained in:
juan 2021-12-01 21:56:58 +08:00
parent ceb39acadb
commit 170f2e3624
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
2 changed files with 61 additions and 0 deletions

61
pset10/2-bullet.c Normal file
View file

@ -0,0 +1,61 @@
#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;
}
}