面向对象的程序设计_第一次作业 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/p/10534231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值