{"title":"小鱼游泳距离计算 - C++代码实现","description":"编写C++代码,计算一只小鱼在给定起始周和天数的情况下,累计游泳的总距离。小鱼每天游泳150公里(工作日)或100公里(周末),并休息周六和周日。","keywords":"小鱼游泳,距离计算,C++代码,循环,周末休息,算法","content":"#include "iostream"\n\nint main() {\n int x, n;\n std::cin >> x >> n;\n\n int totalDistance = 0;\n int distance;\n\n // 根据起始周x,确定小鱼在第一天的游泳距离\n if (x >= 1 && x <= 5) {\n distance = 150;\n } else if (x == 6 || x == 7) {\n distance = 100;\n }\n\n totalDistance += distance;\n\n // 循环计算每一天的游泳距离,并累加到总的游泳距离中\n for (int i = 1; i < n; i++) {\n // 周末休息\n if ((x + i) % 7 == 6 || (x + i) % 7 == 0) {\n continue;\n }\n\n // 根据每一天的游泳距离累加到总的游泳距离中\n totalDistance += distance;\n }\n\n std::cout << totalDistance << std::endl;\n\n return 0;\n}"}

小鱼游泳距离计算 - C++代码实现

原文地址: https://www.cveoy.top/t/topic/qztG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录