实验一 C++简单程序设计

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;
	}
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值