标签:cout 进度条 srand 学习 outfile 第二周 time data seed
所花时间(包括上课)
17小时
代码量
500行
博客量
写了3篇,阅读15篇+
了解到的知识点
文件读写
基础知识,直接看代码吧
示例程序:
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
// 以写模式打开文件
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// 向文件写入用户输入的数据
outfile << data << endl;
cout << "Enter your age: ";
cin >> data;
cin.ignore();
// 再次向文件写入用户输入的数据
outfile << data << endl;
// 关闭打开的文件
outfile.close();
// 以读模式打开文件
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
// 在屏幕上写入数据
cout << data << endl;
// 再次从文件读取数据,并显示它
infile >> data;
cout << data << endl;
// 关闭打开的文件
infile.close();
return 0;
}
随机数生成(真)
C++ 库有一个名为 rand() 的函数,每次调用该函数都将返回一个非负整数。
但是,该函数返回的数字其实是伪随机数。
该算法需要一个起始值,称为种子,以生成数字。如果没有给出一个种子,那么它将在每次运行时产生相同的数字流。
提供种子通过调用 srand(unsigned int seed)
函数完成
time 函数返回从 1970 年 1 月 1 日午夜开始到现在逝去的秒数
综上,随机数获取方法
unsigned seed; // Random generator seed
// Use the time function to get a "seed” value for srand
seed = time(0);
srand(seed);
// Now generate and print three random numbers
cout << rand() << " " ;
cout << rand() << " " ;
cout << rand() << endl;
动态规划
如何理解动态规划,这里贴一段quora的高赞回答,具体实现可以参考网上的其他博客
How should I explain dynamic programming to a 4-year-old?
*writes down "1+1+1+1+1+1+1+1 =" on a sheet of paper*
"What's that equal to?"
*counting* "Eight!"
*writes down another "1+" on the left*
"What about that?"
*quickly* "Nine!"
"How'd you know it was nine so fast?"
"You just added one more"
"So you didn't need to recount because you remembered there were eight!*Dynamic Programming* is just a fancy way to say 'remembering stuff to save time later'"
MarkDown排版
这篇博客就是用MarkDown写的,知识点都在博客里了
标签:cout,进度条,srand,学习,outfile,第二周,time,data,seed 来源: https://www.cnblogs.com/deepend/p/12384311.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。