17.4节练习
2016-07-07 20:26
288 查看
练习17.28 编写函数,每次调用生成并返回一个均匀分布的随机unsigned int。
练习17.29 修改上一题中编写的函数,允许用户提供一个种子作为可选参数。
练习17.30 再次修改你的程序,此次再增加两个参数,表示函数允许返回的最小值和最大值。
练习17.31 对于本节中的游戏编程,如果在do循环体内定义be和e,会发生什么?
每步循环都会创建一个新引擎,从而每步循环都会生成相同的值。
练习17.32 如果我们在循环内定义resp,会发生什么?
作为循环的条件,不能定义在循环体呢,否则编译不通过。
练习17.33 修改11.3.6节(392页)中的单词转换程序,允许对一个给定单词有多种转换方式,每次随机选择一个进行实际转换。
#include <iostream> #include <random> using namespace std; unsigned rand_static() { static default_random_engine e; static uniform_int_distribution<unsigned> u; return u(e); } unsigned rand_() { default_random_engine e; uniform_int_distribution<unsigned> u; return u(e); } int main() { for (int i = 0; i < 10; ++i) { cout << rand_static() << endl; cout << rand_() << endl; cout << endl; } }
练习17.29 修改上一题中编写的函数,允许用户提供一个种子作为可选参数。
#include <iostream> #include <random> using namespace std; unsigned rand_static(unsigned num) { static default_random_engine e(num); //static uniform_int_distribution<unsigned> u; return e(); } unsigned rand_(unsigned num) { default_random_engine e(num); //uniform_int_distribution<unsigned> u; return e(); } int main() { unsigned num; cin >> num; for (int i = 0; i < 10; ++i) { cout << rand_static(num) << endl; cout << rand_(num) << endl; cout << endl; } }
练习17.30 再次修改你的程序,此次再增加两个参数,表示函数允许返回的最小值和最大值。
#include <iostream> #include <random> using namespace std; unsigned rand_static(unsigned num) { static default_random_engine e(num); //static uniform_int_distribution<unsigned> u; cout << "max min:" << e.max() << " " << e.min() << endl; return e(); } unsigned rand_(unsigned num) { default_random_engine e(num); //uniform_int_distribution<unsigned> u; cout << "max min:" << e.max() << " " << e.min() << endl; return e(); } int main() { unsigned num; cin >> num; for (int i = 0; i < 10; ++i) { cout << rand_static(num) << endl; cout << rand_(num) << endl; cout << endl; } }
练习17.31 对于本节中的游戏编程,如果在do循环体内定义be和e,会发生什么?
每步循环都会创建一个新引擎,从而每步循环都会生成相同的值。
练习17.32 如果我们在循环内定义resp,会发生什么?
作为循环的条件,不能定义在循环体呢,否则编译不通过。
练习17.33 修改11.3.6节(392页)中的单词转换程序,允许对一个给定单词有多种转换方式,每次随机选择一个进行实际转换。
#include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <random> std::map<std::string, std::string> buildmap(std::ifstream &ifile) { std::map<std::string, std::string> temp; std::string str, f, s; while (getline(ifile, str)) { std::istringstream sst(str); sst >> f >> s; temp[f] = s; } return temp; } std::s 4000 tring trans(const std::string &s, std::map<std::string, std::string> &m)//转换 { auto temp = m.find(s); if (temp != m.end()) return m[s]; else return s; } std::string trans_random(const std::string &s, std::map<std::string, std::string> &m)//转换 { auto temp = m.find(s); if (temp != m.end()) return (m[s]+"!"); else return s; } int main(int argc, char **argv) { using namespace std; ifstream ifpw(argv[1]), iftxt(argv[2]); if (!ifpw&&!iftxt) { cerr << "open file error!\n"; exit(1); } map<string, string> pw(buildmap(ifpw)); string temp, f; default_random_engine e; bernoulli_distribution b; bool t; while (getline(iftxt, temp)) { istringstream iss(temp); while (iss >> f) { t = b(e); if(t) cout << trans_random(f, pw) << " "; else cout << trans(f, pw) << " "; } cout << endl; } system("pause"); return 0; }
相关文章推荐
- java基础学习总结——面向对象2
- 17.3节练习
- 01 LabVIEW的类中各个Scope的范围
- 【水】基于ege的2048
- poj 1664:放苹果
- 修复efi引导的win7
- 卷二 MFC中简单控件使用
- Swift POP+MVVM
- 【Netty4.X】TCP粘包/拆包问题的解决办法(二)
- 修改tableview索引条背景颜色变透明或其他颜色的方法
- 6P的宽度375VS414
- 16.7.7 T1:YL杯超级篮球赛
- 用Python递归实现汉诺塔问题
- 大话设计模式c++实现--原型模式
- Jquery事件委托之Safari
- 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
- 信息检索导论——四、索引构建
- 公交车人数统计系统设计
- Qt之QCheckBox
- 内容提供者——fragment