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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    GDI绘制时钟效果,与系统时间保持同步,基于Winform
    Asp.Net Core API网关Ocelot
    Docker打包 Asp.Net Core应用,在CentOS上运行
    【C#】数据库脚本生成工具(二)
    【C#附源码】数据库文档生成工具支持(Excel+Htm)
    微信小程序初使心得【微信小程序快速入门】
    论:开发者信仰之“天下IT是一家“(Java .NET篇)
    线程池,千万注意,原来很多人都在错用
    .NET跨平台之运行与Linux上的Jexus服务器
    StackExchange.Redis 之 SortedSet 类型示例
  • 原文地址:https://www.cnblogs.com/lightac/p/10534732.html
Copyright © 2011-2022 走看看