当前位置: 首页 > 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>…...

是德N5173B信号发生器说明手册

是德科技&#xff08;Keysight Technologies&#xff09;的N5173B是一款高性能射频和微波信号发生器&#xff0c;属于EXG系列。该设备广泛应用于通信、雷达、航空航天等领域的研发与测试&#xff0c;提供高精度、高稳定性的信号输出。频率范围与输出功率频率范围&#xff1a;覆…...

网络丢包怎么排查?一文讲透从现象确认、抓包定位到链路归因的完整方法

网络丢包怎么排查&#xff1f;一文讲透从现象确认、抓包定位到链路归因的完整方法 **一句话定义&#xff1a;**网络丢包排查&#xff0c;不是简单看一个丢包率数字&#xff0c;而是要回答“包丢在什么位置、在什么条件下丢、对业务到底造成了什么影响”。 很多团队一看到应用变…...

S2-Pro开源项目协作:使用Git进行团队开发的AI辅助最佳实践

S2-Pro开源项目协作&#xff1a;使用Git进行团队开发的AI辅助最佳实践 1. 为什么需要AI辅助的Git协作 在开源项目开发中&#xff0c;团队协作效率直接影响项目进度和质量。传统的Git工作流虽然强大&#xff0c;但对于新手来说&#xff0c;分支管理、代码冲突解决等环节仍然存…...

避开这些坑!HC32F460正交编码器调试心得:Timer6 vs TimerA 如何选?滤波与中断配置详解

HC32F460正交编码器实战指南&#xff1a;Timer6与TimerA的深度对比与避坑策略 当伺服电机的旋转精度需要控制在0.1度以内时&#xff0c;正交编码器的信号处理质量直接决定了整个控制系统的性能上限。HC32F460作为工业级MCU&#xff0c;其Timer6和TimerA模块都支持正交编码器接口…...

LinkSwift:八大网盘直链解析工具,突破下载限制的智能解决方案

LinkSwift&#xff1a;八大网盘直链解析工具&#xff0c;突破下载限制的智能解决方案 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 &#xff0c;支持 百度网盘 / 阿里云盘 / 中…...

告别色彩失真:flv.js如何让YUV视频在浏览器绚丽绽放

告别色彩失真&#xff1a;flv.js如何让YUV视频在浏览器绚丽绽放 【免费下载链接】flv.js HTML5 FLV Player 项目地址: https://gitcode.com/gh_mirrors/fl/flv.js 在数字视频播放的世界里&#xff0c;色彩还原度直接影响着观看体验。HTML5 FLV Player&#xff08;flv.js…...

别再死记硬背了!用COMSOL搞懂有限元,从‘弱形式’到网格剖分的实战避坑指南

别再死记硬背了&#xff01;用COMSOL搞懂有限元&#xff0c;从‘弱形式’到网格剖分的实战避坑指南 理工科研究者常陷入一个怪圈&#xff1a;能熟练点击COMSOL的每个按钮&#xff0c;却在求解失败时手足无措。当网格剖分警告弹出&#xff0c;当相对容差反复调整仍不收敛&#x…...

从零开始学习AI漫剧,好课优选告诉您思路要转变

想从零开始学习AI漫剧&#xff0c;最关键的好课优选告诉你&#xff1a;不要试图先学完所有理论再动手&#xff0c;而是应该先动手做出一个“粗糙”的作品&#xff0c;在实践中遇到问题&#xff0c;再针对性地学习。 这里为你规划了一条清晰的学习路径&#xff0c;分为四个阶段&…...

2026年大模型系统学习路线:从零基础到落地实战,少走90%弯路

当大模型从“技术热点”走向“产业刚需”&#xff0c;无论是想切入AI赛道的零基础小白&#xff0c;还是想提升竞争力的程序员、产品经理&#xff0c;掌握大模型学习方法都成为必备能力。但大模型知识体系庞大&#xff0c;涵盖数学、编程、深度学习、工程化等多个领域&#xff0…...

嵌入式开发第一步:在VMware里为Ubuntu 22.04.3 LTS做好这些基础配置(含root、换源)

嵌入式开发环境搭建&#xff1a;Ubuntu 22.04 LTS基础配置全指南 当你刚完成Ubuntu 22.04 LTS的安装&#xff0c;兴奋地准备开始嵌入式Linux开发之旅时&#xff0c;可能会发现系统还远未准备好迎接复杂的交叉编译和内核开发工作。本文将带你完成那些容易被忽略却至关重要的基础…...