C++实验1熟悉 Visual Studio 集成开发环境

1 实验目的

复习程序设计的基础知识,熟悉 Visual Studio 的使用方法。

2 实验内容

请编写以下 2 个程序。
2.1 Vedic Square and Vedic Star
(1)Problem Description
In ancient Indian mathematics, a Vedic square is a variation on a typical 9×9
multiplication table. The entry in each cell is the digital root of the product of the
column and row headings.
Vedic Square 是一个 9×9 的表,与九九乘法表类似。只是表的每个格不是行
列序号的乘积,而是乘积的数字根。下图是 Vedic Square。
C++实验1熟悉 Visual Studio 集成开发环境_第1张图片

(2)Vedic Star
By replacing a specific number by asterisk whereas the others by space within the
Vedic square, you can find some distinct shapes each with some form of reflection
symmetry.
(3)Output
You are to print the Vedic square first, and then the shapes of every specific number. An example of the shape of number 1 is as follows.
C++实验1熟悉 Visual Studio 集成开发环境_第2张图片
2.2 Elevator
(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)

3.源代码

2.1 Vedic Square and Vedic Star


#include 
using namespace std;

/* This function returns the sum of all the digits of the integer num. */
int sumDigits(int num) {
	int sum = 0;
	while (num != 0) {
		sum += num % 10;
		num /= 10;
	}
	return sum;
}

/* This function returns the digital root of the integer num. */
int digitalRoot(int num) {
	while (num > 9)
		num = sumDigits(num);
	return num;
}

void getVedicTable(int vedicTable[][9]) {
	for (int i = 0; i < 9; i++)
		for (int j = 0; j < 9; j++)
			vedicTable[i][j] = digitalRoot((i + 1) * (j + 1));
}

void printVedicTable(int vedicTable[][9]) {
	// 打印行标题
	cout << "   | ";
	for (int j = 0; j < 9; j++)
		cout << " " << j << " ";
	cout << endl << "---|-----------------------------" << endl;

	// 打印每一行
	for (int i = 0; i < 9; i++) {
		// 打印列标题
		cout << " " << i + 1 << " | ";

		// 打印每列的内容
		for (int j = 0; j < 9; j++)
			cout << " " << vedicTable[i][j] << " ";

		cout << endl; // 换行,准备打印下一行
	}
}

void printVedicStar(int vedicTable[][9], int digit) {
	//int bestPartner = 10 - digit;
	// 打印每个星座的标题
	cout << endl << "---------------" << digit << "---------------" << endl;

	// 打印星座
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 9; j++) {
			if (vedicTable[i][j] == digit/* || vedicTable[i][j] == bestPartner*/)
				cout << " * ";
			else
				cout << "   ";
		}
		cout << endl;
	}
}

int main() {
	int aVedicTable[9][9]; // Vedic Table, 二维数组

	getVedicTable(aVedicTable); // 生成Vedic Table

	printVedicTable(aVedicTable); // 打印Vedic Table

	// 打印Vedic Star,从1到9
	for (int i = 1; i <= 9; i++)
		printVedicStar(aVedicTable, i);

	return 0;
}


2.2 Elevator

#include 
#include 
#include 
using namespace std;

int main() {
	int next, current = 0, numbers, temp, time = 0;
	int counter = 0;
	int values[20] = { 0 };
	string text[20][51];

	while (cin >> numbers && numbers != 0) {
		time = 0;
		current = 0;
		stringstream sStream;

		for (int i = 0; i < numbers; i++) {
			cin >> next;
			temp = next - current;
			if (temp > 0) {
				time += temp * 6 + 5;
				sStream << "6 * " << temp << " + 5";
			} else {
				time += (-temp) * 4 + 5;
				sStream << "4 * " << -temp << " + 5";
			}
			current = next;
			text[counter][i].append(sStream.str());
			sStream.str("");
		}
		values[counter] = time;
		counter++;
	}

	for (int i = 0; i < counter; i++) {
		cout << values[i] << " (";
		for (int j = 0; j < 50; j++) {
			cout << text[i][j];
			if (! text[i][j + 1].empty())
				cout << " + ";
		}
		cout << ")" << endl;
	}

	return 0;
}

你可能感兴趣的:(C++)