《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;
}
相关推荐
DougLiang3 分钟前
关于easyexcel动态下拉选问题处理
java·开发语言
mochensage10 分钟前
CSP信奥赛C++常用系统函数汇总
c++·信奥
mochensage12 分钟前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
fpcc16 分钟前
跟我学c++中级篇——多线程中的文件处理
c++
chengooooooo18 分钟前
leetcode Top100 238. 除自身以外数组的乘积|数组系列
算法·leetcode
GUIQU.34 分钟前
【每日一题 | 2025年6.2 ~ 6.8】第16届蓝桥杯部分偏简单题
算法·蓝桥杯·每日一题
全职计算机毕业设计1 小时前
基于Java Web的校园失物招领平台设计与实现
java·开发语言·前端
5:001 小时前
云备份项目
linux·开发语言·c++
weixin_527550401 小时前
初级程序员入门指南
javascript·python·算法
笨笨马甲2 小时前
Qt Quick模块功能及架构
开发语言·qt