题解
2025-06-30 11:11:12
发布于:浙江
2阅读
0回复
0点赞
#include <stdio.h>
#include <stdlib.h>
int main() {
char input[10000];
fgets(input, sizeof(input), stdin);
int scores[10000];
int count = 0;
int num = 0;
int sign = 1;
int in_number = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] == '[') {
continue;
} else if (input[i] == ']') {
if (in_number) {
scores[count++] = sign * num;
in_number = 0;
num = 0;
sign = 1;
}
} else if (input[i] == ',') {
if (in_number) {
scores[count++] = sign * num;
in_number = 0;
num = 0;
sign = 1;
}
} else if (input[i] == ' ') {
continue;
} else if (input[i] == '-') {
sign = -1;
in_number = 1;
} else {
in_number = 1;
num = num * 10 + (input[i] - '0');
}
}
if (count == 0) {
printf("0\n");
return 0;
}
int* candies = (int*)malloc(count * sizeof(int));
for (int i = 0; i < count; i++) {
candies[i] = 1;
}
for (int i = 1; i < count; i++) {
if (scores[i] > scores[i-1]) {
candies[i] = candies[i-1] + 1;
}
}
for (int i = count - 2; i >= 0; i--) {
if (scores[i] > scores[i+1] && candies[i] <= candies[i+1]) {
candies[i] = candies[i+1] + 1;
}
}
int total = 0;
for (int i = 0; i < count; i++) {
total += candies[i];
}
printf("%d\n", total);
free(candies);
return 0;
}
这里空空如也
有帮助,赞一个