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

第4章 程序段的反复执行4 多重循环练习(题及答案)

(1)程序阅读

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main(){int i,j,n;cin >> n;for(i = 1; i <= n; i++){for(j = 1; j <= n - i;j++)cout << " ";for(j = 1; j < i; j++)cout << "*";cout << endl;}return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main(){int i,j,n;cin >> n;for(i = 2; i <= n; i++){j = i - 1;while(j > 1 && i % j != 0)j--;cout << i << "(" << j << ")\n";}return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int i, m, n = 0;for(i = 1; i <= 5; i++) {m = i % 2;while(m-- > 0) n++;}cout << m << "," << n;return 0;
}
-1,3


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

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int n;cin >> n;assert(1 <= n && n <= 20);for (int row = 1; row <= n; row++) {for (int col = 1; col <= n + row - 1; col++) {if (col <= n - row) { cout << " ";} else { cout << "*";}}cout << endl;}return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int n;cin >> n;assert(1 <= n && n <= 10);for(int row = 1; row <= n; row++) {int i = 1;for(int col = 1; col <= n + row - 1; col++) {if(col < n - row + 1) {cout << " ";} else {cout << i++;}}cout << endl;}for(int row = n - 1; row >= 1; row--) {int i = 1;for(int col = 1; col <= n + row - 1; col++) {if(col < n - row + 1) {cout << " ";} else {cout << i++;}}cout << endl;}return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int n;cin >> n;assert(1 <= n && n <= 9);for(int row = 1; row <= n; row++) {for(int col = 1; col <= row; col++) {cout << col << "*" << row << "=" << col*row << " ";}cout << endl;}return 0;
}

#include <bits/stdc++.h>
using namespace std;
//汤永红
int main() {int n;cin >> n;assert(100 <= n);int ways = 0;for(int i = 0; i <= n / 50; i++) {ways += (n - i * 50) / 20 + 1;}cout << ways << endl;return 0;
}

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

典型的数论题目,考查的是最大公约数(gcd)与最小公倍数(lcm)的定义和性质。

#include <iostream>
using namespace std;
int main() {int x0, y0;cin >> x0 >> y0;if (y0 % x0 != 0) {cout << 0 << endl; // 如果不能整除,直接输出0return 0;}int k = y0 / x0;int count = 0;for (int a = 1; a * a <= k; ++a) {if (k % a == 0) {int b = k / a;// 计算 a 和 b 的最大公约数(不用函数)int m = a, n = b;while (n != 0) {int r = m % n;m = n;n = r;}int d = m; // 此时 d = gcd(a, b)if (d == 1) {if (a == b)count += 1; // (a, a) 只算一种elsecount += 2; // (a, b) 和 (b, a) 算两种}}}cout << count << endl;return 0;
}

http://www.dtcms.com/a/323823.html

相关文章:

  • Audio Flamingo
  • 网站升级https地址方法
  • LeetCode每日一题,2025-8-10
  • jmeter常规压测【读取csv文件】
  • BGP HCIP
  • 繁花深处:花店建设的时代意义与多元应用—仙盟创梦IDE
  • 农经权二轮延包—已有软件与后续研究
  • 线性代数1000题学习笔记
  • 从街亭失守看管理
  • Datawhale AI 夏令营——全球AI攻防挑战赛(AIGC技术-图像方向)
  • LLaMA-Adapter V2 Parameter-Efficient Visual Instruction Model
  • 快速了解DBSCAN算法
  • 分布微服务电商订单系统Rust编码开发[下]
  • 数据结构:树
  • 分布微服务电商订单系统Rust编码开发[上]
  • 代码随想录算法训练营第六十天|图论part10
  • sqllabs——Less1
  • 【每天一个知识点】深度领域对抗神经网络
  • 医防融合中心-智慧化慢病全程管理医疗AI系统开发(下)
  • 零基础学Java第二讲---数据类型与变量
  • 什么是ABA问题?
  • Day 10: Transformer完整架构详解 - 从位置编码到编解码器的全面剖析
  • 【QT】常⽤控件详解(七)容器类控件 GroupBox TabWidget 布局管理器 Spacer
  • 大型动作模型LAM:让企业重复任务实现80%效率提升的AI技术架构与实现方案
  • 复杂项目即时通讯从android 5升级android x后遗症之解决 ANR: Input dispatching timed out 问题 -优雅草卓伊凡
  • 【东枫科技】 FR2 Massive MIMO 原型验证与开发平台,8*8通道
  • Linux 系统中,如何处理信号以避免竞态条件并确保程序稳定性?
  • 【实证分析】上市公司技术创新持续性数据分析-含代码(2008-2023年)
  • 【嵌入式】嵌入式硬件相关基础知识
  • 计算机网络:广播地址就是默认子网中最大的IP地址吗?