笨蛋学C++【C++基础第五弹-菜鸟教程练习题】

2024-04-24 19:52

本文主要是介绍笨蛋学C++【C++基础第五弹-菜鸟教程练习题】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C++第五弹-练习题

  • Demo测试实例
    • 求商及余数
    • 交换变量
    • 判断一个数是奇数还是偶数
    • 判断元音/辅音
    • 判断三个数中的最大数
    • 计算自然数之和
    • 判断闰年
    • 求一个数的阶乘
    • 创建各类三角形图案
    • 求两数的最大公约数
    • 求两数最小公倍数
    • 实现一个简单的计算器
    • 猴子吃桃问题
    • 三只小猪称体重
    • 猜数字(0-100)
    • 求水仙花数
    • 敲桌子(0-100)
    • 乘法口诀表
    • 数组元素逆置
    • 冒泡排序
    • 统计学生成绩

Demo测试实例

求商及余数

使用 C++ 获取用户的输入两个数字,并将两个数字相除,然后将商和余数输出到屏幕。

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;int main(void){//除数int divisor;//被除数int dividend;//余数int remainder;//商int merchant;cout << "请输入除数:" << endl;cin >> divisor;if(divisor != 0){cout << "请输入被除数" << endl;cin >> dividend;merchant = divisor / dividend;remainder = divisor % dividend;cout << divisor << "/" << dividend << "=" << merchant << ",余数=" << remainder << endl;}else{cout << "除数不能为0" << endl;}return 0;
}

交换变量

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;void swap1(int &numOne,int &numTwo);
void swap2(int &numOne,int &numTwo);
void swap3(int &numOne,int &numTwo);int main(void){//第一个数字int numOne;//第二个数字int numTwo;cout << "请输入第一个数字" << endl;cin >> numOne;cout << "请输入第二个数字" << endl;cin >> numTwo;// cout << "swap1交换前:" << numOne << "," << numTwo << endl;// swap1(numOne,numTwo);// cout << "swap2交换后:" << numOne << "," << numTwo << endl;//// cout << "swap2交换前:" << numOne << "," << numTwo << endl;// int *ptrOne = &numOne;// int *ptrTwo = &numTwo;// swap2(*ptrOne,*ptrTwo);// cout << "swap2交换后:" << numOne << "," << numTwo << endl;cout << "swap3交换前:" << numOne << "," << numTwo << endl;int &rOne = numOne;int &rTwo = numTwo;swap3(rOne,rTwo);cout << "swap3交换后:" << numOne << "," << numTwo << endl;return 0;
}
//中间变量交换
void swap1(int &numOne ,int &numTwo){int temp;temp = numOne;numOne = numTwo;numTwo = temp;
}//数字交换
void swap2(int &numOne,int &numTwo){numOne = numOne + numTwo;numTwo = numOne - numTwo;numOne = numOne - numTwo;
}//异或交换
void swap3(int &numOne,int &numTwo){// int temp;// temp = numOne ^ numTwo;// numOne = temp ^ numOne;// numTwo = temp ^ numTwo;// numOne = numOne ^ numTwo;// numTwo = numTwo ^ numOne;// numOne = numOne ^ numTwo;numOne^=numTwo^=numOne^=numTwo;}

判断一个数是奇数还是偶数

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>
using namespace std;int main(void){int num;cout << "输入一个数字:";cin >> num;if(num % 2 == 0){cout << num << "是偶数" << endl;}else{cout << num << "是奇数" << endl;}return 0;
}

判断元音/辅音

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>using namespace std;int main(void) {char character;cout << "请输入字符" << endl;cin.get(character);char yuan[] = {'a', 'e', 'i', 'o', 'u'};for (int i = 0; i < sizeof(yuan) / sizeof(yuan[0]); i++) {cout << "yuan[i]-32" << yuan[i]-32 << endl;if (character == yuan[i] || character == (yuan[i] - 32)) {cout << character << "是元音字母" << endl;break;}else{cout << character << "不是元音字母" << endl;}}return 0;
}

判断三个数中的最大数

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int nums[3];for (int i = 0; i < sizeof(nums) / sizeof(nums[i]); i++) {cout << "请输入第" << i+1 << "个数:" << endl;cin >> nums[i];}int numMax;for (int i = 0; i < sizeof(nums) / sizeof(nums[i]); i++) {numMax = nums[i] > numMax ? nums[i] : numMax;}cout << "最大值为:" << numMax << endl;return 0;
}

计算自然数之和

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int num, sum = 0;cout << "请输入一个正整数:" << endl;cin >> num;for (int i = 1; i <= num; i++) {sum += i;cout << "sum=" <<sum << endl;}cout << "1到" << num << "的累加和为:" << sum << endl;return 0;
}

判断闰年

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>using namespace std;int main(void) {int inputYear;cout << "请输入年份:" << endl;cin >> inputYear;if (inputYear % 4 == 0 || inputYear % 400 == 0 && inputYear % 100 != 0) {cout << inputYear << "是闰年" << endl;}else{cout << inputYear << "不是闰年" << endl;}return 0;
}

求一个数的阶乘

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;int jieCheng(int num);int main(void){int num;cout << "输入要计算的阶乘数" << endl;cin >> num;if(num !=0){cout << num << "的阶乘结果为:" << jieCheng(num) << endl;}else{cout << "输入的数不能为0" << endl;}return 0;
}int jieCheng(int num){if(num == 1){return 1;}else{return num * jieCheng(num -1);}
}

创建各类三角形图案

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int rows;cout << "请输入行数:" << endl;cin >> rows;for (int i = 0; i < rows + 1; i++) {for (int j = 0; j < i; j++) {cout << "* ";}cout << endl;}cout << "------------------------------" << endl;for (int i = 0; i < rows + 1; i++) {for (int j = 0; j < i; j++) {cout << j + 1 << " ";}cout << endl;}cout << "------------------------------" << endl;char character = 'A';for (int i = 0; i < rows + 1; i++) {for (int j = 0; j < i + 1; j++) {cout << character << " ";}character++;cout << endl;}cout << "------------------------------" << endl;for (int i = 0; i < rows + 1; i++) {for (int j = 0; j < rows - i; j++) {cout << "* ";}cout << endl;}cout << "------------------------------" << endl;for (int i = 0; i < rows + 1; i++) {for (int j = 0; j < rows - i; j++) {cout << j + 1 << " ";}cout << endl;}cout << "------------------------------" << endl;for (int i = 0; i < rows; i++) {//打印空格for(int k =0;k<rows-i;k++){cout<<" ";}for (int j = 0; j < i + 1; j++) {cout << " *";}cout << endl;}cout << "------------------------------" << endl;for(int i=0;i<rows;i++){for(int j = 0;j<rows-i;j++){cout << " ";}for(int k=0;k<i+1;k++){cout <<" " <<k+1;}cout << endl;}cout << "------------------------------" << endl;for(int i=0;i<rows+1;i++){for(int j =0;j<rows-i;j++){cout << " ";}for(int k=0;k<i;k++){cout << " " <<i;}cout << endl;}cout << "------------------------------" << endl;for(int i=0;i<rows+1;i++){for(int k=0;k<i;k++){cout << " ";}for(int j =0;j<rows-i;j++){cout << "* ";}cout << endl;}cout << "------------------------------" << endl;for(int i=0;i<rows+1;i++){for (int j = 0; j < rows-i; ++j) {cout << " ";}int temp;for(int k = 0; k < i+1; ++k){if(k ==0 || i ==0){temp =1;}else{temp = temp * (i-k+1) / k;}cout << " " << temp;}cout << endl;}return 0;
}

求两数的最大公约数

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int numOne;int numTwo;cout << "请输入第一个数" << endl;cin >> numOne;cout << "请输入第二个数" << endl;cin >> numTwo;while (numOne != numTwo) {if (numOne > numTwo) {numOne = numOne - numTwo;} else {numTwo = numTwo - numOne;}}cout << "最大公约数是" << numTwo << endl;return 0;
}

求两数最小公倍数

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int numOne, numTwo;cout << "请输入第一个数" << endl;cin >> numOne;cout << "请输入第二个数" << endl;cin >> numTwo;//先确定最大的值int max = numOne > numTwo ? numOne : numTwo;do {//如果最大的值对两个数都能进行整除,说明就是最小公倍数了if (max % numOne == 0 && max % numTwo == 0) {cout << "最小公倍数是:" << max << endl;break;} else {//否则就让最大值自增,直到能整除max++;}} while (true);return 0;
}

实现一个简单的计算器

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;int main(void){int numOne,numTwo;cout << "请输入第一个数:" << endl;cin >> numOne ;cout << "请输入第二个数:" << endl;cin >> numTwo ;char fuhao;cout << "请输入运算符:" ;cin.ignore();cin.get(fuhao);switch(fuhao){case '+':cout << numOne << " + " << numTwo << " = " << numOne + numTwo << endl;break;case '-':cout << numOne << " - " << numTwo << " = " << numOne - numTwo << endl;break;case '*':cout << numOne << " * " << numTwo << " = " << numOne * numTwo << endl;break;case '/':if(numOne != 0){cout << numOne << " / " << numTwo << " = " << numOne / numTwo << endl;}else{cout << "除数不能为0" << endl;}break;default:cout << "输入的运算符有误" << endl;}return 0;
}

猴子吃桃问题

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int day;cout << "输入天数" << endl;cin >> day;int tao = 1;int temp = day;//因为是每次吃一半加1,从最后天数往前看,就是桃子总数*2+1,第十天是1个桃子,第九天就是(2+1)*2 = 4个桃子for (int i = 1; i < day; i++) {cout << "第" << temp-- << "天的桃子为:" << tao << endl;tao = (tao + 1) * 2;}cout << "第" << temp << "天的桃子为:" << tao << endl;return 0;
}

三只小猪称体重

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {int weight[3];int sizes = sizeof(weight) / sizeof(weight[0]);for (int i = 0; i < sizes; i++) {cout << "请输入第" << i + 1 << "只小猪的体重" << endl;cin >> weight[i];}int max = 0;for (int j = 0; j < sizes; j++) {if (weight[j] > weight[max]) {max = j;}}cout << "最重的是第" << max + 1 << "只小猪,它的体重是:" << weight[max] << endl;return 0;
}

猜数字(0-100)

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>
#include <cmath>
#include <random>using namespace std;int main(void) {//1.设置种子srand((unsigned) time(NULL));//2.随机生成数字int num = rand() % (1 - 1 + 100) + 1;cout << "随机数已生成,请尝试猜测" << endl;int userInput;while (true) {cout << "请输入你猜测的结果:" << endl;cin >> userInput;if (userInput > num) {cout << "猜大了" << endl;} else if (userInput < num) {cout << "猜小了" << endl;} else {if (userInput == num) {cout << "恭喜你,猜对了!" << endl;break;}}}return 0;
}

求水仙花数

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>using namespace std;int main(void) {int bai, shi, ge;for (int i = 2; i < 999; i++) {bai = i / 100;shi = i % 10;ge = i % 100 / 10;if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i) {cout << i << "是水仙花数" << endl;}}return 0;
}

敲桌子(0-100)

//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>using namespace std;int main(void) {for (int i = 0; i < 100; i++) {if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {cout << "敲桌子" << endl;} else {cout << i << endl;}}return 0;
}

乘法口诀表

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>using namespace std;int main(void) {for (int i = 1; i < 10; i++) {for (int j = 1; j < i+1; j++) {cout << j << " * " << i << " = " << (i * j) << " ";}cout << endl;}return 0;
}

数组元素逆置

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>
using namespace std;int main(void){int arr1[5]={1,2,3,4,5};int sizes= sizeof(arr1)/sizeof(arr1[0]);cout << "交换前:";for(int i=0;i<sizes;i++){cout<< arr1[i] << " ";}for(int i=0;i<sizes/2;i++){int temp =0;temp = arr1[i];arr1[i] = arr1[sizes-1-i];arr1[sizes-1-i] = temp;}cout << "交换后:";for(int i=0;i<sizes;i++){cout<< arr1[i] << " ";}return 0;
}

冒泡排序

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>using namespace std;int main(void) {int arr[] = {3, 22, 1, 4, 23, 49, 87, 61};int sizes = sizeof(arr) / sizeof(arr[0]);cout << "排序前:";for (int i = 0; i < sizes; i++) {cout << arr[i] << " ";}for (int i = 0; i < sizes - 1; i++) {for (int j = 0; j < sizes - 1 - i; j++) {if (arr[j] > arr[j + 1]) {int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}cout << "排序后:";for (int i = 0; i < sizes; i++) {cout << arr[i] << " ";}return 0;
}

统计学生成绩

//
// Created by TodaySaturday on 2024/4/24.
//#include <iostream>
using namespace std;int main(void){int score[][3]={{88,22,76},{99,33,77},{66,55,44}};int sum;for(int i=0;i<3;i++){for(int j=0;j<3;j++){sum += score[i][j];}}cout << "sum = " << sum << endl;return 0;
}

这篇关于笨蛋学C++【C++基础第五弹-菜鸟教程练习题】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/932692

相关文章

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Python WebSockets 库从基础到实战使用举例

《PythonWebSockets库从基础到实战使用举例》WebSocket是一种全双工、持久化的网络通信协议,适用于需要低延迟的应用,如实时聊天、股票行情推送、在线协作、多人游戏等,本文给大家介... 目录1. 引言2. 为什么使用 WebSocket?3. 安装 WebSockets 库4. 使用 We

spring AMQP代码生成rabbitmq的exchange and queue教程

《springAMQP代码生成rabbitmq的exchangeandqueue教程》使用SpringAMQP代码直接创建RabbitMQexchange和queue,并确保绑定关系自动成立,简... 目录spring AMQP代码生成rabbitmq的exchange and 编程queue执行结果总结s

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数