C++ 代码用于计算完成书籍阅读所需的天数
假设我们有一个带有 n 个元素的数组 A,并且有另一个值 t。第 i 天,阿马尔花 A[i] 秒工作。在空闲时间,他会读书。整本书需要 t 秒才能完成。我们必须找出他需要多少天才能读完整本书。
因此,如果输入为 A = [86400, 86398]; t = 2,则输出将为 2,因为一天有 86400 秒,而第一天完全被阻塞。在第二天,他将获得 2 秒来完成这本书。
步骤
为了解决这个问题,我们将按照以下步骤进行 −
cnt := 1 n := size of A for initialize i := 0, when i < n, update (increase i by 1), do: x := A[i] t := t - 86400 - x if t <= 0, then: return cnt (increase cnt by 1)
示例
让我们看看以下实现以获得更好的理解 −
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A, int t){
int cnt = 1;
int n = A.size();
for (int i = 0; i < n; i++){
int x = A[i];
t -= 86400 - x;
if (t <= 0){
return cnt;
}
++cnt;
}
}
int main(){
vector<int> A = { 86400, 86398 };
int t = 2;
cout << solve(A, t) << endl;
}输入
{ 86400, 86398 }, 2输出
2
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP