Snuke the doctor prescribed N kinds of medicine for Takahashi. For the next a i days (including the day of the prescription), he has to take b i pills of the i-th medicine. He does not have to take any other medicine. Let the day of the prescription be day On or after day 1, when is the first day on which he has to take K pills or less? please make a C++ code for me
时间: 2024-04-08 15:35:23 浏览: 94
下一天C/C++ 源代码
Sure! Here's a C++ code that solves the problem you described:
```cpp
#include <iostream>
#include <vector>
int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> pills(n);
for (int i = 0; i < n; ++i) {
std::cin >> pills[i];
}
int day = 0;
for (int i = 0; i < n; ++i) {
day += pills[i];
if (day <= k) {
std::cout << i + 1 << std::endl;
return 0;
}
}
std::cout << n + (day - k) / k << std::endl;
return 0;
}
```
In this code, we first read the values of `n` and `k` from the user. `n` represents the number of kinds of medicine and `k` represents the maximum number of pills Takahashi can take in a day.
Then, we read the number of pills for each medicine into a vector `pills`.
We iterate over the medicines and accumulate the number of pills each day. If at any point the accumulated number of pills is less than or equal to `k`, we print the current day and exit the program.
If no such day is found, we calculate the remaining number of pills after `n` days and divide it by `k` to find the number of extra days needed. We print the result.
Please note that this code assumes valid input and does not perform any input validation.
阅读全文