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

cs106x-lecture11(Autumn 2017)-SPL实现

打卡cs106x(Autumn 2017)-lecture11

(以下皆使用SPL实现,非STL库,后续课程结束会使用STL实现)

1、diceRolls

Write a recursive function named diceRolls accepts an integer representing a number of 6-sided dice to roll, and output all possible combinations of values that could appear on the dice. For example, the call of diceRolls(3); should print:

{1, 1, 1}
{1, 1, 2}
{1, 1, 3}
{1, 1, 4}
{1, 1, 5}
{1, 1, 6}
{1, 2, 1}
{1, 2, 2}...
{6, 6, 4}
{6, 6, 5}
{6, 6, 6}

If the number of digits passed is 0 or negative, print no output. Your function must use recursion, but you can use a single for loop if necessary.

解答:

#include <iostream>
#include "console.h"
#include "vector.h"using namespace std;void diceRollsHelper(int n, Vector<int>& v) {if (n == 0) {cout << v << endl;} else {for (int i = 1; i <= 6; i++) {v.add(i);diceRollsHelper(n - 1, v);v.remove(v.size() - 1);}}
}void diceRolls(int n) {if (n >= 0) {Vector<int> v;diceRollsHelper(n, v);}
}int main() {diceRolls(3);return 0;
}

2、diceSum

Write a recursive function named diceSum that accepts two parameters: an integer representing a number of 6-sided dice to roll, and a desired sum, and output all possible combinations of values that could appear on the dice that would add up to exactly the given sum. For example, the call of diceSum(3, 7); should print all combinations of rolls of 3 dice that add up to 7:

{1, 1, 5}
{1, 2, 4}
{1, 3, 3}
{1, 4, 2}
{1, 5, 1}
{2, 1, 4}
{2, 2, 3}
{2, 3, 2}
{2, 4, 1}
{3, 1, 3}
{3, 2, 2}
{3, 3, 1}
{4, 1, 2}
{4, 2, 1}
{5, 1, 1}

If the number of digits passed is 0 or negative, or if the given number of dice cannot add up to exactly the given sum, print no output. Your function must use recursion, but you can use a single for loop if necessary.

解答:

#include <iostream>
#include "console.h"
#include "vector.h"using namespace std;void diceSumHelper(int n, int desired, int sum, Vector<int>& v) {if (n == 0 && sum == desired) {cout << v << endl;} else {for (int i = 1; i <= 6; i++) {int min = sum + i + 1 * (n - 1);int max = sum + i + 6 * (n - 1);if (desired >= min && desired <= max) {v.add(i);diceSumHelper(n - 1, desired, sum + i, v);v.remove(v.size() - 1);}}}
}void diceSum(int n, int desired) {if (n >= 0) {Vector<int> v;int sum = 0;diceSumHelper(n, desired, sum, v);}
}int main() {diceSum(3, 7);return 0;
}

3、printSubVectors 

Write a recursive function named printSubVectors that accepts a reference to a vector of integer values as a parameter and that prints console output (to cout) displaying all possible sub-vectors that can be created from some subset of the vector's elements. For example, if a vector variable v stores the elements {1, 2, 3}, the call of printSubVectors(v); should print the following output to the console:

{}
{3}
{2}
{2, 3}
{1}
{1, 3}
{1, 2}
{1, 2, 3}

Your function can print the sub-vectors in any order, but the sub-vectors you print should preserve the order of the values from the vector. For example, the sub-vectors of {42, 23} should be listed as:

{}
{23}
{42}
{42, 23}

You may assume the vector passed contains no duplicate values. If passed an empty vector, print only a single line containing the empty vector itself, {} .

Constraints: You may define helper functions if you like. You are allowed to construct exactly one auxiliary collection of your choice from the collections we have studied (one vector, set, map, stack, queue, array, etc.). That is, at most one collection can be constructed in total for each call a client makes on your function. Note that a compound collection, such as a Vector of Vectors, is considered to be more than one auxiliary collection in this context. Your function should not alter the contents of the vector that is passed as a parameter; declare your function's header in such a way to assure the client that you will not do so. You may not use any loops to solve this problem; you must use recursion.

解答:

#include <iostream>
#include "console.h"
#include "vector.h"using namespace std;void printSubVectorsHelper(Vector<int>& v, Vector<int>& chosen){if (v.isEmpty()) {cout << chosen << endl;} else {int n = v[0];v.remove(0);chosen.add(n);printSubVectorsHelper(v, chosen);chosen.remove(chosen.size() - 1);printSubVectorsHelper(v, chosen);v.insert(0, n);}
}void printSubVectors(Vector<int> v) {if (v.size() == 0) {cout << v << endl;} else {Vector<int> chosen;printSubVectorsHelper(v, chosen);}
}int main() {printSubVectors({1, 2, 3});return 0;
}

相关文章:

cs106x-lecture11(Autumn 2017)-SPL实现

打卡cs106x(Autumn 2017)-lecture11 (以下皆使用SPL实现&#xff0c;非STL库&#xff0c;后续课程结束会使用STL实现) 1、diceRolls Write a recursive function named diceRolls accepts an integer representing a number of 6-sided dice to roll, and output all possibl…...

负载均衡集群( LVS 相关原理与集群构建 )

目录 1、LVS 相关原理 1.1、LVS集群的体系结构以及特点 1.1.1 LVS简介 1.1.2 LVS体系结构 1.1.3 LVS相关术语 1.1.4 LVS工作模式 1.1.5 LVS调度算法 1.2 LVS-DR集群介绍 1.2.1 LVS-DR模式工作原理 1.2.2 LVS-DR模式应用特点 1.2.3 LVS-DR模式ARP抑制 1.3 LVS – NA…...

【分布式】Hadoop完全分布式的搭建(零基础)

Hadoop完全分布式的搭建 环境准备&#xff1a; &#xff08;1&#xff09;VMware Workstation Pro17&#xff08;其他也可&#xff09; &#xff08;2&#xff09;Centos7 &#xff08;3&#xff09;FinalShell &#xff08;一&#xff09;模型机配置 0****&#xff09;安…...

基于Java+Swing+Mysql实现人事管理信息系统

基于JavaSwingMysql实现人事管理信息系统 一、系统介绍二、功能展示1.用户登陆2.用户注册3.员工信息添加、删除4.员工信息查询、修改5.部门管理6、员工考核 三、数据库四、其它1.其他系统实现五.获取源码 一、系统介绍 系统功能&#xff1a;用户登陆、用户注册、员工信息添加、…...

DeepSeek与ChatGPT:会取代搜索引擎和人工客服的人工智能革命

云边有个稻草人-CSDN博客 在众多创新技术中&#xff0c;DeepSeek和ChatGPT无疑是最为引人注目的。它们通过强大的搜索和对话生成能力&#xff0c;能够改变我们与计算机交互的方式&#xff0c;帮助我们高效地获取信息&#xff0c;增强智能服务。本文将深入探讨这两项技术如何结合…...

企业级RAG开源项目分享:Quivr、MaxKB、Dify、FastGPT、RagFlow

企业级 RAG GitHub 开源项目深度分享&#xff1a;Quivr、MaxKB、Dify、FastGPT、RagFlow 及私有化 LLM 部署建议 随着生成式 AI 技术的成熟&#xff0c;检索增强生成&#xff08;RAG&#xff09;已成为企业构建智能应用的关键技术。RAG 技术能够有效地将大型语言模型&#xff…...

js基础知识总结

1、js数据类型有哪些&#xff1f;存储区别 js基础类型及引用类型存储区别代码示例如下&#xff1a; // 基本数据类型 let a 10; let b a; // b 是 a 的一个副本 b 20; // 修改 b 不会影响 …...

LearnOpenGL——高级OpenGL(下)

教程地址&#xff1a;简介 - LearnOpenGL CN 高级数据 原文链接&#xff1a;高级数据 - LearnOpenGL CN 在OpenGL中&#xff0c;我们长期以来一直依赖缓冲来存储数据。本节将深入探讨一些操作缓冲的高级方法。 OpenGL中的缓冲本质上是一个管理特定内存块的对象&#xff0c;它…...

vue脚手架开发打地鼠游戏

游戏设计&#xff1a; 规划游戏的核心功能&#xff0c;如场景、随机出现的地鼠、计分系统、游戏时间限制等。简单设计游戏流程&#xff0c;包括开始界面、游戏进行中、关卡设置&#xff08;如不同关卡地鼠出现数量、游戏时间等&#xff09;、关卡闯关成功|失败、游戏结束闯关成…...

uniapp 连接mqtt

1&#xff1a;下载插件 npm install mqtt 2&#xff1a;创建 mqtt.js /* main.js 项目主入口注入实例 */ // import mqttTool from ./lib/mqttTool.js // Vue.prototype.$mqttTool mqttTool/* 使用范例见 /pages/index/index.vue */ // mqtt协议&#xff1a;H5使用ws/wss APP-…...

EX_25/2/19

1. 封装一个 File 类&#xff0c;用有私有成员 File* fp 实现以下功能 File f "文件名" 要求打开该文件 f.write(string str) 要求将str数据写入文件中 string str f.read(int size) 从文件中读取最多size个字节&#xff0c;并将读取到的数据返回 析构函数 …...

Breakout Tool

思科 CML 使用起来还是很麻烦的&#xff0c;很多操作对于习惯了 secure crt 或者 putty 等工具的网络工程师都不友好。 Breakout Tool 提供对远程实验室中虚拟机控制台与图形界面的本地化接入能力&#xff0c;其核心特性如下&#xff1a; Console 访问&#xff1a;基于 Telnet…...

【大模型】DeepSeek:AI浪潮中的破局者

【大模型】DeepSeek&#xff1a;AI浪潮中的破局者 引言&#xff1a;AI 新时代的弄潮儿DeepSeek&#xff1a;横空出世展锋芒&#xff08;一&#xff09;诞生背景与发展历程&#xff08;二&#xff09;全球影响力初显 探秘 DeepSeek 的技术内核&#xff08;一&#xff09;独特的模…...

Kafka 简介

Kafka 简介 Apache Kafka 是一个开源的分布式流处理平台&#xff0c;广泛应用于实时数据流处理、日志管理、消息传递等场景。Kafka 最初由 LinkedIn 开发&#xff0c;并于 2011 年捐献给 Apache 软件基金会。 Kafka 的设计目标是高吞吐量、低延迟和高可用性&#xff0c;它能够…...

什么是掉期(Swap)?——金融衍生品的关键工具(中英双语)

什么是掉期&#xff08;Swap&#xff09;&#xff1f;——金融衍生品的关键工具 引言 掉期&#xff08;Swap&#xff09; 是金融市场中最重要的衍生品之一&#xff0c;它允许两方交换未来的现金流&#xff0c;以优化融资成本、规避利率或汇率风险&#xff0c;甚至进行投机交易…...

深入解析 Vue 项目中的缓存刷新机制:原理与实战

目录 前言1. Demo2. 知识拓展 前言 在 Vue 项目中&#xff0c;缓存通常用于存储用户信息、角色权限、系统设置等&#xff0c;以提高页面加载速度并减少 API 请求 这里使用 web-storage-cache 作为封装的本地存储工具&#xff0c;支持 localStorage 和 sessionStorage 方式存储…...

【C++】 Flow of Control

《C程序设计基础教程》——刘厚泉&#xff0c;李政伟&#xff0c;二零一三年九月版&#xff0c;学习笔记 文章目录 1、选择结构1.1、if 语句1.2、嵌套的 if 语句1.3、条件运算符 ?:1.4、switch 语句 2、循环结构2.1、while 语句2.2、do-while 语句2.3、 for 循环2.4、循环嵌套…...

【异常错误】pycharm debug view变量的时候显示不全,中间会以...显示

异常问题&#xff1a; 这个是在新版的pycharm中出现的&#xff0c;出现的问题&#xff0c;点击view后不全部显示&#xff0c;而是以...折叠显示 在setting中这么设置一下就好了&#xff1a; 解决办法&#xff1a; https://youtrack.jetbrains.com/issue/PY-75568/Large-stri…...

2.19c++练习

1.封装一个mystring类 拥有私有成员&#xff1a; char* p int len 需要让以下代码编译通过&#xff0c;并实现对应功能 mystring str "hello" mystring ptr; ptr.copy(str) ptr.append(str) ptr.show() 输出ptr代表的字符串 ptr.compare(str) 比较ptr和…...

【为什么使用`new DOMParser`可以保持SVG命名空间】

为什么使用new DOMParser可以保持SVG命名空间&#xff1a; 一、命名空间基础概念 1. XML命名空间定义 <svg xmlns"http://www.w3.org/2000/svg"><!-- 此元素及其子元素属于SVG命名空间 --><rect x"10" y"20"/> </svg>…...

web vue 项目 Docker化部署

Web 项目 Docker 化部署详细教程 目录 Web 项目 Docker 化部署概述Dockerfile 详解 构建阶段生产阶段 构建和运行 Docker 镜像 1. Web 项目 Docker 化部署概述 Docker 化部署的主要步骤分为以下几个阶段&#xff1a; 构建阶段&#xff08;Build Stage&#xff09;&#xff1a…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动

一、前言说明 在2011版本的gb28181协议中&#xff0c;拉取视频流只要求udp方式&#xff0c;从2016开始要求新增支持tcp被动和tcp主动两种方式&#xff0c;udp理论上会丢包的&#xff0c;所以实际使用过程可能会出现画面花屏的情况&#xff0c;而tcp肯定不丢包&#xff0c;起码…...

三维GIS开发cesium智慧地铁教程(5)Cesium相机控制

一、环境搭建 <script src"../cesium1.99/Build/Cesium/Cesium.js"></script> <link rel"stylesheet" href"../cesium1.99/Build/Cesium/Widgets/widgets.css"> 关键配置点&#xff1a; 路径验证&#xff1a;确保相对路径.…...

边缘计算医疗风险自查APP开发方案

核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...

(二)TensorRT-LLM | 模型导出(v0.20.0rc3)

0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述&#xff0c;后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作&#xff0c;其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...

STM32标准库-DMA直接存储器存取

文章目录 一、DMA1.1简介1.2存储器映像1.3DMA框图1.4DMA基本结构1.5DMA请求1.6数据宽度与对齐1.7数据转运DMA1.8ADC扫描模式DMA 二、数据转运DMA2.1接线图2.2代码2.3相关API 一、DMA 1.1简介 DMA&#xff08;Direct Memory Access&#xff09;直接存储器存取 DMA可以提供外设…...

基础测试工具使用经验

背景 vtune&#xff0c;perf, nsight system等基础测试工具&#xff0c;都是用过的&#xff0c;但是没有记录&#xff0c;都逐渐忘了。所以写这篇博客总结记录一下&#xff0c;只要以后发现新的用法&#xff0c;就记得来编辑补充一下 perf 比较基础的用法&#xff1a; 先改这…...

Keil 中设置 STM32 Flash 和 RAM 地址详解

文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...

sqlserver 根据指定字符 解析拼接字符串

DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...