《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;
}
相关推荐
Sakuyu434681 分钟前
C语言基础--基本数据类型
c语言·开发语言
在坚持一下我可没意见3 分钟前
Python 修仙修炼录 05:循环神通,省去无用苦修
开发语言·python·面试·入门·循环·复习
ZHW_AI课题组9 分钟前
基于逻辑回归的乳腺癌预测分类
算法·分类·逻辑回归
胡志辉14 分钟前
贪心算法最坑的地方:每一步都看起来很对,最后还是错了
算法
Hua-Jay15 分钟前
OpenCV联合C++/Qt 学习笔记(二十)----Harri角点检测、Shi-Tomas角点检测及亚像素级别角点位置优化
c++·笔记·qt·opencv·学习·计算机视觉
代码北人生18 分钟前
GitHub 日榜第一、月下载 110 万:supervision 出现之前,写计算机视觉代码是什么感觉
算法·claude
techdashen21 分钟前
Rust 社区在 4 月做了什么:项目管理月报解读
开发语言·rust·mfc
南宫萧幕22 分钟前
HEV能量管理策略 Simulink 实战:从零搭建 Rule-based 与 A-ECMS 对比模型及排错指南
人工智能·算法·matlab·simulink·控制
萧戈22 分钟前
C/C++ 运行时库概念详解
c语言·c++
十五年专注C++开发22 分钟前
QFluentKit: 一个基于 Qt Widgets 的 Fluent Design 风格 UI 组件库
开发语言·c++·qt·ui·qfluentkit