《C++ primer plus》第六版课后编程题-第05章

第五章

1

cpp 复制代码
#include <iostream>
#include <array>

using namespace std;


void main() {
	int n1, n2;
	cin >> n1 >> n2;
	int sum = 0;
	for (int i = n1; i <= n2; i++) {
		sum += i;
	}
	cout << sum;
}

2

cpp 复制代码
#include <iostream>
#include <array>

using namespace std;

const int ArSize = 16;

void main() {
	array<long double,ArSize> factorials;
	factorials[1] = factorials[0] = 1;
	for (int i = 2; i < ArSize; i++)
		factorials[i] = i * factorials[i - 1];
	for (int i = 0; i < ArSize; i++)
		std::cout << i << "!=" << factorials[i] << std::endl;

}

3

cpp 复制代码
#include <iostream>
#include <array>

using namespace std;


void main() {
	int sum = 0;
	int n = 1;
	while (n != 0) {
		cin >> n;
		sum += n;
		cout << sum << endl;
	}

}

4

cpp 复制代码
#include <iostream>
#include <array>

using namespace std;

double Daphne_money(int years) {
	return 100+0.1 * 100 * years;
}

double Cleo_money( int years) {
	if (years == 1)
		return 100;
	else {
		return Cleo_money(years - 1) * 1.05;
	}
}

void main() {
	int years = 1;
	while (Daphne_money(years) > Cleo_money(years)) {
		years++;
	}
	cout << years;
}

5

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>

using namespace std;


void main() {
    vector<string> sells = { "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November",
        "December" };
    vector<int> number(12);
    int total = 0;
    for (int i = 0; i < 12; i++) {
        cout << "Total sells in " << sells[i] << " is:" << endl;
        cin >> number[i];
        total += number[i];
    }
    cout << "total sells is" << total << endl;
}

6

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>

using namespace std;


void main() {
    vector<vector<string>> sells(3,vector<string>(12));
    sells= { { "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November",
        "December" },
        { "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November",
        "December" },
        { "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November",
        "December" } };
    vector<vector<int>> number(3,vector<int>(12));
    int total_per_year[3] = { 0,0,0 };
    int total = 0;
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 12; i++) {
            cout << "Total sells in " << sells[j][i] <<" No."<<j+1<< "year is:" << endl;
            cin >> number[j][i];
            total_per_year[j] += number[j][i];
            total += number[j][i];
        }
        cout << "total sells in No." << j+1 << " year is" << total_per_year[j] << endl;
    }
    cout << "total sells of all years is" << total << endl;
}
相关推荐
幽蓝计划11 分钟前
HarmonyOS NEXT仓颉开发语言实战案例:外卖App
开发语言·harmonyos
伍哥的传说17 分钟前
鸿蒙系统(HarmonyOS)应用开发之实现电子签名效果
开发语言·前端·华为·harmonyos·鸿蒙·鸿蒙系统
ytttr87331 分钟前
matlab通过Q学习算法解决房间路径规划问题
学习·算法·matlab
小张成长计划..1 小时前
数据结构-栈的实现
开发语言·数据结构
旷世奇才李先生1 小时前
Lua 安装使用教程
开发语言·lua
Zonda要好好学习1 小时前
Python入门Day2
开发语言·python
不良手残2 小时前
IDEA类和方法注释模板设置-保姆教程
java·开发语言
go54631584652 小时前
修改Spatial-MLLM项目,使其专注于无人机航拍视频的空间理解
人工智能·算法·机器学习·架构·音视频·无人机
项目題供诗2 小时前
黑马python(二十四)
开发语言·python
油泼辣子多加2 小时前
【Torch】nn.BatchNorm1d算法详解
算法