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 @   catting123  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示