摘要:
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; }}
閱讀全文
摘要:
看到這個代碼,我傻了。我也想過用這種方法來解決這個問題。但是,我沒有實現,為什么?因為平時很少寫有關位運算的代碼。其實這個不是關鍵,因為我對位的運算不是很熟悉,總感覺是半知半解。當然用的時候也就用不起了。教訓呀。題目: 某電視臺舉辦了低碳生活大獎賽。題目的計分規則相當奇怪:每位選手需要回答10個問題(其編號為1到10). 答對的,當前分數翻倍;答錯了則扣掉與題號相同的分數(選手必須回答問題,不回答按錯誤處理)。每位選手都有一個起步的分數為10分。 某獲勝選手最終得分剛好是100分,如果不讓你看比賽過程,你能推斷出他(她)哪個題目答對了,哪個題目答錯了嗎? 如果把答對的記為1,答錯的記為0...
閱讀全文
摘要:
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
閱讀全文
摘要:
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
閱讀全文
摘要:
兩個以上的函數,具相同的函數名,但是形參的個數或者形參的類型不同,編譯器根據實參的類型及實參的個數最佳匹配,自動確定調用哪一個函數,這就是函數的重載。1>. int add(int a, int b) {}2>. int add(int a, int b, int c) {}3>. float add(float a, float b) {}三個add函數,具體用哪個一個重載要做的事情了。
閱讀全文
摘要:
內聯函數不是在調用時發生控制轉移,而是在編譯時將函數體嵌入在每一個調用處。這樣就節省了參數傳遞、控制轉移等開銷。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);}
閱讀全文
摘要:
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”類似的。第二個一
閱讀全文