面向对象的程序设计_第一次作业 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;
	}
	cout << 0 << endl;
}

ps:+ '0’实现 char 与 int 的转化

猜你喜欢

转载自blog.csdn.net/qq_43591782/article/details/88430882