书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

2012年4月19日

rand&&srand

摘要: rand()功能:伪随机数发生器库: stdlib.h用法:需要先调用srand初始化,一般用当前日期初始化随机数种子, 这样每次执行代码都可以产生不同的随机数。View Code #include "iostream"#include "cstdio"#include "ctime"using namespace std;int main(){ srand((unsigned)time(NULL)); for(int i=0; i<20; i++) { cout<<rand()%10<<endl; }} 阅读全文

posted @ 2012-04-19 23:16 More study needed. 阅读(154) 评论(0) 推荐(0) 编辑

Bit运算

摘要: 看到这个代码,我傻了。我也想过用这种方法来解决这个问题。但是,我没有实现,为什么?因为平时很少写有关位运算的代码。其实这个不是关键,因为我对位的运算不是很熟悉,总感觉是半知半解。当然用的时候也就用不起了。教训呀。题目: 某电视台举办了低碳生活大奖赛。题目的计分规则相当奇怪:每位选手需要回答10个问题(其编号为1到10). 答对的,当前分数翻倍;答错了则扣掉与题号相同的分数(选手必须回答问题,不回答按错误处理)。每位选手都有一个起步的分数为10分。 某获胜选手最终得分刚好是100分,如果不让你看比赛过程,你能推断出他(她)哪个题目答对了,哪个题目答错了吗? 如果把答对的记为1,答错的记为0... 阅读全文

posted @ 2012-04-19 22:38 More study needed. 阅读(223) 评论(0) 推荐(0) 编辑

C++ 构造函数各种怪式一网打尽

摘要: 1.正常无参(无初始值)View Code #include "iostream"#include "string"using namespace std;class Point{public: Point() { cout<<"Default constructor called."<<endl; cout<<x<<" "<<y<<endl; }private: int x, y;};int main(){ Point *ptr1=new P 阅读全文

posted @ 2012-04-19 20:29 More study needed. 阅读(231) 评论(0) 推荐(0) 编辑

C++ new动态创建对象

摘要: View Code代码就是这样的简单。无参时的创建。View Code #include "iostream"#include "string"using namespace std;class Point{public: Point(int x, int y) { cout<<"Default constructor called."<<endl; cout<<x<<" "<<y<<endl; }private: int x, y;};int 阅读全文

posted @ 2012-04-19 20:23 More study needed. 阅读(386) 评论(0) 推荐(0) 编辑

C++ 函数重载

摘要: 两个以上的函数,具相同的函数名,但是形参的个数或者形参的类型不同,编译器根据实参的类型及实参的个数最佳匹配,自动确定调用哪一个函数,这就是函数的重载。1>. int add(int a, int b) {}2>. int add(int a, int b, int c) {}3>. float add(float a, float b) {}三个add函数,具体用哪个一个重载要做的事情了。 阅读全文

posted @ 2012-04-19 19:46 More study needed. 阅读(137) 评论(0) 推荐(0) 编辑

C++ inline的使用

摘要: 内联函数不是在调用时发生控制转移,而是在编译时将函数体嵌入在每一个调用处。这样就节省了参数传递、控制转移等开销。View Code #include "iostream"#include "string"using namespace std;inline void print(string Str){ cout<<Str<<endl;}int main(){ string Str; while(cin>>Str) print(Str);} 阅读全文

posted @ 2012-04-19 19:37 More study needed. 阅读(163) 评论(0) 推荐(0) 编辑

打表打表何谓打表?

摘要: View Code #include<iostream>using namespace std;int main(){ freopen("God.txt","w", stdout); for(int i=0; i<10000; i++) { cout<<i<<","; if(i%30==0 && i!=0) cout<<endl<<endl; }}上面的freopen就是用来打表的了。无语呀,到现在才明白。第一个一定是:“文件名.txt”类似的。第二个一 阅读全文

posted @ 2012-04-19 18:44 More study needed. 阅读(382) 评论(0) 推荐(0) 编辑

导航

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!