c++ - 如何找到全轮最多的乘客?
问题描述
我是一个新的问题解决者。最近我在codeforces网站上遇到了这个问题。根据用户给出的转弯次数,我设法获得了每个转弯所需的两个值,但我无法找到停止时最多的乘客数量。
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int turn,get_off,get_on,total_passenger,highest_total;
cin>>turn;
int* stops=new int(turn);
for(int i=0;i<turn;i++){
cin>>get_off>>get_on;
total_passenger=get_on-get_off;
stops[i]=total_passenger;
}
for(int i=0;i<turn;i++){
if(stops[i]>stops[i+1]){
highest_total=stops[i];
}
}
cout<<highest_total<<endl;
return 0;
}
解决方案
您可以尝试使用https://en.cppreference.com/w/cpp/algorithm/max_element 也可以将乘客总数设置为下车和上车之间的差异,我认为您应该添加该差异并分配int* 的内存使用 [] 而不是 ()
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int turn,get_off,get_on,total_passenger,highest_total;
cin>>turn;
int* stops=new int[turn];
for(int i=0;i<turn;i++){
cin>>get_off>>get_on;
total_passenger+=get_on-get_off;
stops[i]=total_passenger;
}
cout<<std::max_element(stops, stops + turn)<<endl;
}
推荐阅读
- xml - 对于给定的 XML 文件,此 DTD 是否错误
- intellij-idea - 类调用其他类的方法
- reactjs - 在 react-redux connect() 中,connected-react-router push() 没有位置变化
- c++ - 非 constexpr 变量模板是否具有零开销?
- r - 计算数据框中数字的频率
- hadoop - HADOOP 上的 HBASE 分布式模式卡住了
- spring-boot - Spring Boot 2.0.4 - H2 数据库 - @SpringBootTest - 失败原因:org.h2.jdbc.JdbcSQLException:找不到架构“classpath:db/schema.sql”
- bash - bash 脚本读取用户输入并将其导出为变量
- dynamics-crm - 使用实体元数据早期绑定调用 Dynamics Web API
- c# - C#,具有许多顺序逻辑的少数任务或具有很少顺序逻辑的许多任务?