diff --git a/pset10/sort.c b/pset10/1-sort.c similarity index 100% rename from pset10/sort.c rename to pset10/1-sort.c diff --git a/pset10/2-bullet.c b/pset10/2-bullet.c new file mode 100644 index 0000000..199bc60 --- /dev/null +++ b/pset10/2-bullet.c @@ -0,0 +1,61 @@ +#include + +#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; + } +}