zoukankan      html  css  js  c++  java
  • 面向对象的程序设计_第一次作业 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;
        }
    }
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    linux mono环境
    【百度杯】ctf夺旗大战,16万元奖励池等你拿
    【渗透技术】渗透测试技术分析_TomCat
    成功率“99%”!截止目前史上最强大电信诈骗术
    【sql注入】浅谈sql注入中的Post注入
    通过Weeman+Ettercap配合拿下路由器管理权限
    【sql注入教程】mysql注入直接getshell
    【云盘资料】Sql注入从菜鸟到高手系列教程
    【安全开发】浅谈JSP安全开发之XSS
    Python黑帽编程2.1 Python编程哲学
  • 原文地址:https://www.cnblogs.com/lightac/p/10534231.html
Copyright © 2011-2022 走看看