C++ 代码查找电视观看一场比赛的时间
假设我们有一个包含 n 个元素的数组 A。Amal 想观看一场 90 分钟的比赛,且没有任何休息。每分钟可能是精彩或者无聊。如果连续 15 分钟是无聊的,则 Amal 会立即关闭电视。数组 A 中有 n 分钟精彩比赛。我们需要计算 Amal 观看比赛的时间。
因此,如果输入像 A = [7, 20, 88],则输出将为 35,因为在 20 分钟后,他仍然会观看比赛直到 35 分钟,然后关闭电视。
步骤
要解决此问题,我们将按照以下步骤操作 −
Define an array a of size: 100. n := size of A for initialize i := 1, when i <= n, update (increase i by 1), do: a[i] := A[i - 1] if a[i] - a[i - 1] > 15, then: Come out from the loop return minimum of (a[i - 1] + 15) and 90
例子
让我们看看以下实现以获得更好的理解 −
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A){
int i, a[100];
int n = A.size();
for (i = 1; i <= n; i++){
a[i] = A[i - 1];
if (a[i] - a[i - 1] > 15)
break;
}
return min(a[i - 1] + 15, 90);
}
int main(){
vector<int> A = { 7, 20, 88 };
cout << solve(A) << endl;
}输入
{ 7, 20, 88 }输出
35
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP