zoukankan      html  css  js  c++  java
  • 面向对象的程序设计_第一次作业 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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    ajaxUtil
    AJAX学习-----与 XML 文件进行交互式通信
    AJAX-----数据库实例
    AJAX学习-----服务器响应
    AJAX学习-----ASP/PHP 请求实例
    scrapy 反扒措施
    scrapy 中文官网学习
    scrapy 破解图片网站防盗链下载
    scrapy 随机中间件配置
    flask 邮件发送
  • 原文地址:https://www.cnblogs.com/lightac/p/10534732.html
Copyright © 2011-2022 走看看