C++编程练习题及答案详解

C++编程最基础练习题(1-10) 小白入门必刷 50道C++编程练习题 小白记录学习C++的过程~ 阅读详情

1、执行以下操作后,写出 x 的值:int x = 4; x += 4;

8

2、执行以下操作后,写下x的值,然后用编译器验证:int x = 9; x-=2;

7

3、写出执行以下操作后x的值:int x = 900; x/=2;

450

4、写出执行以下操作后x的值,然后用编译器验证:int x = 50; x*=2;

100

5、执行以下操作后,写下x的值,然后用编译器进行验证:int x = 1; x += 1;

2

6、写下执行以下操作后 x 的值,然后用编译器验证:int x = 2; x -= 200;

-198

7、执行以下操作后,写出x的值,然后用编译器验证:int x = 5; x*=5;

25

8、当 wearingSocks 的值为 true 时,你认为 !!wearingSocks 的值会是什么?

True

9、运行以下代码后,isVisible 的值是多少?bool hidden = true; bool isVisible = !hidden;

True

10、编写一个 while 循环,打印从 1 到 10 的数字。

为了使代码完整,添加必要的头文件和 main 函数,以下是完整的 C++ 代码:

#include <iostream>

int main() {
    int x = 1;
    while( x <= 10 ) {
        std::cout << x << std::endl;
        x++;
    }
    return 0;
}

11、编写一个while循环,逆序打印从10到1的数字。

#include <iostream> 
int main() { 
    int x = 10; 
    while( x >= 1 ) { 
        std::cout << x << std::endl; 
        x--; 
    } 
    return 0; 
}

12、编写一个 while 循环,打印从 2 到 20 以 2 为增量的数字(例如 2、4、6 和 8)。

int x = 2;
while( x <= 20 )
{
    cout << x << endl;
    x += 2;
}
C++练习题(附答案) 关于C++的练习题 阅读详情

相关推荐

C++经典题目,初学者快速入门!【附有解析】

大一小白初学C++的简单例子,超适合小白初学者。题目包括且不限于:闰年判断、冒泡排序、各类型的求和、数列求和、菱形打印、九九乘法表、判断质数、水仙花数、最大公约数与最小公倍数、杨辉三角、求阶乘、数组相关类型

2201_75475240的博客 1万+

北大编程网格【练习题】,北医计算概论课程编程题答案整理,C语言/python/C++

北大编程网格练习题/作业题答案整理 所有题目已经做完且编译通过,在这个过程中我也整理了所有的题目及其答案。 有需要的小伙伴可以留言或者私信我。 目录北大编程网格练习题/作业题答案整理练习题练习1输出连续素数相关月画矩形花纹输出日历问题分数求和不吉利日期整数的质因子细菌的战争练习2程序调试:分数序列求和练习3鸡尾酒疗法求e角谷猜想人民币支付求特殊自然数练习4癌细胞体积寻找三角形凑数美丽编号最后一块石头相似的名字新年晚会最长连号田忌赛马括号闭合问题护林员盖房子练习5就医问题马虎的佩奇多多爱赚零花钱因子问题

qq_43481435的博客 1万+

C++ Primer Plus(第六版)编程练习答案 所有章节汇总

本博客汇总了本人历时一年学完《C++ Primer Plus (第6版)》所做的所有编程练习的答案,供大家学习参考和交流。 以下附上各章节的答案详解博客地址,以及源码工程下载地址。 PS:源码下载大部分都是需要积分的,当时设置的时候好像有点乱,有的是1积分,有的是3积分,最难的第12章和第13章我设置的是不要积分,大家都可以下载下来自己运行一下看看结果。 第 2 章 开始学习C++: ...

清水河C罗——Leonardo-Liu 4万+

第6期《C++入门练习题(附答案)》

前言 C++是C语言的继承,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。这里是一些非常水的入门题,适合新手。 工具/原料 电脑 编译器 选择结构 读入一个整数a,如果a为偶数在屏幕上输出yes,否则输出no。 超市里卖电池,每个电池8角钱,若数量不少于10个,则打7.5折。输入购买的电池数量,求需支付多少钱? 有两种方法 对一批货物

joe_zxq21的博客 2078

c++ primer plus 编程答案 上

天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式为:1光年=63240天文单位.7.编写一个程序,要求用户输入小时数和分钟数。其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。转换公式:华氏温度 = 1.8×摄氏温度 + 32.0。

Jonathan的博客 1625

C语言与C++编程实战:练习题及答案解析

本文还有配套的精品资源,点击获取 简介:本资源为C语言和C++编程学习者提供了一系列配套练习题和答案,涵盖基础语法到高级特性,包括变量、数据类型、运算符、控制结构、函数、指针、类和对象、模板、内存管理及异常处理等内容。C++特有的面向对象编程概念如封装、继承、多态性以及STL容器和算法等也是练习的重点。通过这些实战练习,学习者可系统提升编程技能,巩固基础知识点并掌握更高级的...

weixin_28933797的博客 852

C++编程练习题大全(带答案)

本资源为C++学者入门实用资料,C++编程练习题大全(带答案),转自网络,知识共享,版权归原创所有

STC15单片机双串口通信实战:串口2配置与多设备交互

本文详细介绍了STC15单片机双串口通信的实战配置,重点解析串口2的硬件连接、寄存器设置及多设备交互实现。通过实际项目案例和完整代码示例,帮助开发者快速掌握STC15串口2的配置技巧,解决常见通信问题,提升嵌入式系统开发效率。

weixin_29174013的博客 262

三极管简单振荡电路实现:从零开始的实战示例

通过一个基础实例,手把手教你如何利用三极管构建简单的振荡电路,深入理解三极管在实际电路中的工作原理与应用技巧。

weixin_32836713的博客 1213

基于springboot生鲜食品订购系统的设计与实现

近年来互联网络的迅猛发展和电子终端设备的普及,赋予了各行业充足的发展空间。微信小程序的O2O生鲜食品订购相比于传统信息技术,时效性是它最大的特色,已经在电子娱乐、经济等中发挥着举足轻重的作用。短时间内迅速扩大了线上管理系统的规模。尽管O2O生鲜食品订购已经有了很大程度的发展,但在微信小程序的O2O生鲜食品订购领域上却少有建设。

1028

《AI应用架构师必学:企业AI能力地图构建的高级技巧》

企业AI能力地图,简单来说,是一种可视化工具,它展示了企业在AI领域所具备的各种能力及其相互关系。就如同城市地图展示城市的街道、建筑和设施一样,AI能力地图呈现了企业在数据处理、模型开发、算法应用等多个方面的能力布局。

AGI×大数据,开启智能时代的认知跃迁;解码AGI,赋能数据驱动的智能革命。 824

Java中使用工厂模式和策略模式优雅消除if-else语句(UML类图+案例分析)

在最近的后端开发中,多多少少会发现有很多if-else语句,如果条件过多则会造成整体代码看起来非常臃肿。本篇博客汇总了我对工厂模式和策略模式的理解,以及我是如何把它们巧妙运用在实际项目当中的一些技巧进行了分享,如有问题,欢迎评论区讨论!

黄团团的个人博客 2619

YOLOv11改进 | Neck篇 | 轻量化跨尺度跨通道融合颈部CCFM助力YOLOv11有效涨点

到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,目前本专栏免费阅读(暂时,大家尽早关注不迷路~)如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~YOLOv11有效涨点专栏——本专栏持续复习各种顶会内容——科研必备​ycb。

Snu77的博客 8105

机器人在果园内行巡检仿真(2D雷达、realsense相机)

DiffBot是一种具有两个轮子的自主差动驱动机器人。它的主要处理单元是一个运行Ubuntu Mate 20.04和ROS 1 (ROS Noetic)中间件的Raspberry pi4b。该存储库包含ROS驱动包、真实机器人的ROS控制硬件接口和模拟DiffBot的配置。修改配置文件orchard_world.launch。

如果想成为中心,那么就到中心去吧。 1316

YOLOv8工业缺陷检测实战:攻克光照不均难题,精度从70%到92%的全流程优化(附缺陷数据集)

YOLOv8在工业缺陷检测中突破光照不均难题,核心是“硬件控光+数据造景+模型聚焦+训练攻坚硬件控光:从源头减少光照变异,降低算法难度;数据造景:用CLAHE、合成样本让模型“见过足够多的光照情况”;模型聚焦:用ECA注意力和Focal-EIoU损失,让模型盯住缺陷而非光照;训练攻坚:分阶段+难例挖掘,强迫模型学习薄弱点。这套方案已在汽车轴承、手机外壳、PCB板3类产线落地,平均精度提升20%+,漏检率控制在0.08%以内,完全满足工业级要求。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1202

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

上一篇: 35、支持向量机中的不变性与学习理论
下一篇: 15、深入理解Gitflow与Forking工作流
sql99
博客等级 码龄19年 171粉丝 1905原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址