面向对象的程序设计_第一次作业 3月12日
问题一(数字根问题)
1 #include <iostream> 2 #include <iomanip> 3 4 using namespace std; 5 6 int roots(int num) { 7 int res = 0; 8 while (num) { 9 res += num % 10; 10 num /= 10; 11 } 12 if (res / 10 == 0) 13 return res; 14 else 15 return roots(res); 16 } 17 18 int main() 19 { 20 int det[10][10]; 21 cout << " " << "|" << " "; 22 for (int i = 1; i < 10; ++i) 23 cout << i << " "; 24 cout << endl; 25 cout << "—"; 26 for (int i = 1; i < 23; ++i) 27 cout << "—"; 28 cout << endl; 29 for (int i = 1; i < 10; ++i) { 30 cout << i << " " << '|' << " "; 31 for (int j = 1; j < 10; ++j) { 32 det[i][j] = roots(i * j); 33 cout << setw(5) << left << roots(i * j); 34 } 35 cout << endl << endl; 36 } 37 cout << "请输入一个数字" << endl; 38 int num = 0; 39 while (cin >> num) { 40 cout << " " << "|" << " "; 41 for (int i = 1; i < 10; ++i) 42 cout << i << " "; 43 cout << endl; 44 cout << "—"; 45 for (int i = 1; i < 23; ++i) 46 cout << "—"; 47 cout << endl; 48 for (int i = 1; i < 10; ++i) { 49 cout << i << " " << '|' << " "; 50 for (int j = 1; j < 10; ++j) { 51 if (det[i][j] == num) 52 cout << setw(5) << "*"; 53 else 54 cout << setw(5) << " "; 55 } 56 cout << endl << endl; 57 } 58 cout << "请输入一个数字" << endl; 59 } 60 return 0; 61 }
问题二(电梯问题)
(1)Problem Description The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop. For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
(2)Input There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
(3)Output Print the total time on a single line for each test case.
(4)Sample Input
1 2
3 2 3 1
0
(5)Sample Output
17 (6 * 2 + 5)
41 (6 * 2 + 5 + 6 * 1 + 5 + 4 * 2 + 5)
#include <iostream> using namespace std; int main() { int num = 0; cin >> num; while (num) { int floor = 0; int before = 0; char step[1000] = { '0' }; step[0] = '('; int j = 1; int res = 0; for (int i = 0; i < num; ++i) { cin >> floor; int differ = floor - before; before = floor; if (i) { step[j++] = ' '; step[j++] = '+'; step[j++] = ' '; } if (differ > 0) { step[j++] = '6'; step[j++] = ' '; step[j++] = '*'; step[j++] = ' '; step[j++] = differ + '0'; res += 6 * differ; } else { step[j++] = '4'; step[j++] = ' '; step[j++] = '*'; step[j++] = ' '; step[j++] = -differ + '0'; res += 4 * -differ; } step[j++] = ' '; step[j++] = '+'; step[j++] = ' '; step[j++] = '5'; res += 5; } step[j] = ')'; cout << res << step << endl; cin >> num; } }
出处:https://www.cnblogs.com/lightac/
联系:
Email: dzz@stu.ouc.edu.cn
QQ: 1171613053
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络