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实现,非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 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完全分布式的搭建 环境准备: (1)VMware Workstation Pro17(其他也可) (2)Centos7 (3)FinalShell (一)模型机配置 0****)安…...

基于Java+Swing+Mysql实现人事管理信息系统
基于JavaSwingMysql实现人事管理信息系统 一、系统介绍二、功能展示1.用户登陆2.用户注册3.员工信息添加、删除4.员工信息查询、修改5.部门管理6、员工考核 三、数据库四、其它1.其他系统实现五.获取源码 一、系统介绍 系统功能:用户登陆、用户注册、员工信息添加、…...
DeepSeek与ChatGPT:会取代搜索引擎和人工客服的人工智能革命
云边有个稻草人-CSDN博客 在众多创新技术中,DeepSeek和ChatGPT无疑是最为引人注目的。它们通过强大的搜索和对话生成能力,能够改变我们与计算机交互的方式,帮助我们高效地获取信息,增强智能服务。本文将深入探讨这两项技术如何结合…...

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

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

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

vue脚手架开发打地鼠游戏
游戏设计: 规划游戏的核心功能,如场景、随机出现的地鼠、计分系统、游戏时间限制等。简单设计游戏流程,包括开始界面、游戏进行中、关卡设置(如不同关卡地鼠出现数量、游戏时间等)、关卡闯关成功|失败、游戏结束闯关成…...
uniapp 连接mqtt
1:下载插件 npm install mqtt 2:创建 mqtt.js /* main.js 项目主入口注入实例 */ // import mqttTool from ./lib/mqttTool.js // Vue.prototype.$mqttTool mqttTool/* 使用范例见 /pages/index/index.vue */ // mqtt协议:H5使用ws/wss APP-…...
EX_25/2/19
1. 封装一个 File 类,用有私有成员 File* fp 实现以下功能 File f "文件名" 要求打开该文件 f.write(string str) 要求将str数据写入文件中 string str f.read(int size) 从文件中读取最多size个字节,并将读取到的数据返回 析构函数 …...

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

【大模型】DeepSeek:AI浪潮中的破局者
【大模型】DeepSeek:AI浪潮中的破局者 引言:AI 新时代的弄潮儿DeepSeek:横空出世展锋芒(一)诞生背景与发展历程(二)全球影响力初显 探秘 DeepSeek 的技术内核(一)独特的模…...
Kafka 简介
Kafka 简介 Apache Kafka 是一个开源的分布式流处理平台,广泛应用于实时数据流处理、日志管理、消息传递等场景。Kafka 最初由 LinkedIn 开发,并于 2011 年捐献给 Apache 软件基金会。 Kafka 的设计目标是高吞吐量、低延迟和高可用性,它能够…...
什么是掉期(Swap)?——金融衍生品的关键工具(中英双语)
什么是掉期(Swap)?——金融衍生品的关键工具 引言 掉期(Swap) 是金融市场中最重要的衍生品之一,它允许两方交换未来的现金流,以优化融资成本、规避利率或汇率风险,甚至进行投机交易…...
深入解析 Vue 项目中的缓存刷新机制:原理与实战
目录 前言1. Demo2. 知识拓展 前言 在 Vue 项目中,缓存通常用于存储用户信息、角色权限、系统设置等,以提高页面加载速度并减少 API 请求 这里使用 web-storage-cache 作为封装的本地存储工具,支持 localStorage 和 sessionStorage 方式存储…...

【C++】 Flow of Control
《C程序设计基础教程》——刘厚泉,李政伟,二零一三年九月版,学习笔记 文章目录 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变量的时候显示不全,中间会以...显示
异常问题: 这个是在新版的pycharm中出现的,出现的问题,点击view后不全部显示,而是以...折叠显示 在setting中这么设置一下就好了: 解决办法: https://youtrack.jetbrains.com/issue/PY-75568/Large-stri…...
2.19c++练习
1.封装一个mystring类 拥有私有成员: char* p int len 需要让以下代码编译通过,并实现对应功能 mystring str "hello" mystring ptr; ptr.copy(str) ptr.append(str) ptr.show() 输出ptr代表的字符串 ptr.compare(str) 比较ptr和…...
【为什么使用`new DOMParser`可以保持SVG命名空间】
为什么使用new DOMParser可以保持SVG命名空间: 一、命名空间基础概念 1. XML命名空间定义 <svg xmlns"http://www.w3.org/2000/svg"><!-- 此元素及其子元素属于SVG命名空间 --><rect x"10" y"20"/> </svg>…...

(十)学生端搭建
本次旨在将之前的已完成的部分功能进行拼装到学生端,同时完善学生端的构建。本次工作主要包括: 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...
DockerHub与私有镜像仓库在容器化中的应用与管理
哈喽,大家好,我是左手python! Docker Hub的应用与管理 Docker Hub的基本概念与使用方法 Docker Hub是Docker官方提供的一个公共镜像仓库,用户可以在其中找到各种操作系统、软件和应用的镜像。开发者可以通过Docker Hub轻松获取所…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器
——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的一体化测试平台,覆盖应用全生命周期测试需求,主要提供五大核心能力: 测试类型检测目标关键指标功能体验基…...

(二)原型模式
原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

srs linux
下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935,SRS管理页面端口是8080,可…...
在Ubuntu中设置开机自动运行(sudo)指令的指南
在Ubuntu系统中,有时需要在系统启动时自动执行某些命令,特别是需要 sudo权限的指令。为了实现这一功能,可以使用多种方法,包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法,并提供…...

BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践
6月5日,2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席,并作《智能体在安全领域的应用实践》主题演讲,分享了在智能体在安全领域的突破性实践。他指出,百度通过将安全能力…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作
一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...

使用Spring AI和MCP协议构建图片搜索服务
目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式(本地调用) SSE模式(远程调用) 4. 注册工具提…...
【JavaSE】多线程基础学习笔记
多线程基础 -线程相关概念 程序(Program) 是为完成特定任务、用某种语言编写的一组指令的集合简单的说:就是我们写的代码 进程 进程是指运行中的程序,比如我们使用QQ,就启动了一个进程,操作系统就会为该进程分配内存…...