c ++明明的随机数_从列表C ++程序中随机建议电影

c ++明明的随机数

Problem statement:

问题陈述:

Write an application code that will suggest movies from a list randomly and there won't be any repeat while suggesting the movies. That means the same movie won't be suggested twice though it will be done randomly. Input will be a movie list.

编写应用程序代码,以随机建议列表中的电影,并且在建议电影时不会重复。 这意味着尽管会随机播放同一部电影,但不会两次被推荐。 输入将是电影列表。

Prerequisite: Shuffling a given array in O(n) time using Fisher-Yates algorithm

先决条件: 使用Fisher-Yates算法在O(n)时间内对给定数组进行混洗

Example:

例:

Input:
["D-DAY", "RAINCOAT", "OCTOBER","LUNCHBOX", "BARFI", "RAAZI","PIKU"]
Movie suggestion list can be
"BARFI"
"PIKU"
"RAAZI"
"OCTOBER"
"D_DAY"
"LUNCHBOX"
"RAINCOAT"

Solution

We can use Fisher-Yates random shuffling algorithm to solve this problem. Firstly, we need to create a map to store indexes of the movies as we will shuffle based on their indexes.

我们可以使用Fisher-Yates随机改组算法来解决此问题。 首先,我们需要创建一个地图来存储电影的索引,因为我们将根据电影的索引进行随机播放。

So the map will be:

因此,地图将为:

KEY	VALUE
1	"D-DAY"
2	"RAINCOAT"
3	"OCTOBER"
4	"LUNCHBOX"
5	"BARFI"
6	"RAAZI"
7	"PIKU"

So our movie list will be converted as: [1,2,3,4,5,6,7]

因此,我们的电影列表将被转换为:[1,2,3,4,5,6,7]

Then we will shuffle using the Fisher-Yates algorithm

然后,我们将使用Fisher-Yates算法进行洗牌

At each step iteration, we will suggest movie[i]. Below is the detailed algorithm for suggesting movie randomly

在每一步迭代中,我们都会建议movie [i] 。 以下是随机建议电影的详细算法

The detailed algorithm will be,

详细的算法将是

For i=n-1 to 1
Pick and element in the range [0,i-1] randomly
Swap the randomly picked element with a[i]
// since it's not going to be reshuffled again 
// as we are decrementing i , 
// thus it's guaranteed that it won't be suggested twice 
Recommend movie[a[i]]
Decrement i
End for loop

So, how this guarantees unique suggestion each time?

那么,如何保证每次的独特建议呢?

Because, we are fixing the ith element after the swap and decrementing i, so that the movie that got suggested never takes part again in swaps.

因为,我们在交换之后固定了 i 元素,并减小了i ,以便所建议的电影再也不会参与交换。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
void recommend_movie_randomly(vector<string> movies)
{
srand(time(0));
map<int, string> mymap;
int n = movies.size();
for (int i = 0; i < n; i++) {
mymap[i] = movies[i];
}
vector<int> arr(n); //stores the indexes
for (int i = 0; i < n; i++)
arr[i] = i;
//shiffling randomly and suggesting movie 
//at each iteartion
for (int i = n - 1; i >= 1; i--) {
//j will be a random no with in range 0 to i-1
int j = rand() % i;
//swap ith index with jth
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//suggest the ith movie now
cout << "Suggesting next movie...\n";
cout << mymap[arr[i]] << endl;
}
}
int main()
{
//input list
vector<string> movie_list{ "D-DAY", "RAINCOAT", "OCTOBER", "LUNCHBOX", "BARFI", "RAAZI", "PIKU" };
cout << "Recommending movies randomly from the list\n";
recommend_movie_randomly(movie_list);
return 0;
}

Output:

输出:

Recommending movies randomly from the list
Suggesting next movie...
RAAZI
Suggesting next movie...
OCTOBER
Suggesting next movie...
PIKU
Suggesting next movie...
D-DAY
Suggesting next movie...
RAINCOAT
Suggesting next movie...
LUNCHBOX



Also tagged in: Synopsys

还标记在: Synopsys

翻译自: https://www.includehelp.com/icp/suggesting-movie-randomly-from-a-list-cpp-program.aspx

c ++明明的随机数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/379498.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

邮箱服务器

一&#xff0e;邮箱服务器的基本概念 邮件的客户端&#xff1a;可以只安装在电脑上&#xff08;C/S&#xff09;的也可以是网页形式&#xff08;B/S&#xff09;的 邮件服务器&#xff1a;起到邮件的接受与推送的作用 邮件发送的协议&#xff1a; 协议&#xff1a;就是数据传输…

C#提高保存jpg图像的质量

在程序中直接生成的jpg图像&#xff0c;汉字有毛边&#xff0c;经过一番搜索&#xff0c;在msdn上发现了下面控制jpg质量系数的文章&#xff0c;修改后试了一下&#xff0c;效果确实比前面强多了。原理我也不大懂&#xff0c;把代码贴出来&#xff0c;与大家共享。 联合图…

延迟和定时器管理

文章目录1 内核中时间概念2 标准定时器jiffies和HZ定时器API标准定时器案例3 高精度定时器(HRT)高精度定时器案例4 内核中延迟和睡眠原子上下文非原子上下文1 内核中时间概念 时间概念对计算机来说有些模糊&#xff0c;事实上内核必须在硬件的帮助下才能计算和管理时间。硬件为…

Web开发工具(插件)收集

1.IE Developer Toolbar 浏览和修改&#xff0c;选定Web页上的特定元素&#xff0c;查看HTML对象的类名、ID&#xff0c;以及类似链接路径、tab顺序、快捷键等。 2.HttpWatch Professional 一款强大的网页数据分析工具,可以查看当前网页的http数据 FireFox插件 FireFox下插件实…

cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

转载&#xff0c;并经过本人补充cin、cin.get()、cin.getline()、getline()、gets()等函数的用法2007/10/27 22:51学C的时候&#xff0c;这几个输入函数弄的有点迷糊&#xff1b;这里做个小结&#xff0c;为了自己复习&#xff0c;也希望对后来者能有所帮助&#xff0c;如果有差…

Java StringBuilder subSequence()方法与示例

StringBuilder类subSequence()方法 (StringBuilder Class subSequence() method) subSequence() method is available in java.lang package. subSequence()方法在java.lang包中可用。 subSequence() method is used to return the new set of a character sequence that is a …

Linux设备驱动开发---设备树的概念

文章目录1 设备树机制命名约定别名、标签和phandleDT编译器2 表示和寻址设备SPI和I2C寻址平台设备寻址3 处理资源提取特定应用数据文本字符串单元格和无符号的32位整数布尔提取并分析子节点4 平台驱动程序与DTOF匹配风格处理非设备树平台平台数据与DT设备树&#xff08;DT&…

【转】C#中数组复制的4种方法

C#中数组复制的4种方法 from&#xff1a;http://blog.csdn.net/burningcpu/article/details/1434167今天旁边的同事MM叫我调了一段程序&#xff0c;她想复制一个数组&#xff0c;int[] pins {9,3,4,9};int [] alias pins;这里出了错误&#xff0c;也是错误的根源&#xff0c…

Java StringBuilder codePointAt()方法与示例

StringBuilder类codePointAt()方法 (StringBuilder Class codePointAt() method) codePointAt() method is available in java.lang package. codePointAt()方法在java.lang包中可用。 codePointAt() method is used to return the Unicode code point at the given indices an…

用户虚拟地址转化成物理地址,物理地址转换成内核虚拟地址,内核虚拟地址转换成物理地址,虚拟地址和对应页的关系

文章目录1. 用户虚拟地址转换成物理地址2. 内核虚拟地址转换成物理地址3. 物理地址转换成内核虚拟地址4 内核虚拟地址和对应页5 根据进程号获取进程描述符1. 用户虚拟地址转换成物理地址 static void get_pgtable_macro(void) {printk("PAGE_OFFSET 0x%lx\n", PAGE…

简单三层架构(登录)

1&#xff0c;首先导包 dao //获取数据String username request.getParameter("username");String password request.getParameter("password");//传递到Service层UserService service new UserService();//这里的UserService 需要创建到service包下Use…

通过隐藏option实现select的联动效果

开始的时候需求是根据一定条件隐藏一部分<option>标签&#xff0c;类似联动效果&#xff0c;但是目前的html规范并没有为<option>提供隐藏的效果&#xff0c;因此常用的设置display或者visibility无效。网上大部分解决方案是删除<option>节点或<option>…

Java SimpleTimeZone setEndRule()方法与示例

SimpleTimeZone类setEndRule()方法 (SimpleTimeZone Class setEndRule() method) Syntax: 句法&#xff1a; public void setEndRule(int en_mm, int en_dd, int en_time);public void setEndRule(int en_mm, int en_dd, int en_dow, int en_time);public void setEndRule(int…

Linux设备驱动开发--- DMA

文章目录1 设置DMA映射缓存一致性和DMADMA映射一致映射流式DMA映射2 完成的概念3 DMA引擎API分配DMA从通道设置从设备和控制器指定参数获取事务描述符提交事务发布待处理DMA请求并等待回调通知4 程序单缓冲区映射分散聚集映射DMA是计算机系统的一项功能&#xff0c;它允许设备在…

类加载器

一、类加载器 1&#xff0c;什么是类加载器&#xff1f; 类加载器就是用来加载字节码文件 2&#xff0c;类加载器的种类有哪些&#xff1f; 1&#xff09;BootStrap&#xff1a;引导类加载器&#xff1a;加载都是最基础的文件 2&#xff09;ExtClassLoader&#xff1a;扩展类加…

一个用java读取XML文件的简单方法(转)

XML文件 book.xml <book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first> <last>Gates</last> <age>46</age&g…

Java ObjectStreamField getName()方法与示例

ObjectStreamField类的getName()方法 (ObjectStreamField Class getName() method) getName() method is available in java.io package. getName()方法在java.io包中可用。 getName() method is used to get the name of this ObjectStreamField field. getName()方法用于获取…

【css】CSS中折叠margin的问题

为什么要翻译这篇说明&#xff1f;css2本有人已翻译过&#xff0c;但看一下&#xff0c;很粗糙&#xff08;不是说自己就怎么怎么样啊&#xff0c;翻译者真的是很值得敬佩的&#xff01;&#xff09;&#xff0c;近来跟css与xhtml接触得越来越多&#xff0c;但接触得越多&#…

算法---链表

文章目录反转链表合并两个有序链表删除重复元素反转链表 反转链表包括两种&#xff0c;反转全部元素或者反转部分元素。在这里&#xff0c;我们约定&#xff1a;数据元素类型是struct LinkNode&#xff0c;要反转链表的第一个节点是head&#xff0c;head的前面一个节点是pre&a…

SSM

二、环境设置&#xff08;MyEclipse&#xff09; 1&#xff0c;字体设置 window–>Preference->General->Appearance->Colors and Fonts->Basic Text->Font 2&#xff0c;workspace字符集设置 window–>Preference->General->Appearance->W…