网站介绍流程,网站建设及托管合同模板,外包推广服务,什么网站做任务目录 一、小C的外卖超时判断
问题描述
测试样例 解题思路#xff1a;
问题理解
数据结构选择
算法步骤
最终代码#xff1a; 运行结果#xff1a;
二、小C的排列询问
问题描述
测试样例
最终代码#xff1a;
运行结果#xff1a; 编辑 一、小C的外卖超时判断…目录 一、小C的外卖超时判断
问题描述
测试样例 解题思路
问题理解
数据结构选择
算法步骤
最终代码 运行结果
二、小C的排列询问
问题描述
测试样例
最终代码
运行结果 编辑 一、小C的外卖超时判断
问题描述
小C点了一个外卖并且急切地等待着骑手的送达。她想知道她的外卖是否超时了。
已知小C在时刻 t1 点了外卖外卖平台上显示的预计送达时间为 t2而实际送达时间为 t3。需要判断外卖是否超时。如果外卖超时则输出 Yes否则输出 No。
t3 在 t2 之后则认定为超时。
实际送达时间与预计送达时间在 2 小时之内。 测试样例
示例 1 输入t1 18:00, t2 19:05, t3 19:05 输出No 示例 2 输入t1 23:00, t2 00:21, t3 00:23 输出Yes 示例 3 输入t1 23:05, t2 00:05, t3 23:58 输出No 解题思路
问题理解
我们需要判断外卖是否超时。具体来说如果实际送达时间 t3 在预计送达时间 t2 之后则认定为超时。
数据结构选择
由于输入的时间是字符串格式例如 18:00我们需要将这些时间转换为可以比较的格式。一种常见的方法是将时间转换为分钟数从当天的00:00开始计算。
算法步骤
解析时间字符串将 t1, t2, t3 解析为小时和分钟。转换为分钟数将小时和分钟转换为从当天00:00开始的分钟数。比较时间比较 t3 和 t2 的分钟数。如果 t3 大于 t2则输出 Yes否则输出 No。
最终代码
#include iostream
#include stringusing namespace std;std::string solution(const std::string t1, const std::string t2, const std::string t3) {// 解析时间字符串int t1_hour std::stoi(t1.substr(0, 2));int t1_minute std::stoi(t1.substr(3, 2));int t2_hour std::stoi(t2.substr(0, 2));int t2_minute std::stoi(t2.substr(3, 2));int t3_hour std::stoi(t3.substr(0, 2));int t3_minute std::stoi(t3.substr(3, 2));// 转换为分钟数int t1_minutes t1_hour * 60 t1_minute;int t2_minutes t2_hour * 60 t2_minute;int t3_minutes t3_hour * 60 t3_minute;// 处理跨天情况if (t2_minutes t1_minutes) {t2_minutes 24 * 60; // 加上一天的分钟数}if (t3_minutes t1_minutes) {t3_minutes 24 * 60; // 加上一天的分钟数}// 计算时间差int t2_diff t2_minutes - t1_minutes;int t3_diff t3_minutes - t1_minutes;// 比较时间差if (t3_diff t2_diff) {return No;} else {return Yes;}
}int main() {std::cout (solution(18:00, 19:05, 19:05) No) std::endl;std::cout (solution(23:00, 00:21, 00:23) Yes) std::endl;std::cout (solution(23:05, 00:05, 23:58) No) std::endl;return 0;
} 运行结果 二、小C的排列询问
问题描述
小C拿到了一个排列她想知道在这个排列中元素 xx 和 yy 是否是相邻的。排列是一个长度为 nn 的数组其中每个数字从 11 到 nn 恰好出现一次。
你的任务是判断在给定的排列中xx 和 yy 是否是相邻的。 测试样例
样例1 输入n 4, a [1, 4, 2, 3], x 2, y 4 输出True 样例2 输入n 5, a [3, 4, 5, 1, 2], x 3, y 2 输出False 样例3 输入n 6, a [6, 1, 5, 2, 4, 3], x 5, y 2 输出True 最终代码
#include iostream
#include vector
using namespace std;bool solution(int n, vectorint a, int x, int y) {// 遍历数组for (int i 0; i n - 1; i) {// 检查当前元素和下一个元素是否分别是 x 和 y或者 y 和 xif ((a[i] x a[i 1] y) || (a[i] y a[i 1] x)) {return true;}}// 如果没有找到相邻的 x 和 y返回 falsereturn false;
}int main() {cout (solution(4, {1, 4, 2, 3}, 2, 4) true) endl;cout (solution(5, {3, 4, 5, 1, 2}, 3, 2) false) endl;cout (solution(6, {6, 1, 5, 2, 4, 3}, 5, 2) true) endl;return 0;
}
运行结果 文章转载自: http://www.morning.gwdkg.cn.gov.cn.gwdkg.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.yjknk.cn.gov.cn.yjknk.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn