| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1500 人关注过本帖
标题:初学c++ 有两道题不会但是有答案,请问能不能帮忙解释一下怎么做的!感谢!
只看楼主 加入收藏
vvvuvv
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-10-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
初学c++ 有两道题不会但是有答案,请问能不能帮忙解释一下怎么做的!感谢!
1. You are given data file(data.txt) from an accelerometer that measured the acceleration of a drag racer for 7 seconds. During this time, the car, from a standing start, ran a quarter mile race. The data you are given is in meters/sec^2 and sample very millisecond. There are seven thousand data points in the file. The file consist of 700 lines of 10 data points each, text format, floating point, space delimited. After the drag racer crossed the finish line the remainder of the file is filled with -999.99. Write a program that calculates and outputs the speed of the drag racer as it crosses the finish line in units of kilometers/hour. Remember, where a is the acceleration as a function of time. You may assume that the acceleration during each millisecond interval is constant.

程序代码:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream myfile;
    double acc[7000];
    double vel = 0;
    int ctr = 0;
    myfile.open("data.txt");
    for (int i = 0; i < 7000; i++)
        myfile >> acc[i];
    myfile.close();
    // integrate acc data until you hit -999.99
    while (acc[ctr] != -999.99) {
        vel = vel + acc[ctr]*0.001;
        ctr++;
    }
    cout << "The velocity at the end of a quarter mile is ";
    cout << vel * 3600.0 / 1000.0 << " km/sec";
    return 0;
}


2. a. Craps is a game where a player rolls two dice.  On the first roll . If the sum of the dice is 7 or 11 the wins the bet. If it is 2,3 or 12 he “craps out” and loses his money. Any other outcome and the player must roll again with further rules we won’t discuss here.  Create a function called firstRoll with the following prototype:
char firstRoll( );
&nbsp;
The function simulates the rolling of two dice and based on that returns the character ‘w’ for a win, ‘l’ for a loss, or ‘r’ for roll again. Note you must simulate the roll of each die separately and add them together. Generating a single random number between 1 and 12 will give you incorrect results.
&nbsp;
   b.  Write a program that estimates the probability of winning, losing , or rolling again on the first roll of a craps game by calling firstRoll one thousand times and keeping track of the number of times each outcome occurs. The output of the program should be:
&nbsp;
For the first craps roll  
Probability of winning is 0.xx
Probability of losing is 0.yy
Probability of having to roll again is 0.zz.

程序代码:
#include <cstdlib>
#include <iostream>

using namespace std;

char firstRoll();
int main(int argc, char** argv) {
    int wcnt=0,lcnt=0,rcnt=0;
    char outcome;
    for  (int ctr=0;ctr<1000;++ctr){
        outcome=firstRoll();
        if (outcome=='w')++wcnt;
        if (outcome=='l')++lcnt;
        if (outcome=='r')++rcnt;
    }  
    cout<<"For the first craps roll \n";
    cout<<"The probability of winning is "<<float(wcnt)/1000<<endl;
    cout<<"The probability of losing is "<<float(lcnt)/1000<<endl;
    cout<<"The probability of having to roll again is " 
            <<float(rcnt)/1000<<endl;
    return 0;
}
搜索更多相关主题的帖子: data the for and int 
2017-10-26 06:26
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
结合代码才看懂这段洋文的意思:
有种比赛叫“四分之一英里加速赛”,参与此比赛的车叫“加速赛车” ------ 无用信息,不需要管
文件data.txt中记录了一辆车在7秒内的加速度,记录间隔是1毫秒(0.001秒),加速度单位是 米/秒^2,数值是浮点类型,之间以空格分隔 ------ 所以数据量最多是7000条
文件data.txt中有7000条记录,如果赛车在7秒之前就已经到达终点,则随后的加速度记录为-999.99
要求你计算这辆车在越过终点时的速度(千米/小时) ------ 唯一的问题是,假如一辆在7秒内尚未到达终点怎么办?看参考代码,似乎不考虑这种可能。
故有代码
程序代码:
#include <iostream>
#include <fstream>
using namespace std;

int main( void )
{
    double vel = 0;

    ifstream data( "data.txt" );
    for( double acc; data>>acc && acc!=-999.99; )
        vel += acc*0.001;

    cout << vel*3.6 << endl;
}

2017-10-26 08:53
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
第二题,长话短说(主要是我自己还迷糊着呢):
定义一个函数 char firstRoll(),它模拟掷骰子两次,点数和为7或11为赢,返回'w';点数和为2,3或12为输,返回'l';其它情况需要重掷,返回'r'。
调用 char firstRoll() 一千次,统计出 输、输、重掷 的几率,输出格式是
For the first craps roll  
Probability of winning is 0.xx
Probability of losing is 0.yy
Probability of having to roll again is 0.zz.

故有代码
程序代码:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

char firstRoll()
{
    unsigned a = unsigned( rand()/(RAND_MAX+1.0) * 6 ) + 1;
    unsigned b = unsigned( rand()/(RAND_MAX+1.0) * 6 ) + 1;
    switch( a+b )
    {
    case 7:
    case 11:
        return 'w';
    case 2:
    case 3:
    case 12:
        return 'l';
    }
    return 'r';
}

int main( void )
{
    size_t wcnt=0, lcnt=0, rcnt=0;
    for( size_t i=0; i!=1000; ++i )
    {
        switch( firstRoll() )
        {
        case 'w': ++wcnt; break;
        case 'l': ++lcnt; break;
        case 'r': ++rcnt; break;
        }
    }

    cout << fixed << setprecision(2)
         << "For the first craps roll\n"
         << "The probability of winning is " << wcnt/1000. << '\n'
         << "The probability of losing is " << lcnt/1000. << '\n'
         << "The probability of having to roll again is " << rcnt/1000. << endl;

    return 0;
}


写段代码验证一下:
程序代码:
#include <iostream>
using namespace std;

int main( void )
{
    size_t wcnt=0, lcnt=0, rcnt=0;

    for( unsigned a=1; a<=6; ++a )
    for( unsigned b=1; b<=6; ++b )
    {
        switch( a+b )
        {
        case 7:
        case 11:
            ++wcnt; break;
        case 2:
        case 3:
        case 12:
            ++lcnt; break;
        default:
            ++rcnt;
        }
    }

    cout << wcnt/36.0 << '\n';
    cout << lcnt/36.0 << '\n';
    cout << rcnt/36.0 << endl;
}
输出
0.222222
0.111111
0.666667
2017-10-26 09:32
vvvuvv
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-10-26
收藏
得分:0 
回复 3楼 rjsp
感谢!果然我是个题目都没看懂的人
2017-10-26 10:15
vvvuvv
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-10-26
收藏
得分:0 
回复 3楼 rjsp
能不能问一下  unsigned a = unsigned( rand()/(RAND_MAX+1.0) * 6 ) + 1  是依据上面写出来的啊
2017-10-26 10:17
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
以下是引用vvvuvv在2017-10-26 10:17:13的发言:

   是依据上面写出来的啊
什么意思?
骰子一共六个面,每面的几率是1/6,也就是等概率产生{1,2,3,4,5,6}中的一个数
所以代码是 unsigned( rand()/(RAND_MAX+1.0) * 6 ) + 1
2017-10-26 11:50
快速回复:初学c++ 有两道题不会但是有答案,请问能不能帮忙解释一下怎么做的!感 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015936 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved