• 面向对象的程序设计_第一次作业 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. 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;
    	}
    }
    
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    模板引擎art-template怎么安装?
    关于vue中如何监听数组变化
    vue开发中的几个高级应用
    关于Mock.js使用
    F和Q:
    聚合和分组:
    html的或替换:
    空行替换: 替换为 :
    orm的操作:
    Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
  • 原文地址:https://www.cnblogs.com/lightac/p/10534732.html
Copyright © 2020-2023  润新知