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 1. On or after day 1, when is the first day on which he has to take K pills or less?c++代码
时间: 2024-04-08 17:35:24 浏览: 91
Sure! Here's a C++ code snippet that calculates the first day on which Takahashi has to take K pills or less:
```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 = 1;
int remainingPills = 0;
for (int i = 0; i < N; i++) {
remainingPills += pills[i];
if (remainingPills <= K) {
std::cout << day << std::endl;
return 0;
}
day++;
remainingPills -= K;
}
day += (remainingPills / K) + 1;
std::cout << day << std::endl;
return 0;
}
```
In this code, we first take the inputs N and K, representing the number of medicine types and the maximum number of pills Takahashi can take in a day. Then we take the array of pills for each medicine type.
We iterate through the array and keep track of the remaining pills. If at any point the remaining pills are less than or equal to K, we output the current day and exit the program. Otherwise, we increment the day counter and reduce the remaining pills by K.
Finally, if there are still remaining pills, we calculate the additional days needed by dividing the remaining pills by K and adding 1 to account for the leftover pills. The result is the day on which Takahashi finishes taking all the prescribed medicine while taking K pills or less each day.
Please note that this code assumes valid inputs and does not include any error handling.
阅读全文