C ++中的概率代码 [英] Code with probability in C++

查看:158
本文介绍了C ++中的概率代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用C ++进行编码还是比较陌生的,并且想知道是否有人可以协助我指出完成项目的正确方向。我正在尝试创建一个程序,通过使用概率向用户分发齿轮。我的程序将有12种可能性来选择4类物品。例如,我将有2把匕首,3个头盔,3个盾牌和4个护甲。在这一点上,我完全困惑,我已经尝试研究我可以使用的信息,但我不确定这个项目需要哪些C ++概念。任何帮助将不胜感激!我不希望任何人以任何方式完成项目,我只是寻求帮助,指导我找到正确的方向,找到我需要学习的主题才能完成这个项目。非常感谢大家!



原始问题:



你将实施一个程序,提供装备特定游戏的玩家。您可以根据自己的意愿设置游戏主题。您的程序将使用概率来决定向玩家分发的装备。装备包括匕首,头盔,盾牌和盔甲。



您的计划:



所有将要发放的装备都用一个袋子里的代币表示。首先,令牌包中有2个匕首令牌,3个头盔代币,3个盾牌代币和4个护甲代币。程序将从包中选择一个代币并将该装备交给玩家。为了模拟选择哪个标记,您将计算选择每个齿轮的概率,并以最高的选择概率分配齿轮。如果存在平局,则用户以相同的概率获得两个(或全部)齿轮。在分发齿轮之前,您应该在表格中显示每种齿轮的概率。参见样本输出以了解如何格式化概率。记住,你不能直接比较双打。你必须使用abs(x-y)<进行比较。 EPSILONwhere EPSILON是一个非常小的数字,如1e-14。



这是输出屏幕截图的一个例子:

http://prntscr.com/l2qg5y [ ^ ]



我尝试过:



我知道我的代码现在非常蹩脚,但任何帮助都将不胜感激!



I am fairly new to coding with C++ and was wondering if anyone could assist in pointing me in the right direction to finishing a project. I am trying to create a program that distributes "gear" to the user by using probability. My program will have a pool of 12 possibilities to choose 4 categories of items. For example, I will have 2 daggers, 3 helmets, 3 shields and 4 armor. At this point I am completely confused and I have tried researching for information that I may be able to use, but I am not sure which concepts of C++ will be required for this project. Any help will be appreciated! I do not wish for the project to be completed in anyway by anyone and I am just looking for help to guide me in the right direction to find topics I need to learn in order to complete this project. Thanks a lot everyone!

Original problem:

You will implement a program that hands out gear to the players of a particular game. You may theme your game as you wish.Your program will use probabilities to decide what gear to hand out to the player. The gear includes a dagger, a helmet, a shield, and armor.

Your Program:

All of the gear that will be handed out is represented by a token in a bag. To begin, there are 2 daggers tokens, 3 helmet tokens, 3 shield tokens, and 4 armor tokens in the Token Bag.The program will choose a token from the bag and give that gear to the player. To simulate which token was chosen, you will compute the probability of choosing each piece of gear and hand out the gear with the highest probability of being chosen. If there is a tie, the user gets both (or all)of the gear with the same probability.You should display the probabilities of each kind of gear in a table before handing out the gear.See the sample output for how to format the probabilities.Remember, you cannot directly compare doubles. You have to compare using abs(x-y) < EPSILONwhere EPSILON is some very small number, like 1e-14.

This is an example of the output screen shot:
http://prntscr.com/l2qg5y[^]

What I have tried:

I know my code is super lame right now, but any help would be greatly appreciated!

// CSa262_Lab2.cpp : This file contains the 'main' function. Program 
execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
cout << "------------------------------------------------------------------- 
-------------" << endl;
cout << "Earning Gear Program" << endl;
cout << "------------------------------------------------------------------- 
-------------" << endl;
cout << "This program will simulate probability of earning different pieces 
of gear." << endl;
cout << "------------------------------------------------------------------- 
-------------" << endl;

int numArmTokens = 3;
int numHelmTokens = 3;
int numShieldTokens = 3;
int numDaggTokens = 2;
int totalTokens = (numArmTokens + numHelmTokens + numShieldTokens + 
numDaggTokens);
char ans;
int x = 1;

cout << "Your possabilites for gear are: " << endl;
do
{
    cout << "P(Armor) = " << numArmTokens << "/" << totalTokens << endl;
    cout << "P(Dagger) = " << numDaggTokens << "/" << totalTokens << endl;
    cout << "P(Helmet) = " << numHelmTokens << "/" << totalTokens << endl;
    cout << "P(Shield) = " << numShieldTokens << "/" << totalTokens << endl;


    if (numArmTokens >= numDaggTokens || numArmTokens >= numHelmTokens ||
        numArmTokens >= numShieldTokens)
    {

        cout << "You have a choice of: " << endl << ++x << ". Armor" << 
endl;
        cin >> x;
        numArmTokens--;
        totalTokens--;


    }


    if (numDaggTokens >= numArmTokens || numDaggTokens >= numHelmTokens ||
        numDaggTokens >= numShieldTokens)
    {

        cout << "You have a choice of: " << endl << "Dagger" << endl;
        cin >> x;

        numDaggTokens--;
        totalTokens--;
    }

    if (x > 4)
    {
        cout << "Please enter a valid option." << endl;
    }

    cout << "Would you like to select more gear? (Y/N)" << endl;
    cin >> ans;
} while (ans == 'y' || ans =='Y');



system("pause");
return 0;
}

推荐答案

您需要提供一些业务逻辑来分配设备。您可以使用 rand 功能生成一些分发概率的随机数据。链接带有适合您需求的示例代码。注意整数除法,它会减少分数。



问题往往是边缘情况,所以要注意一个完整的逻辑。像所有盔甲一样必须分发。
You need to provide some business logic for distributing gear. You can use the rand function for generating some random data which is distributing the probality. Link is with sample code which should suit for your needs. Watch out for integer division, which cuts the fractions.

The problem are often the edge case, so watch out for a complete logic. Like all armour must distributed.


这篇关于C ++中的概率代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆