当前位置: 首页 > 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.xdnf.cn/news/1278127.html

相关文章:

  • Python day40
  • C++ list类
  • 【深度学习新浪潮】遥感图像风格化迁移研究工作介绍
  • JS中typeof与instanceof的区别
  • 腾讯云EdgeOne KV存储在游戏资源发布中的技术实践与架构解析
  • 数学建模——回归分析
  • 【GPT入门】第44课 检查 LlamaFactory微调Llama3的效果
  • 集成电路学习:什么是Parameter Server参数服务器
  • 机器学习-增加样本、精确率与召回率
  • Java开源代码源码研究:我的成长之路与实战心得分享
  • 学习分库分表的前置知识:高可用系统架构理论与实践
  • 构建企业级Odoo 18 WMS——功能、架构与拓展蓝图
  • LeetCode每日一题,2025-8-10
  • 《C语言》结构体和联合体练习题--2
  • 前端学习日记 - 前端函数防抖详解
  • 无人机集群协同三维路径规划,采用梦境优化算法(DOA)实现,Matlab代码
  • python魔法属性__doc__介绍
  • 区块链让物联网真正链接万物
  • Mysql系列--5、表的基本查询(上)
  • 【论文阅读】Deep Adversarial Multi-view Clustering Network
  • C语言:指针(2)
  • 基于ECharts的智慧社区数据可视化
  • Knuth‘s TwoSum Algorithm 原理详解
  • JS实现数组扁平化
  • 【C#补全计划】万类之父中的方法
  • Linux环境下实现简单TCP通信(c)
  • 《算法导论》第 16 章 - 贪心算法
  • [激光原理与应用-221]:设计 - 皮秒紫外激光器 - 常见技术难题、原因与解决方案
  • 博览会(树形DP)
  • 性能解析案例