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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    安装pyamf碰到的问题及其解决方法^_^(原创)
    django建立模型的一些体会(原创)
    终于搞完第三个作业了~~
    django传递嵌套对象给flex前端的方法(原创)
    flex:在一个.as文件中调用另一个.as文件的public函数(原创)
    hdu(4339)树状数组+二分查找
    1504: ZZ的橱柜(长沙理工oj)
    zoj(2110)Tempter of the Bone(DFS+奇偶剪枝)
    hdu(4267)A Simple Problem with Integers(三维树状数组)
    [USACO 1.5.3]特殊的质数肋骨
  • 原文地址:https://www.cnblogs.com/lightac/p/10534231.html
Copyright © 2011-2022 走看看