如何在C ++中的类对象中实现百分比机会函数?

时间:2015-11-02 05:36:51

标签: c++

我有一个家庭作业,要求我计算用户输入数字1到3的百分比几率。但是,我不知道如何做到这一点。 这是我的代码,但不是全部代码:

void SwingAtBall( Ball *tBall ) {

std::cout << "How hard do you want to hit the ball? Please enter a number between 1 to 3." << std::endl;

    int tBallHit;
    std::cin >> tBallHit;
    if ( tBallHit > 3 || tBallHit < 1 ) {
        std::cout << "That is not a valid value. Please enter a number between 1 to 3." << std::endl;
        std::cin >> tBallHit;
    }


}
// Prompt for 1 to 3.  There is a (input * 15) percent chance the ball only goes 5 feet.  Otherwise the ball is hit a random number between 0 and (input * 150).  Print how far it went.

2 个答案:

答案 0 :(得分:0)

如果我的理解是正确的,那么球有5英尺(100 - (输入* 15))百分比的可能性(输入* 15%)将从0到(输入* 150)脚,然后下面的代码将计算你正在寻找...

#include <iostream>
#include <ctime>

using namespace std;

int main()
{       

    int inp, chance1, dist1, dist2, dist_final;

    do {
        cout << "Enter integer between 1 and 3: ";
        cin >> inp;
    } while (inp < 0 || inp > 3);

    dist1 = 5;

    srand(time(0));
    dist2 = rand() % (inp * 150);

    chance1 = (inp * 15) / 100;

    dist_final = chance1 * dist1 + (1 - chance1) * dist2;

    cout << "It went this far: " << dist_final << endl;

    // system("pause");
    return 0;
}

答案 1 :(得分:0)

对于像这样的事情,整个nuspec file

简单的百分比很简单,你可以使用整数。选择0-99或1-100,并在<random> header中使用它。当然,15%是3/20所以你也可以使用select dateid, value, (select avg(value) from mytable t2 where t2.dateid between (DATE_SUB(date(t1.dateid),INTERVAL 6 day)+0) and t1.dateid) as avg_past_7 from mytable t1 where dateid between 20150101 and 20150201 order by dateid; ,但这更加模糊。

&#34; 0到(输入* 150)&#34;是uniform_int_distribution(1,20)