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

相关文章:

  • RAGFlow 拉取 Docker 镜像失败
  • 压力测试等工具源码包编译及使用方法
  • 基于python高校固定资产管理系统
  • 【银行测试】保险项目测试点+测试流程详情(二)
  • scanpy单细胞转录组python教程(一):不同形式数据读取
  • java报错“ NoSuchMethodError:com.test.Service.doRoomList(Ljava/lang/String;)V解决方案
  • Gin 框架错误处理机制详解
  • 线性代数1000题学习笔记
  • 如何将PDF文档进行高效编辑处理!
  • NLP学习开始-02逻辑回归
  • 【Spring IoC 核心实现类详解:DefaultListableBeanFactory】
  • 从策略梯度到 PPO
  • Linux权限管理终极指南(用户身份与文件权限
  • Python中的 __name__
  • 计算机视觉(CV)——pytorch张量基本使用
  • imx6ull-驱动开发篇17——linux原子操作实验
  • docker等基础工具使用
  • 个人笔记Mybatis2
  • 第一章 概述
  • 快速了解DBSCAN算法
  • reinterpret_cast and static cast
  • Docker实战:为项目打造即开即用的宝塔LNMP环境
  • redis集群-docker环境
  • 【从源码角度深度理解 CPython 的垃圾回收机制】:第2课循环引用:标记清除-分代回收
  • 机器学习线性归回实战(单因子和多音字分别建立预测房价模型)
  • 一个基于 Next.js 和 Puppeteer 的 Markdown 转图片服务,支持 Docker 部署和 API 集成
  • Node.js面试题及详细答案120题(01-15) -- 基础概念篇
  • python | numpy小记(十):理解 NumPy 中的 `np.random.multinomial`(进阶)
  • Stlink识别不到-安装驱动
  • 医防融合中心-智慧化慢病全程管理医疗AI系统开发(下)