鱼C论坛

 找回密码
 立即注册
查看: 2122|回复: 4

[已解决]求助一下

[复制链接]
发表于 2023-10-13 18:47:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
NEUQer同学是今年将要大学入学的准新生,在知道我们的新生编程训练营后就计划参加训练营学习编程,为了夯实指法基础,还参加了NEUQer指法训练营第四期。他了解到计算机专业对英语基础要求很高,所以还每天计划学习英语。因此,在开学前的这一个月内,他每天的学习任务是英语、指法和编程。为了了提高学习效率,他计划早6点前晚6点后在家里休息,早上6点至7点、中午12点至1点、下午5点至6点是每天的就餐时间,其余时间在分配在三项学习任务上。

请问你知道NEUQer现在在做什么吗?

(本题要求不使用 if语句和switch语句)

输入格式:
一行中给出当天的一个时间点,形如:HH:MM:SS,HH表示小时,MM表示分,SS表示秒,全天时间采用24小时制表示。

输出格式:
根据不同情况,输出一行文本,确定在休息输出:resting;确定在就餐时段则输出:eating;确定在学习状态就输出:studying;两段时间交接处不确定在干什么时输出:on the way。

输入样例1:
20:10:20
输出样例1:
resting
输入样例2:
06:00:00
输出样例2:
on the way
输入样例3:
08:00:00
输出样例3:
studying
输入样例4:
17:30:00
输出样例4:
eating

这个题用c语言或者c++要怎么写,谢谢大家
最佳答案
2023-10-13 18:47:50
这道题可以使用字符串的比较来解决,具体思路如下:

1. 将输入的时间字符串按照小时、分钟、秒分别存储到三个变量中。

2. 根据NEUQer的学习计划,将一天的时间分为三个时间段:早上6点至7点、中午12点至1点、下午5点至6点为就餐时间,其余时间为学习时间,早6点前晚6点后为休息时间。

3. 根据当前时间判断NEUQer的状态,输出对应的结果。

代码如下:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-13 18:47:50 | 显示全部楼层    本楼为最佳答案   
这道题可以使用字符串的比较来解决,具体思路如下:

1. 将输入的时间字符串按照小时、分钟、秒分别存储到三个变量中。

2. 根据NEUQer的学习计划,将一天的时间分为三个时间段:早上6点至7点、中午12点至1点、下午5点至6点为就餐时间,其余时间为学习时间,早6点前晚6点后为休息时间。

3. 根据当前时间判断NEUQer的状态,输出对应的结果。

代码如下:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-13 18:48:37 | 显示全部楼层
这个问题可以通过比较时间来解决。首先,我们需要将输入的时间转换为秒,然后与预设的时间段进行比较。以下是一个使用C++编写的示例代码:

  1. #include <iostream>
  2. #include <string>

  3. int main() {
  4.     std::string time;
  5.     std::cin >> time;

  6.     int hour = std::stoi(time.substr(0, 2));
  7.     int minute = std::stoi(time.substr(3, 2));
  8.     int second = std::stoi(time.substr(6, 2));

  9.     int total_seconds = hour * 3600 + minute * 60 + second;

  10.     int morning_start = 6 * 3600;
  11.     int morning_end = 7 * 3600;
  12.     int noon_start = 12 * 3600;
  13.     int noon_end = 13 * 3600;
  14.     int evening_start = 17 * 3600;
  15.     int evening_end = 18 * 3600;
  16.     int day_end = 18 * 3600;
  17.     int day_start = 6 * 3600;

  18.     if (total_seconds < day_start || total_seconds >= day_end) {
  19.         std::cout << "resting";
  20.     } else if ((total_seconds >= morning_start && total_seconds < morning_end) ||
  21.                (total_seconds >= noon_start && total_seconds < noon_end) ||
  22.                (total_seconds >= evening_start && total_seconds < evening_end)) {
  23.         if (total_seconds % 3600 == 0) {
  24.             std::cout << "on the way";
  25.         } else {
  26.             std::cout << "eating";
  27.         }
  28.     } else {
  29.         std::cout << "studying";
  30.     }

  31.     return 0;
  32. }
复制代码


这段代码首先读取输入的时间,并将其转换为小时、分钟和秒。然后,它将这些时间单位转换为总秒数。接着,它定义了早餐、午餐和晚餐的开始和结束时间,以及一天的开始和结束时间。

然后,代码检查当前时间是否在一天的开始和结束时间之外。如果是,那么输出\"resting\"。如果不是,它会检查当前时间是否在早餐、午餐或晚餐的时间段内。如果是,并且当前时间正好是小时的开始(即分钟和秒都为0),那么输出\"on the way\"。否则,输出\"eating\"。如果当前时间不在任何已定义的时间段内,那么输出\"studying\"。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-13 18:49:01 | 显示全部楼层
解答:NEUQer现在在做什么

可以使用C++编写以下代码来判断NEUQer当前的状态:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sstream>
  4. // 判断NEUQer当前状态的函数
  5. std::string getNEUQerStatus(const std::string& timeStr) {
  6.     // 切割时间字符串,获取小时、分、秒
  7.     std::istringstream iss(timeStr);
  8.     int hour, minute, second;
  9.     char delimiter;
  10.     if (!(iss >> hour >> delimiter >> minute >> delimiter >> second)) {
  11.         return "Invalid time format";
  12.     }
  13.     // 构建时间对应的秒数
  14.     int totalSeconds = hour * 3600 + minute * 60 + second;
  15.     // 定义学习、就餐时间段对应的秒数范围
  16.     int studyStart = 7 * 3600;      // 早上7点
  17.     int studyEnd = 12 * 3600;       // 中午12点
  18.     int meal1Start = 12 * 3600;     // 中午12点
  19.     int meal1End = 13 * 3600;       // 中午1点
  20.     int meal2Start = 17 * 3600;     // 下午5点
  21.     int meal2End = 18 * 3600;       // 下午6点
  22.     // 判断NEUQer的状态
  23.     if (totalSeconds < 6 * 3600 || totalSeconds >= 18 * 3600) {
  24.         return "resting";
  25.     }
  26.     else if (totalSeconds >= studyStart && totalSeconds < studyEnd) {
  27.         return "studying";
  28.     }
  29.     else if ((totalSeconds >= meal1Start && totalSeconds < meal1End) ||
  30.              (totalSeconds >= meal2Start && totalSeconds < meal2End)) {
  31.         return "eating";
  32.     }
  33.     else {
  34.         return "on the way";
  35.     }
  36. }
  37. int main() {
  38.     std::string timeStr;
  39.     std::cin >> timeStr;
  40.     std::string status = getNEUQerStatus(timeStr);
  41.     std::cout << status << std::endl;
  42.     return 0;
  43. }
复制代码

这段代码会根据输入的时间字符串判断NEUQer当前的状态,并输出相应的结果。使用getNEUQerStatus函数进行状态判断,并返回对应的字符串。主函数main中接受用户输入的时间字符串并调用函数完成状态判断,最后输出结果。

编译运行以上代码,输入时间后即可得到NEUQer当前的状态。

希望能帮到你!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-13 18:50:16 | 显示全部楼层
isdkz 发表于 2023-10-13 18:47
这道题可以使用字符串的比较来解决,具体思路如下:

1. 将输入的时间字符串按照小时、分钟、秒分别存储 ...

看不到代码呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 12:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表