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

c++第四章练习题

c++第四章练习题

查找最小值

编写一个应用程序,查找多个整数中的最小值。假设读取的第一个值指定了需要从用户输入的数值数量。
关键点
先读取第一个数 n,表示后续需要输入 n 个整数。
遍历输入的整数,动态比较并记录最小值。

#include <iostream>
int main() {//初始化读入数字的个数int num{0}, min{0};std::cout << "input number of digits: ";std::cin >> num;std::cout << "input " << num << " numbers: ";int arr[num];for (size_t i = 0; i < num; ++i) {std::cin >> arr[i];}min = arr[0];//::cout << min;for (size_t i = 1; i < num; ++i) {if (min > arr[i]) min = arr[i];}std::cout << "smallest number: " << min;return 0;
}

上述自己写的程序缺少对异常情况的处理
添加对用户输入0、1等极端情况的处理

#include <iostream>
int main() {//初始化读入数字的个数int num{0}, min{0};std::cout << "input number of digits: ";std::cin >> num;int arr[num];//针对用户输入0、1进行特殊处理switch (num) {case 0: std::cout << "No integer were entered."; break;case 1:std::cout << "input an number: ";std::cin >> min;std::cout << "smallest number: " << min;break;default:std::cout << "input " << num << " number: ";for (size_t i = 0; i < num; ++i) {std::cin >> arr[i];}min = arr[0];for (size_t i = 1; i < num; ++i) {if (min > arr[i])min = arr[i];}std::cout << "smallest number: " << min;}return 0;
}


计算奇数的乘积

编写一个应用程序,计算从 1 到 15 的所有奇数的乘积。
关键点
遍历 1 到 15 的整数,筛选出奇数(i % 2 != 0)。
累乘所有奇数,注意结果可能较大,需用 long 或 unsigned long 类型。

#include <iostream>
int main() {int arr[15];unsigned long result{1};//输入1~15for (size_t i=0; i<15; ++i) {arr[i]=i+1;//奇数if (arr[i] % 2 !=0) {result *= arr[i];}}std::cout << "product of odd integers from 1 to 15: " << result;return 0;
}

在这里插入图片描述

阶乘

阶乘在概率问题中经常用到。正整数 n 的阶乘(写作 n!,读作 “n 的阶乘”)等于从 1 到 n 的所有正整数的乘积。编写一个应用程序,计算 1 到 20 的阶乘。使用 long 类型,以表格形式显示结果。计算 100 的阶乘可能会遇到什么困难?
关键点
计算阶乘时,数值增长极快(如 20! 已超过 20 亿),long 类型可能仅能存储到一定范围的阶乘(需根据具体编译器和平台判断)。
计算 100! 的困难:普通整数类型(如long long)无法存储如此大的数值,需使用大整数库(如 C++ 的__int128或第三方库)或高精度计算方法。

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
cpp_int factorial(int n) {cpp_int fact{1};for (size_t i=1; i<=n; ++i)fact *= i;return fact;
}
int main() {//计算1-20的阶乘std::cout << "number\t" << "factorial\t\n";for (size_t i=1; i<=20; ++i) {std::cout << i << "\t" << factorial(i) << "\t\n";}return 0;
}

修改的复利程序

修改图 4.17 中的复利应用程序(该程序引入了 Boost Multiprecision 库的cpp_dec_float_50类)

使其对利率 5%、6%、7%、8%、9% 和 10% 重复执行计算步骤。使用for循环遍历不同的利率。你将亲眼见证复利的神奇效果。

#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
using boost::multiprecision::cpp_dec_float_50;int main() {//复利公式 s=p*(1+rate)^yearcpp_dec_float_50 principal{1000};std::cout << "\nYear\t" << " interest\t "<< "Amount on deposit\t\n";for (size_t year=1; year<=10; ++year) {cpp_dec_float_50 rate{"0.05"};for (size_t i=0; i<=5; ++i) {cpp_dec_float_50 amount{principal*pow(1+rate,year)};std::cout << year << "\t" << rate << "\t" << amount << "\t\n";rate += cpp_dec_float_50("0.01");}}return 0;
}

三角形打印程序

编写一个应用程序,分别显示以下图案,每个图案依次打印在另一个下方。使用for循环生成图案。所有星号()必须通过单个语句cout << ''打印,使星号并排显示。可使用语句cout << '\n’换行,使用cout << ’ '在最后两个图案中显示空格。程序中不应有其他输出语句。
提示:最后两个图案要求每行以适当数量的空格开头。

#include <iostream>int main() {//第一个图案for (size_t i=1; i<=10; ++i) { //控制行for (size_t j=1; j<=i; ++j) { //控制列std::cout << '*';}std::cout << "\n";}std::cout << "\n";//第二个图案for (size_t i=1; i<=10; ++i) {for (size_t j=10; j>=i; --j) { //10,9,8,7……std::cout << '*';}std::cout << "\n";}std::cout << "\n";//第三个图案for (size_t i=1; i<=10; ++i) {for (size_t k=1; k<i;++k)std::cout << ' ';for (size_t j=10; j>=i; --j) { //10,9,8,7……std::cout << '*';}std::cout << "\n";}std::cout << "\n";//第四个图案for (size_t i=1; i<=10; ++i) { //控制行for (size_t k=10; k>i;--k) std::cout << ' ';for (size_t j=1; j<=i; ++j) { //控制列std::cout << '*';}std::cout << "\n";}return 0;
}


柱状图打印程序

编写一个应用程序,读取五个1到30之间的数字。对于每个读取的数字,程序应显示相同数量的相邻星号。例如,如果程序读取数字7,则应显示*******。在读取所有五个数字后,显示这些星号条。

#include <iostream>
#include <random>
int main() {std::default_random_engine engine{};std::uniform_int_distribution randomnum{1,30};for (size_t k=0; k<5; k++) {for (size_t i=0; i<=randomnum(engine); i++) {std::cout << "*";}std::cout << "\n";}return 0;
}

计算π的值

通过无穷级数计算π的值。打印如下所示的表格,该表格显示通过计算此级数的前200,000项近似得到的π值。在首次得到以3.14159开头的近似值之前,需要使用多少项?

#include <iostream>
#include<boost/multiprecision/cpp_dec_float.hpp>
using boost::multiprecision::cpp_dec_float_50;
int main() {cpp_dec_float_50 pi{0};std::cout << "Accuracy set at : 200000" << "\n";std::cout << "term\t" << "pi\t\n";for (size_t i=1; i<=200'000; ++i) {if ((i % 2) == 1) { //奇数项pi += cpp_dec_float_50(4) / cpp_dec_float_50((2*i)-1);std::cout << i << "\t" << std::setprecision(20) << pi << "\t\n";}else { //偶数项pi -= cpp_dec_float_50(4) / cpp_dec_float_50((2*i)-1);std::cout << i << "\t" << std::setprecision(20) << pi << "\t\n";}}return 0;
}

相关文章:

  • openpnp - 给M4x0.7mm的直油嘴加油的工具选择
  • day025-网络基础-DNS与ARP协议
  • 征程 6 J6EM 常见 qconfig 配置解读与示例
  • LangGraph(八)——LangGraph运行时
  • 博士论文写作笔记
  • 【大模型DA】Unified Language-driven Zero-shot Domain Adaptation
  • agent-zero: 打造你的AI专属AI助理
  • Canvas: trying to draw too large(256032000bytes) bitmap.
  • JavaScript 模块系统:CJS/AMD/UMD/ESM
  • QT/c++航空返修数据智能分析系统
  • Cocos 打包 APK 兼容环境表(Android API Level 10~15)
  • 【渲染】拆解《三国:谋定天下》场景渲染技术
  • 读《Go语言圣经记录》(二):深入理解Go语言的程序结构
  • 工作流引擎-06-流程引擎(Process Engine)对比 Flowable、Activiti 与 Camunda 全维度对比分析
  • 淘宝商品详情页有哪些常见的动态加载技术?
  • t018-高校宣讲会管理系统 【含源码!】
  • 大规模真实场景 WiFi 感知基准数据集
  • 子串题解——和为 K 的子数组【LeetCode】
  • C++11 智能指针:从原理到实现
  • 为什么badmin reconfig以后始终不能提交任务
  • 广州网络建站/seo推广的全称是
  • 金华网站建设/网站快速收录
  • 做兼职比较专业靠谱的网站/中文域名的网站
  • 微商城系统哪家强/优化大师怎么强力卸载
  • 怎么看一家网站是谁做的/小说搜索风云榜排名
  • icp网站备案号查询/百度网络推广怎么收费