面向对象的程序设计_第一次作业 3月12日
问题一(数字根问题)
#include <iostream> #include <iomanip> using namespace std; int roots(int num) { int res = 0; while (num) { res += num % 10; num /= 10; } if (res / 10 == 0) return res; else return roots(res); } int main() { int det[10][10]; cout << " " << "|" << " "; for (int i = 1; i < 10; ++i) cout << i << " "; cout << endl; cout << "—"; for (int i = 1; i < 23; ++i) cout << "—"; cout << endl; for (int i = 1; i < 10; ++i) { cout << i << " " << '|' << " "; for (int j = 1; j < 10; ++j) { det[i][j] = roots(i * j); cout << setw(5) << left << roots(i * j); } cout << endl << endl; } cout << "请输入一个数字" << endl; int num = 0; while (cin >> num) { cout << " " << "|" << " "; for (int i = 1; i < 10; ++i) cout << i << " "; cout << endl; cout << "—"; for (int i = 1; i < 23; ++i) cout << "—"; cout << endl; for (int i = 1; i < 10; ++i) { cout << i << " " << '|' << " "; for (int j = 1; j < 10; ++j) { if (det[i][j] == num) cout << setw(5) << "*"; else cout << setw(5) << " "; } cout << endl << endl; } cout << "请输入一个数字" << endl; } return 0; }
问题二(电梯问题)
(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
2d345
. 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; } }
ps:+ '0’实现 char 与 int 的转化
输入完一起出结果版
#include <iostream> using namespace std; int main() { int ans[1000]; int k = 0; int num = 0; cin >> num; char step[1000][1000] = { '0' }; while (num) { int floor = 0; int before = 0; for(int i = 0; i < 1000; ++i) step[i][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[k][j++] = ' '; step[k][j++] = '+'; step[k][j++] = ' '; } if (differ > 0) { step[k][j++] = '6'; step[k][j++] = ' '; step[k][j++] = '*'; step[k][j++] = ' '; step[k][j++] = differ + '0'; res += 6 * differ; } else { step[k][j++] = '4'; step[k][j++] = ' '; step[k][j++] = '*'; step[k][j++] = ' '; step[k][j++] = -differ + '0'; res += 4 * -differ; } step[k][j++] = ' '; step[k][j++] = '+'; step[k][j++] = ' '; step[k][j++] = '5'; res += 5; } step[k][j] = ')'; ans[k] = res; k++; cin >> num; } for (int i = 0; i < k; ++i) { cout << ans[i] << step[i] << endl; } }
- 大三学长带我学习JAVA.作业4.流程控制语句.续.Flow.Control.Statement.Cont 理解面向对象程序设计 学长带我学java的作业4
- 第一次C程序设计作业
- 第一次C程序设计课后作业
- 面对对象程序设计第一次作业
- 第一次C程序设计作业
- java 源代码计算器。面向对象教程第一次作业
- 数据结构第一次作业:程序设计技术规则
- 第一次面向对象程序设计作业-大一下学期的自我目标
- 第一次c程序设计作业
- 第一次面向对象程序设计作业
- 第一次C程序设计作业
- 第一次C程序设计作业
- 第一次程序设计作业
- 第一次面向对象程序设计作业-大一下学期的自我目标
- 第一次C程序设计作业
- 第一次程序设计作业《三天打鱼两天晒网》
- 哈工大深研院数字图像处理第一次大作业:不调用Matlab现有库函数实现图像增强
- 推荐学习面向对象程序设计的同学阅读:面向对象的思考过程 一书
- 程序设计作业项目二
- 第一次编程作业修改