C++简单程序设计

题目

QQ截图20220721091130 QQ截图20220721091130

代码

第一题

#include <iostream>
using namespace std;

//求数字根
int digital_root(int n) {
	while (n >= 10) {
		n = n / 10 + n % 10;
	}
	return n;
}

int main() {
	int i;
	int j;
	int a[20][20];
	char b[20][20];
	for (i = 0; i < 9; i++) {
		for (j = 0; j < 9; j++) {
			a[i][j] = digital_root((i + 1) * (j + 1));
		}
	}
	for (i = 0; i < 9; i++) {
		for (j = 0; j < 9; j++) {
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	int n;
	cout << "Please enter a integer range 1-9: ";
	cin >> n;
	cout << "Look, vedic stars!" << endl;
	for (i = 0; i < 9; i++) {
		for (j = 0; j < 9; j++) {
			if (a[i][j] == n) {
				b[i][j] = '*';
			} else {
				b[i][j] = ' ';
			}
		}
	}
	for (i = 0; i < 9; i++) {
		for (j = 0; j < 9; j++) {
			cout << b[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

第二题

#include <iostream>
#include <string>
using namespace std;

struct elevator {
	int time = 0; //初始时间为零
	string calculation; //计算过程用字符串表示
}ele[100];

int main() {
	int n;
	int number = 0; //用于计数,一共输入几组数据
	while (cin >> n && n != 0) { //连续输入,输入零结束
		int now = 0;
		int next = 0;
		for (int i = 0; i < n; i++) {
			cin >> next;
			if (now < next) { //电梯上升
				ele[number].time += (next - now) * 6 + 5;
				if (i != 0) { //第一次前面无加号,第二次及以后前面都有加号
					ele[number].calculation.append("+");
				}
				ele[number].calculation.append("6*" + to_string(next - now) + "+5");
			} else { //电梯下降
				ele[number].time += (now - next) * 4 + 5;
				if (i != 0) {
					ele[number].calculation.append("+");
				}
				ele[number].calculation.append("4*" + to_string(now - next) + "+5");
			}
			now = next;
		}
		if (n != 0) {
			number++;
		}
	}
	for (int i = 0; i < number; i++) {
		cout << ele[i].time << "(" << ele[i].calculation << ")" << endl;
	}
	return 0;
}
posted @ 2022-08-30 20:37  catting123  阅读(38)  评论(0)    收藏  举报