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>…...
《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》
引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...
渲染学进阶内容——模型
最近在写模组的时候发现渲染器里面离不开模型的定义,在渲染的第二篇文章中简单的讲解了一下关于模型部分的内容,其实不管是方块还是方块实体,都离不开模型的内容 🧱 一、CubeListBuilder 功能解析 CubeListBuilder 是 Minecraft Java 版模型系统的核心构建器,用于动态创…...
MODBUS TCP转CANopen 技术赋能高效协同作业
在现代工业自动化领域,MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步,这两种通讯协议也正在被逐步融合,形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...
2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面
代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口(适配服务端返回 Token) export const login async (code, avatar) > {const res await http…...
项目部署到Linux上时遇到的错误(Redis,MySQL,无法正确连接,地址占用问题)
Redis无法正确连接 在运行jar包时出现了这样的错误 查询得知问题核心在于Redis连接失败,具体原因是客户端发送了密码认证请求,但Redis服务器未设置密码 1.为Redis设置密码(匹配客户端配置) 步骤: 1).修…...
论文笔记——相干体技术在裂缝预测中的应用研究
目录 相关地震知识补充地震数据的认识地震几何属性 相干体算法定义基本原理第一代相干体技术:基于互相关的相干体技术(Correlation)第二代相干体技术:基于相似的相干体技术(Semblance)基于多道相似的相干体…...
HubSpot推出与ChatGPT的深度集成引发兴奋与担忧
上周三,HubSpot宣布已构建与ChatGPT的深度集成,这一消息在HubSpot用户和营销技术观察者中引发了极大的兴奋,但同时也存在一些关于数据安全的担忧。 许多网络声音声称,这对SaaS应用程序和人工智能而言是一场范式转变。 但向任何技…...
python爬虫——气象数据爬取
一、导入库与全局配置 python 运行 import json import datetime import time import requests from sqlalchemy import create_engine import csv import pandas as pd作用: 引入数据解析、网络请求、时间处理、数据库操作等所需库。requests:发送 …...
Axure 下拉框联动
实现选省、选完省之后选对应省份下的市区...
高考志愿填报管理系统---开发介绍
高考志愿填报管理系统是一款专为教育机构、学校和教师设计的学生信息管理和志愿填报辅助平台。系统基于Django框架开发,采用现代化的Web技术,为教育工作者提供高效、安全、便捷的学生管理解决方案。 ## 📋 系统概述 ### 🎯 系统定…...
