当前位置: 首页 > article >正文

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

第五章

1

#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

#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

#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

#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

#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

#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;
}

http://www.kler.cn/a/513986.html

相关文章:

  • root用户Linux银河麒麟服务器安装vnc服务
  • Java设计模式 六 原型模式 (Prototype Pattern)
  • TRELLIS微软的图生3D
  • 基于 MDL 行情插件的中金所 L1 数据处理最佳实践
  • 【Linux】多线程(二)
  • Linux:常用命令--文件与目录操作
  • docker构建Java项目镜像常用的Java版本,国内私有仓库公网快速下载,解决从docker.io无法下载的问题
  • 【Elasticsearch】腾讯云安装Elasticsearch
  • C#集合操作优化:高效实现批量添加与删除
  • vue3+uniapp开发鸿蒙初体验
  • 【图像处理】——掩码
  • C#防止重复提交
  • Unity中两个UGUI物体的锚点和中心点设置成不一样的,然后怎么使两个物体的位置一样?
  • vsftpd虚拟用户部署
  • MATLAB中characterListPattern函数用法
  • 【爱上C++】vector用法详解
  • 案例分析一
  • MySQL新建和删除普通用户
  • Mac苹果电脑 怎么用word文档和Excel表格?
  • 如何使用Python爬虫获取微店商品详情:代码示例与实践指南