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

第4章 程序段的反复执行2 while语句P128练习题(题及答案)

((1)阅读程序

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main(){int n,s=0;cin >> n;while(n){s = s * 10 + n % 10;n /= 10;}cout << s << endl;return 0;
}

分别输入:0 1024 1234567890

输出结果:0 4201 987654321

((2)阅读程序

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main(){int n;cin >> n;while(n != 0){cout << n % 2;n /= 2;}return 0;
}

输输入: 4 0

输输出:001 无输出

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main(){int n,sum = 0;cin >> n;while(n){sum += n % 10;n /= 10;} cout << sum << endl;return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int n, m, i, j;cin >> n >> m;i = n;j = m;while(i != j) {if(i > j)i -= j;elsej -= i;}if(i == 1)cout << "Yes";elsecout << "No";return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main(){int l,r,ans = 0;cin >> l >> r;for(int i = l;i <= r; i++){//拆位int x = i;while(x){int c = x % 10;if(c == 2) ans++;x /= 10;} }cout << ans << endl;return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int n, K;double Sn, An;printf("Please input K(1<=K<=15):");scanf("%d", &K);if ((K >= 1) && (K <= 15)) {n = 1;while(1) {An = 1.0 / n;Sn += An;if (Sn > K) {break;}n++;}printf("n=%d,Sn=%f\n", n, Sn);} else {printf("Input error!Please input again!\n");}return 0;
}

I(2016)love(08)China(15)!
L(2016)oryh(08)Fklqd(15)!
#include <bits/stdc++.h>
using namespace std;
//汤永红
char c;
int main() {while((c = getchar() ) != '\n') {if(islower(c)) {putchar('a' + (c - 'a' + 3) % 26);} else if(isupper(c)) {putchar('A' + (c - 'A' + 3) % 26);} else {putchar(c);}}return 0;
}

http://www.xdnf.cn/news/17552.html

相关文章:

  • pytorch llm 计算flops和参数量
  • Gltf 模型 加载到 Cesium 的坐标轴映射浅谈
  • 深入理解C++构造函数与初始化列表
  • Python训练营打卡Day27-类的定义和方法
  • AudioLLM
  • 专题二_滑动窗口_找到字符串中所有字母异位词
  • 第二十天:数论度量
  • 前端Web在Vue中的知识详解
  • 数据溢出ERROR L107:ADDRESS SPACE OVERFLOW
  • 11. 为什么要用static关键字
  • 【C++】string 的特性和使用
  • Python(13) -- 面向对象
  • 【面试场景题】通过LinkedHashMap来实现LRU与LFU
  • Java+Vue打造的采购招投标一体化管理系统,涵盖招标、投标、开标、评标全流程,功能完备,附完整可二次开发的源码
  • 标准IO实现
  • Effective C++ 条款32:确定你的public继承塑模出 is-a 关系
  • AWT 基本组件深入浅出:Button/Label/TextField/Checkbox/Choice/List 全面实战与性能优化
  • 2025-08-09 李沐深度学习14——经典卷积神经网络 (2)
  • MySQL相关概念和易错知识点(4)(分组查询、连接查询、合并查询、子查询)
  • Mysql笔记-系统变量\用户变量管理
  • 【LLM实战|langchain】langchain基础
  • toRef和toRefs
  • 智慧城管复杂人流场景下识别准确率↑32%:陌讯多模态感知引擎实战解析
  • Easysearch 冷热架构实战
  • Linux下管道的实现
  • SpringBoot 集成 MapStruct
  • 《从零实现哈希表:详解设计、冲突解决与优化》
  • [激光原理与应用-197]:光学器件 - 图解双折射晶体的工作原理
  • Aurora接口FPGA设计
  • C# 异步编程(使用异步Lambda表达式)