1 实验目的
复习程序设计的基础知识,熟悉Visual Studio的使用方法。编写简单的C++程序。
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。
(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.
源代码如下:
#include<iostream>
using namespace std;
int Digital_Root(int number);
int main() {
int VedicSquare[9][9]; // 定义二维数组
int i, j;
int number; // 需要显示的数字
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
VedicSquare[i][j] = (i+1) * (j+1);
while (VedicSquare[i][j] >= 10) {
VedicSquare[i][j] = Digital_Root(VedicSquare[i][j]);
}
cout << VedicSquare[i][j] << " ";
}
cout << endl;
}
cout << "请输入显示的数字:";
cin >> number;
while (number != 0) {
cout << "Vedic Star图形为:" << endl;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (VedicSquare[i][j] == number)
cout << "* ";
else
cout << " ";
}
cout << endl;
}
cout << "请输入显示的数字:";
cin >> number;
}
return 0;
}
int Digital_Root(int number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
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)
源代码如下:
#include<iostream>
using namespace std;
int main() {
int number;
int i;
int time;
int floor;
int temper;
cin >> number;
while (number != 0) {
time = 0;
temper = 0;
for (i = 0; i < number; i++) {
cin >> floor;
if (floor > temper)
time += 6 * (floor - temper)+5;
else
time += 4 * (temper - floor)+5;
temper = floor;
}
cout << time<<endl;
cin >> number;
}
}