diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0a2978 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +*.o +*/build/ +**/build +*.json +*/.vscode +*/.vscode/ +*/pic/ +*.jpg +*.png +*/.vs +*/.vs/ +.idea/ \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week10(final)/\350\256\241\347\256\227\346\225\260\347\273\204\347\232\204\344\275\2163\344\275\215\344\271\213\345\222\214.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week10(final)/\350\256\241\347\256\227\346\225\260\347\273\204\347\232\204\344\275\2163\344\275\215\344\271\213\345\222\214.cpp" index ccbd826..54516b5 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week10(final)/\350\256\241\347\256\227\346\225\260\347\273\204\347\232\204\344\275\2163\344\275\215\344\271\213\345\222\214.cpp" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week10(final)/\350\256\241\347\256\227\346\225\260\347\273\204\347\232\204\344\275\2163\344\275\215\344\271\213\345\222\214.cpp" @@ -1,29 +1,49 @@ +#include +#include +#include #include -#include -#include using namespace std; - // 在此处补充你的代码 -struct CMy_add +int CompareString(const void * e1, const void * e2) { - int ∑ - CMy_add(int &s) :sum(s) {} - void operator()(int val) - { - sum += (val & 7); - } -}; - -int main(int argc, char* argv[]) + MyString * s1 = (MyString *)e1; + MyString * s2 = (MyString *)e2; + if (*s1 < *s2) + return -1; + else if (*s1 == *s2) + return 0; + else if (*s1 > *s2) + return 1; +} +int main() { - int v, my_sum = 0; - vector vec; - cin >> v; - while (v) { - vec.push_back(v); - cin >> v; - } - for_each(vec.begin(), vec.end(), CMy_add(my_sum)); - cout << my_sum << endl; + MyString s1("abcd-"), s2, s3("efgh-"), s4(s1); + MyString SArray[4] = { "big","me","about","take" }; + cout << "1. " << s1 << s2 << s3 << s4 << endl; + s4 = s3; + s3 = s1 + s3; + cout << "2. " << s1 << endl; + cout << "3. " << s2 << endl; + cout << "4. " << s3 << endl; + cout << "5. " << s4 << endl; + cout << "6. " << s1[2] << endl; + s2 = s1; + s1 = "ijkl-"; + s1[2] = 'A'; + cout << "7. " << s2 << endl; + cout << "8. " << s1 << endl; + s1 += "mnop"; + cout << "9. " << s1 << endl; + s4 = "qrst-" + s2; + cout << "10. " << s4 << endl; + s1 = s2 + s4 + " uvw " + "xyz"; + cout << "11. " << s1 << endl; + qsort(SArray, 4, sizeof(MyString), CompareString); + for (int i = 0; i < 4; i++) + cout << SArray[i] << endl; + //s1的从下标0开始长度为4的子串 + cout << s1(0, 4) << endl; + //s1的从下标5开始长度为10的子串 + cout << s1(5, 10) << endl; return 0; } \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week11/README.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week11/README.md" index 2eba087..ef89acf 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week11/README.md" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week11/README.md" @@ -32,7 +32,7 @@ int main() ### `auto`鍏抽敭瀛 -搴旂敤涓锛氱敤浜庡畾涔夊彉閲忥紝缂栬瘧鍣ㄥ彲浠ヨ嚜鍔ㄥ垽鏂彉閲忕殑绫诲瀷 +搴旂敤涓锛氱敤浜庡畾涔夊彉閲忥紝缂栬瘧鍣ㄥ彲浠ヨ嚜鍔ㄥ垽鏂彉閲忕殑绫诲瀷锛屼絾闇瑕佸鍙橀噺杩涜鍒濆鍖 ```c++ map > mp; @@ -54,7 +54,19 @@ auto add(T1 x, T2 y)->decltype(x + y) ### `decltype`鍏抽敭瀛 -姹傝〃杈惧紡绫诲瀷 +绫诲瀷鍒ゅ埆锛屾眰琛ㄨ揪寮忕被鍨 + +```cpp +int i; +decltype(i) x1; // x1 is int +struct A {double x;}; +const A* a = new A(); + +decltype(a->x) x3; // x3 is double +decltype((a->x)) x4; // x4 is double& !!!娉ㄦ剰甯︿簡鎷彿涔嬪悗鏄紩鐢 +``` + + ### 鏅鸿兘鎸囬拡`shared_ptr` @@ -66,9 +78,9 @@ auto add(T1 x, T2 y)->decltype(x + y) shared_ptr ptr(new T); ``` -姝ゅ悗`ptr`灏卞彲浠ュ儚 `T*` 绫诲瀷鐨勬寚閽堜竴鏍锋潵浣跨敤锛屽嵆 `*ptr` 灏辨槸鐢╜new`鍔ㄦ佸垎閰嶇殑 閭d釜瀵硅薄锛岃屼笖涓嶅繀鎿嶅績閲婃斁鍐呭瓨鐨勪簨. +姝ゅ悗`ptr`灏卞彲浠ュ儚 `T*` 绫诲瀷鐨勬寚閽堜竴鏍锋潵浣跨敤锛屽嵆 `*ptr` 灏辨槸鐢╜new`鍔ㄦ佸垎閰嶇殑 閭d釜瀵硅薄锛岃屼笖涓嶅繀鎿嶅績閲婃斁鍐呭瓨鐨勪簨锛屽嵆绋嬪簭缁撴潫鎴栧璞℃秷浜℃椂锛屼細鑷姩澶勭悊鍐呭瓨銆 -- 澶氫釜`shared_ptr`瀵硅薄鍙互鍚屾椂鎵樼涓涓寚閽堬紝绯荤粺浼氱淮鎶や竴涓墭绠¤鏁般傚綋 鏃燻shared_ptr`鎵樼璇ユ寚閽堟椂锛宒elete璇ユ寚閽堛 +- 澶氫釜`shared_ptr`瀵硅薄鍙互鍚屾椂鎵樼涓涓寚閽堬紙new鎿嶄綔寰楀埌鐨勬寚閽堬紝鍚﹀垯delete浼氬嚭閿欙級锛岀郴缁熶細缁存姢涓涓墭绠¤鏁般傚綋 鏃燻shared_ptr`鎵樼璇ユ寚閽堟椂锛宒elete璇ユ寚閽堛 - `shared_ptr`瀵硅薄涓嶈兘鎵樼鎸囧悜鍔ㄦ佸垎閰嶇殑鏁扮粍鐨勬寚閽堬紝鍚﹀垯绋嬪簭杩愯浼氬嚭閿欙紒锛 浣跨敤渚嬪瓙 @@ -93,12 +105,13 @@ int main() shared_ptr sp2(sp1); //sp2涔熸墭绠(2) cout << "1)" << sp1->n << "," << sp2->n << endl; //杈撳嚭1)2,2 shared_ptr sp3; + // 瀵逛竴涓猻hared_ptr绠$悊鐨勬寚閽堬紝鍙互鐢╣et()鑾峰緱鍏舵寚鍚戠殑瀵硅薄 A *p = sp1.get(); //p 鎸囧悜A(2) cout << "2)" << p->n << endl; sp3 = sp1; //sp3涔熸墭绠(2) cout << "3)" << (*sp3).n << endl; //杈撳嚭 2 sp1.reset(); //sp1鏀惧純鎵樼A(2) - if (!sp1) + if (!sp1)//鍒ゆ柇鏄惁鏈夋墭绠$殑鎸囬拡 { cout << "4)sp1 is null" << endl; //浼氳緭鍑 } @@ -110,7 +123,8 @@ int main() //sp5.reset(q); 涓嶅Ε锛屼細瀵艰嚧绋嬪簭鍑洪敊 sp1.reset(); //sp1鏀惧純鎵樼A(3) cout << "before end main" << endl; - sp4.reset(); //sp4鏀惧純鎵樼A(3) + sp4.reset(); //sp4鏀惧純鎵樼A(3)锛屾鏃禔(3)浼氳鑷姩delete鎺 + // 杈撳嚭 "destructor" cout << "end main" << endl; return 0; //绋嬪簭缁撴潫锛屼細delete 鎺堿(2) } @@ -118,3 +132,376 @@ int main() `sp5.reset(q)`锛氳繖涓鍙ヤ互鏅氭寚閽堝仛鍙傛暟锛岃繍琛屾椂涓嶄細鍘诲垽鏂璥q`鏄惁琚叾浠朻shared_ptr`鎵鎵樼锛屾墍浠ュ綋绋嬪簭杩愯缁撴潫锛宍q`鏈夊彲鑳借澶氭`delete`銆 +### 绌烘寚閽坣ullptr + +```cpp +#include +#include +using namespace std; +int main() +{ + int *p1 = NULL; + int *p2 = nullptr; + shared_ptr p3= nullptr; + if(p1==p2);//true + if(p3==nullptr);//true + if(p3==p2);//error, 涓よ呯被鍨嬩笉鍚 + if(p3==NULL);//true + bool b = nullptr; //b=false + int i = nullptr;//error. nullptr涓嶈兘鑷姩杞负鏁村瀷 +} +``` + + + +### 鍩轰簬鑼冨洿鐨刦or寰幆 + +### 鍙冲煎紩鐢ㄥ拰move璇箟 + +涓鑸潵璇达紝涓嶈兘鍙栧湴鍧鐨勮〃杈惧紡锛屽氨鏄彸鍊硷紝鑳藉彇鍦板潃鐨勶紝灏辨槸宸﹀笺 + +```cpp +class A{}; +A &r= A(); //error. A()鏄复鏃跺彉閲忥紝鏃犲悕鍙橀噺锛屾槸鍙冲硷紱 +A &&r= A(); //ok. r鏄彸鍊煎紩鐢ㄣ +``` + +涓昏鐩殑鏄彁楂樼▼搴忚繍琛岀殑鏁堢巼锛屽噺灏戦渶瑕佽繘琛屾繁鎷疯礉鐨勫璞¤繘琛屾繁鎷疯礉鐨勬鏁般 + +```cpp +class String +{ +public: + char* str;//鎸囧悜鍔ㄦ佸垎閰嶇殑瀛樺偍绌洪棿 + String(): str(new char[1]){str[0]=0;} //鎸囧悜涓涓┖涓 + String(const char* s){ + str = new char[strlen(s)+1]; + strcpy(str,s); + } + String(const String &S) { + cout<<"copy constructor called"< +void MoveSwap(T &a, T &b) { + // move()浣滅敤锛氭妸涓涓乏鍊煎彉鎴愬彸鍊 + T tmp(move(a));// std::move(a)涓哄彸鍊硷紝杩欓噷浼氳皟鐢╩ove constructor + a=move(b); //move(b)涓哄彸鍊硷紝鍥犳浼氳皟鐢╩ove assignment + b=move(tmp); //move(tmp)涓哄彸鍊硷紝鍥犳浼氳皟鐢╩ove assignment +} + +``` + +浣跨敤涓婅堪绠楁硶鐨勫墠鎻愭槸锛屾垜浠笉鍦ㄤ箮鍦ㄨ繍琛岃繃绋嬩腑a,b鐨勫肩殑鏀瑰彉锛屽洜涓哄湪璋冪敤MoveSwap鏃讹紝浼氬瓨鍦ㄦ煇涓鏃跺埢锛宎鐨勫唴瀹逛负绌轰覆銆 + +### 鏃犲簭瀹瑰櫒锛堝搱甯岃〃锛 + +#include + +鍦ㄦ彃鍏ュ拰鏌ユ壘鏃讹紝鏃堕棿澶嶆潅搴﹁繎涔庝负甯告暟锛屼絾闇瑕佹洿澶氱殑鍐呭瓨绌洪棿 + +### 姝e垯琛ㄨ揪寮 + +#include + +姝e垯琛ㄨ揪寮忚鏄庝簡涓绉嶆ā寮 + +```cpp +int main() +{ + regex reg("b.?p.*k"); + //浠ュ瓧绗寮澶达紝绱ф帴涓涓垨闆朵釜瀛楃锛屽叾娆′负瀛楃p锛岃浇鎺ョ潃0涓垨澶氫釜鐩稿悓鐨勫瓧绗︼紝鏈鍚庝互k缁撳熬 + cout< 杩斿洖鍊肩被鍨 +{ + 璇彞 +} +``` + +- [=]浠ヤ紶鍊煎舰寮忎娇鐢ㄥ閮ㄦ墍鏈夌殑鍙橀噺锛屽鏋滄槸涓涓璞★紝鍒欎細寮曞彂澶嶅埗鏋勯犲嚱鏁 +- []涓嶄娇鐢ㄤ换浣曞閮ㄥ彉閲 +- [&]浠ュ紩鐢ㄦ柟寮忎娇鐢ㄦ墍鏈夊閮ㄥ彉閲 +- [x, &y] x浠ヤ紶鍊煎舰寮忎娇鐢, y浠ュ紩鐢ㄦ柟寮忎娇鐢 +- [=, &x, &y] x,y浠ュ紩鐢ㄥ舰寮忎娇鐢紝鍏朵笌鍙橀噺浠ヤ紶鍊煎舰寮忎娇鐢 +- [&, x, y] x,y浠ヤ紶鍊煎舰寮忎娇鐢紝鍏朵綑鍙橀噺浠ュ紩鐢ㄥ舰寮忎娇鐢 + +"->杩斿洖鍊肩被鍨"涔熷彲浠ユ病鏈夛紝缂栬瘧鍣ㄨ嚜鍔ㄨ繘琛屽垽鏂 + +```cpp +int main() +{ + int x = 100, y=200, z=300; + cout<<[](double a, double b){return a+b;}(1.2,2.5); + auto ff=[=, &x, &y](int n) { + cout<bool{return x%10fib = [&fib](int n) +{ return n<=2?1:fib(n-1)+fib(n-2);}; +``` + +function琛ㄧず杩斿洖鍊间负int锛屾湁涓涓猧nt鍙傛暟鐨勫嚱鏁般傛鏃朵笉鑳藉啓涓篴uto锛屽洜涓虹紪璇戝櫒鏃犳硶閫氳繃return鐩存帴寰楀埌涓涓‘瀹氱殑绫诲瀷銆 + +## 寮哄埗绫诲瀷杞崲 + +### static_cast + +鐢ㄦ潵杩涜姣旇緝鈥滆嚜鐒垛濆拰浣庨闄╃殑杞崲锛屽鏁存暟鍨嬪拰瀹炴暟鍨嬨佸瓧绗﹀瀷涔嬮棿鐨勭浉浜掕浆鎹€ + +涓嶈兘鍦ㄤ笉鍚岀被鍨嬬殑鎸囬拡涔嬮棿鐩镐簰杞崲锛屼篃涓嶈兘鐢ㄤ簬鏁村瀷鍜屾寚閽堜箣闂寸殑鐩镐簰杞崲锛屼篃涓嶈兘鐢ㄤ簬涓嶅悓绫诲瀷鐨勫紩鐢ㄤ箣闂寸殑杞崲銆 + +鏃х殑绫讳技浜(int)鐨勫己鍒剁被鍨嬭浆鎹紝涓嶈兘璇嗗埆椋庨櫓澶у皬銆傝屼笖debug鏃讹紝濡傛灉涓嶇敤"cast"鍏抽敭瀛楋紝妫绱㈡椂寰堥夯鐑︺ + +```cpp +class A +{ +public: + operator int() {return 1;} //绫诲瀷杞崲鐨勯噸杞斤紝涓嶇敤鍐欒繑鍥炲 + operator char *() {return NULL;} +}; +int main() +{ + A a; + int n; char *p = "New Dragon Inn"; + n = static_cast(3.14); //n鐨勫煎彉涓3 + n = static_cast (a); //璋冪敤a.operator int锛 n鐨勫煎彉涓1 + p = static_cast(a); //璋冪敤a.operator char *. p鐨勫煎彉涓篘ULL + n = static_cast (p); //缂栬瘧閿欒锛宻tatic_cast涓嶈兘灏嗘寚閽堣浆鎹负鏁村瀷 + p = static_cast (n);//缂栬瘧閿欒锛宻tatic_cast涓嶈兘灏嗘暣鍨嬭浆鎹负鎸囬拡 + return 0; +} +``` + + + +### reinterpret_cast + +涓嶅悓绫诲瀷鐨勬寚閽堜箣闂寸殑杞崲銆佷笉鍚岀被鍨嬬殑寮曠敤涔嬮棿杞崲銆佷互鍙婃寚閽堝拰鑳藉绾冲緱涓嬫寚閽堢殑鏁存暟绫诲瀷涔嬮棿鐨勮浆鎹€傝浆鎹㈢殑鏃跺欙紝 鎵ц鐨勬槸閫愪釜姣旂壒鎷疯礉鐨勬搷浣溿 + +```cpp +#include +using namespace std; +class A +{ +public: + int i; + int j; + A(int n):i(n),j(n) { } +}; +int main() +{ + A a(100); + int & r = reinterpret_cast(a); //寮鸿璁 r 寮曠敤 a + r = 200; //鎶 a.i 鍙樻垚浜 200,r鐨勫洓涓瓧鑺傚啓鍏200銆傝孉鐨勫唴瀛樺垎甯冧负i锛4涓瓧鑺傦級锛宩(4涓瓧鑺) + cout << a.i << "," << a.j << endl; // 杈撳嚭 200,100 + int n = 300; + A * pa = reinterpret_cast ( & n); //寮鸿璁 pa 鎸囧悜 n + pa->i = 400; // n 鍙樻垚 400 + pa->j = 500; //姝ゆ潯璇彞涓嶅畨鍏紝寰堝彲鑳藉鑷寸▼搴忓穿婧 + cout << n << endl; // 杈撳嚭 400 + long long la = 0x12345678abcdLL; + pa = reinterpret_cast(la); // la澶暱锛屽彧鍙栦綆32浣0x5678abcd鎷疯礉缁檖a + unsigned int u = reinterpret_cast(pa); //pa閫愪釜姣旂壒鎷疯礉鍒皍 + cout << hex << u << endl; //杈撳嚭 5678abcd + typedef void (* PF1) (int); + typedef int (* PF2) (int,char *); + PF1 pf1; PF2 pf2; + pf2 = reinterpret_cast(pf1); //涓や釜涓嶅悓绫诲瀷鐨勫嚱鏁版寚閽堜箣闂村彲浠ヤ簰鐩歌浆鎹 +} +``` + +### const_cast + +鐢ㄦ潵杩涜鍘婚櫎const灞炴х殑杞崲銆傚皢const寮曠敤杞崲鎴愬悓绫诲瀷鐨勯潪const寮曠敤锛屽皢const鎸囬拡杞崲涓哄悓绫诲瀷鐨勯潪const鎸囬拡鏃剁敤瀹冦 + +### dynamic_cast + +- dynamic_cast涓撻棬鐢ㄤ簬灏嗗鎬佸熀绫荤殑鎸囬拡鎴栧紩鐢紝寮哄埗杞崲涓烘淳鐢熺被鐨勬寚閽堟垨寮曠敤锛岃屼笖鑳藉妫鏌ヨ浆鎹㈢殑 瀹夊叏鎬с傚浜庝笉瀹夊叏鐨勬寚閽堣浆鎹紝杞崲缁撴灉杩斿洖NULL 鎸囬拡銆 + +- dynamic_cast涓嶈兘鐢ㄤ簬灏嗛潪澶氭佸熀绫荤殑鎸囬拡鎴栧紩鐢紝 寮哄埗杞崲涓烘淳鐢熺被鐨勬寚閽堟垨寮曠敤 + +```cpp +#include +#include +using namespace std; +class Base +{ //鏈夎櫄鍑芥暟锛屽洜姝ゆ槸澶氭佸熀绫 +public: + virtual ~Base() { } +}; +class Derived:public Base { }; +int main() +{ + Base b; + Derived d; + Derived * pd; + pd = reinterpret_cast ( &b); + if( pd == NULL) //姝ゅpd涓嶄細涓篘ULL銆 + //reinterpret_cast涓嶆鏌ュ畨鍏ㄦэ紝鎬绘槸杩涜杞崲 + cout << "unsafe reinterpret_cast" << endl; + //涓嶄細鎵ц + pd = dynamic_cast ( &b); + if( pd == NULL) + //缁撴灉浼氭槸NULL锛屽洜涓 &b涓嶆槸鎸囧悜娲剧敓绫诲璞★紝姝よ浆鎹笉瀹夊叏 + cout << "unsafe dynamic_cast1" << endl; //浼氭墽琛 + Base * pb = & d; + pd = dynamic_cast ( pb); //瀹夊叏鐨勮浆鎹 + if( pd == NULL) //姝ゅpd 涓嶄細涓篘ULL + cout << "unsafe dynamic_cast2" << endl; //涓嶄細鎵ц + return 0; +} +``` + +## 寮傚父澶勭悊 + +```cpp +#include +using namespace std; +int main() { + double m ,n; + cin >> m >> n; + try { + cout << "before dividing." << endl; + if( n == 0) + throw -1; //鎶涘嚭鏁村瀷寮傚父 + else if( m == 0 ) + throw -1.0; //鎶涘嚭double鍨嬪紓甯 + else + cout << m / n << endl; + cout << "after dividing." << endl; + } + catch(double d) { + cout << "catch(double) " << d << endl; + } + catch(...) { //鎹曡幏浠讳綍寮傚父鐨刢atch鍧 + // 濡傛灉瀛樺湪double寮傚父锛宑atch浼氫粠涓婂埌涓嬩竴娆″尮閰嶏紝杩涘叆绗竴涓尮閰嶆垚鍔熺殑catch鍧 + cout << "catch(...) " << endl; + } + cout << "finished" << endl; + return 0; +} +``` + +### 寮傚父鐨勫啀鎶涘嚭 + +濡傛灉涓涓嚱鏁板湪鎵ц鐨勮繃绋嬩腑锛屾姏鍑虹殑寮傚父鍦ㄦ湰鍑芥暟鍐呭氨琚玞atch鍧楁崟鑾峰苟澶勭悊浜嗭紝閭d箞璇ュ紓甯稿氨涓嶄細鎶涚粰杩欎釜鍑芥暟鐨勮皟鐢ㄨ(涔熺О鈥滀笂涓灞傜殑鍑芥暟鈥)锛涘鏋滃紓甯稿湪鏈嚱鏁颁腑娌¤澶勭悊锛屽氨浼氳鎶涚粰涓婁竴灞傜殑鍑芥暟銆 + +```cpp +#include + +#include + +using namespace std; +class CException { + public: + string msg; + CException(string s): msg(s) {} +}; +double Devide(double x, double y) { + if (y == 0) + throw CException("devided by zero"); + cout << "in Devide" << endl; + return x / y; +} +int CountTax(int salary) { + try { + if (salary < 0) + throw -1; + cout << "counting tax" << endl; + } catch (int) { + cout << "salary < 0" << endl; + } + cout << "tax counted" << endl; + return salary * 0.15; +} +int main() { + double f = 1.2; + try { + CountTax(-1); + f = Devide(3, 0); + cout << "end of try block" << endl; + } catch (CException e) { + cout << e.msg << endl; + } + cout << "f=" << f << endl; + cout << "finished" << endl; + return 0; +} +``` + + + +### C++鏍囧噯寮傚父绫 + +\#include + +1. **bad_cast** 鍦ㄧ敤 dynamic_cast杩涜浠庡鎬佸熀绫诲璞★紙鎴栧紩鐢級,鍒版淳鐢熺被鐨勫紩鐢ㄧ殑寮哄埗绫诲瀷杞崲鏃讹紝濡傛灉杞崲鏄笉瀹夊叏鐨勶紝鍒欎細鎶涘嚭姝ゅ紓甯搞 + +2. **bad_alloc** 鍦ㄧ敤new杩愮畻绗﹁繘琛屽姩鎬佸唴瀛樺垎閰嶆椂锛屽鏋滄病鏈夎冻澶熺殑鍐呭瓨锛屽垯浼氬紩鍙戞寮傚父銆 + +3. **out_of_range**鐢╲ector鎴杝tring 鐨刟t鎴愬憳鍑芥暟鏍规嵁涓嬫爣璁块棶鍏冪礌鏃讹紝 濡傛灉涓嬫爣瓒婄晫锛屽氨浼氭姏鍑烘寮傚父銆 + diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week2/README.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week2/README.md" index 40c9c6c..7598e8a 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week2/README.md" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week2/README.md" @@ -1,4 +1,4 @@ -# Week2 +锘# Week2 ## 鍑芥暟鎸囬拡 @@ -8,9 +8,11 @@ 绫诲瀷鍚 (*鎸囬拡鍙橀噺鍚)(鍙傛暟鍒楄〃) ``` +绫诲瀷鍚嶆槸鎸囧嚱鏁扮殑杩斿洖鍊肩被鍨 + 绀轰緥 -```c++ +```cpp void PrintMin(int a, int b) { if(a < b) @@ -37,7 +39,7 @@ int main() 鍑芥暟鍘熷瀷锛 -```c++ +```cpp void qsort(void *base, int nelem, unsigned int width, int (*pfCompare)(const void *, const void *)); ``` @@ -48,7 +50,7 @@ void qsort(void *base, int nelem, unsigned int width, int (*pfCompare)(const voi 姣旇緝鍑芥暟鍘熷瀷锛 -```c++ +```cpp int 姣旇緝鍑芥暟鍚(const void *elem1, const void *elem2); ``` @@ -58,14 +60,14 @@ int 姣旇緝鍑芥暟鍚(const void *elem1, const void *elem2); 绀轰緥锛 -```c++ +```cpp #include using namespace std; int MyCompare(const void * elem1, const void * elem2) { unsigned int *p1, *p2; - p1 = (unsigned int *)elem1; + p1 = (unsigned int *)elem1;//* elem1闈炴硶锛屽洜涓篹lem1鏄痸oid *绫诲瀷锛岀紪璇戝櫒鏃犳硶纭畾鍏跺崰鐢ㄧ殑瀛楄妭鏁 p2 = (unsigned int *)elem2; return (*p1 % 10) - (*p2 % 10) } @@ -88,14 +90,16 @@ int main() 鍛戒护琛屾柟寮忚繍琛岀▼搴 -```c++ +```cpp int main(int argc, char *argv[]) { ... } ``` +argc: 浠h〃绋嬪簭鍚姩鏃讹紝鍛戒护鍑藉弬鏁扮殑涓暟銆侰/C++瑙勫畾锛屽彲鎵ц绋嬪簭鏈韩鐨勬枃浠跺悕锛屼篃绠椾竴涓懡浠ゅ嚱鍙傛暟锛屽洜姝わ紝argc鐨勫艰嚦灏戞槸涓銆 +argv: 鎸囬拡鏁扮粍锛屽叾涓殑姣忎釜鍏冪礌閮芥槸涓涓猚har* 绫诲瀷鐨勬寚閽堬紝璇ユ寚閽堟寚鍚戜竴涓瓧绗︿覆锛岃繖涓瓧绗︿覆閲屽氨瀛樻斁鐫鍛戒护琛屽弬鏁般 ## 浣嶈繍绠 @@ -133,7 +137,7 @@ int main(int argc, char *argv[]) 渚嬶細缁欏畾`int a, n` 姹傜a鐨勭n浣 -```c++ +```cpp (a >> n) & 1 (a & (1 << n)) >> n // (n != 31) ``` @@ -144,10 +148,11 @@ int main(int argc, char *argv[]) 瀹氫箟 -```c++ +```cpp 绫诲瀷鍚 &寮曠敤鍚 = 鏌愬彉閲忓悕 ``` +- 鏌愪釜鍙橀噺锛岀瓑浠蜂簬杩欎釜鍙橀噺锛岀浉褰撲簬杩欎釜鍙橀噺鐨勫埆鍚 - 涓瀹氳鍒濆鍖栵紝鍒濆鍖栧悗涓嶈兘鏀瑰彉 - 鍙兘寮曠敤鍙橀噺锛屼笉鑳藉紩鐢ㄥ父閲忓拰琛ㄨ揪寮 @@ -155,7 +160,7 @@ int main(int argc, char *argv[]) - 寮曠敤鍋氬弬鏁帮細浜ゆ崲 -```c++ +```cpp void swap(int &a, int &b) { int tmp = a; @@ -166,7 +171,7 @@ void swap(int &a, int &b) - 寮曠敤鍋氬嚱鏁拌繑鍥炲 -```c++ +```cpp int n = 4; int &SetValue() { @@ -182,7 +187,7 @@ int main() - 甯稿紩鐢 -```c++ +```cpp int n = 100; const int &r = n; r = 200; // error @@ -198,7 +203,8 @@ r = 200; // error - 涓嶅彲閫氳繃甯搁噺鎸囬拡淇敼鍏舵寚鍚戠殑鍐呭 - 甯搁噺鎸囬拡鐨勬寚鍚戝彲浠ヤ慨鏀 -- 涓嶈兘鎶婂父閲忔寚閽堣祴鍊肩粰闈炲父閲忔寚閽堬紝浣嗗彲浠ュ己鍒剁被鍨嬭浆鎹 +- 涓嶈兘鎶婂父閲忔寚閽堣祴鍊肩粰闈炲父閲忔寚閽堬紝浣嗗彲浠ュ己鍒剁被鍨嬭浆鎹紝涔熷彲浠ユ妸闈炲父閲忔寚閽堣祴鍊肩粰甯搁噺鎸囬拡 +- 鐩稿浜巇efine锛屾洿鎺ㄨ崘const锛屽洜涓篶onst鏈夌被鍨嬫鏌 @@ -208,11 +214,19 @@ r = 200; // error - 鍒嗛厤 - 鍒嗛厤涓涓彉閲忥細`Pointer = new Type;` + + Type鏄换鎰忕被鍨嬪悕锛宲ointer鏄疶*鐨勬寚閽堛傚姩鎬佸垎閰嶅嚭涓鍧楀ぇ灏忎负sizeof(Type)鐨勫唴瀛樼┖闂达紝骞舵妸璇ュ唴瀛樼┖闂寸殑璧峰鍦板潃璧嬪肩粰Pointer + - 鍒嗛厤涓涓暟缁勶細`Pointer = new Type[Number]` + - `new`鐨勮繑鍥炵被鍨嬮兘鏄浉搴旂被鍨嬬殑**鎸囬拡** + - 閲婃斁锛堜笉鑳介噴鏀惧娆★級 + - `delete Pointer;` - `delete [] Pointer;` + + 娉ㄦ剰Pointer蹇呴』鎸囧悜new鍑烘潵鐨勫姩鎬佺┖闂淬 @@ -225,7 +239,7 @@ r = 200; // error 瀹氫箟锛屽叧閿瓧 `inline` -```c++ +```cpp inline int Max(int a, int b); ``` @@ -237,7 +251,7 @@ inline int Max(int a, int b); 涓涓垨澶氫釜鍑芥暟锛屽悕瀛楃浉鍚岋紝鐒惰屽弬鏁颁釜鏁版垨鍙傛暟绫诲瀷涓嶇浉鍚岋紝杩欏彨鍋氬嚱鏁扮殑閲嶈浇銆 锛堝弬鏁扮浉鍚岋紝杩斿洖鍊间笉鍚屼笉绠楅噸杞斤級 -```c++ +```cpp int Max(double f1, double f2); int Max(int n1, int n2); int Max(int n1, int n2, int n3); @@ -307,7 +321,7 @@ int Max(int n1, int n2, int n3); ### 璁块棶绫荤殑鎴愬憳鍙橀噺鍜屾垚鍛樺嚱鏁 -```c++ +```cpp 瀵硅薄鍚.鎴愬憳鍚 ``` @@ -315,13 +329,13 @@ int Max(int n1, int n2, int n3); 鎸囬拡->鎴愬憳鍚 ``` -```c++ +```cpp 寮曠敤鍚.鎴愬憳鍚 ``` ### 绫荤殑鎴愬憳鍑芥暟鐨勫彟涓绉嶅啓娉 -```c++ +```cpp type class::function(parameter) { ... diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week4/README.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week4/README.md" index b138f4c..37d56ed 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week4/README.md" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week4/README.md" @@ -1,4 +1,4 @@ -# Week4 +锘# Week4 ## 杩愮畻绗﹂噸杞 @@ -10,20 +10,29 @@ 鐩殑锛 -- 鎵╁睍C++涓彁渚涚殑杩愮畻绗︾殑閫傜敤鑼冨洿, 浠ョ敤浜庣被鎵琛ㄧず鐨勬娊璞℃暟鎹被鍨 +- 鎵╁睍cpp涓彁渚涚殑杩愮畻绗︾殑閫傜敤鑼冨洿, 浠ョ敤浜庣被鎵琛ㄧず鐨勬娊璞℃暟鎹被鍨 杩愮畻绗﹂噸杞界殑瀹炶川鏄嚱鏁伴噸杞斤紝鍙傛暟涓暟涓鸿繍绠楃鐩暟 -```c++ +```cpp 杩斿洖鍊肩被鍨 operator 杩愮畻绗(褰㈠弬琛) { 鈥︹ } ``` +鍦ㄧ▼搴忕紪璇戞椂锛 + +- 鎶婂惈杩愮畻绗︾殑琛ㄨ揪寮 -> 瀵硅繍绠楃鍑芥暟鐨勮皟鐢 +- 鎶婅繍绠楃鐨勬搷浣滄暟 -> 杩愮畻绗﹀嚱鏁扮殑鍙傛暟 +- 杩愮畻绗﹀娆¢噸杞芥椂锛屾牴鎹疄鍙傜被鍨嬪喅瀹氳皟鐢ㄥ摢涓繍绠楃鍑芥暟 +- 杩愮畻绗﹀彲浠ヨ閲嶈浇涓烘櫘閫氬嚱鏁帮紝涔熷彲浠ヨ閲嶈浇涓虹被鐨勬垚鍛樺嚱鏁 + 渚嬪澶嶆暟鐩稿姞锛 -```c++ +閲嶈浇涓烘櫘閫氬嚱鏁帮紝鍙傛暟涓暟涓鸿繍绠楃鐩暟 + +```cpp Complex operator+(const Complex &a, const Complex &b) { return Complex(a.real + b.real, a.imag + b.imag) @@ -32,7 +41,7 @@ Complex operator+(const Complex &a, const Complex &b) 杩愮畻绗﹂噸杞戒负鎴愬憳鍑芥暟锛屽弬鏁颁釜鏁颁负杩愮畻绗︾洰鏁板噺涓 -```c++ +```cpp Complex operator+(const Complex &operand2); // x = y + z // 绛変环浜 x = y.operator+(z) @@ -49,15 +58,15 @@ Complex operator+(const Complex &operand2); - 鎶婁竴涓 `int` 绫诲瀷鍙橀噺 璧嬪肩粰涓涓 Complex瀵硅薄 - 鎶婁竴涓 `char*` 绫诲瀷鐨勫瓧绗︿覆璧嬪肩粰涓涓 瀛楃涓插璞 -娉ㄦ剰锛氳祴鍊艰繍绠楃 `=` 鍙兘閲嶈浇涓烘垚鍛樺嚱鏁帮紒 +娉ㄦ剰锛氳祴鍊艰繍绠楃 `=` 鍙兘閲嶈浇涓**鎴愬憳鍑芥暟**锛 渚嬪瓙锛氬瓧绗︿覆璧嬪 -```c++ +```cpp class String { private: - char *str; + char *str;//鎸囧悜鍔ㄦ佸垎閰嶇殑瀛樺偍绌洪棿 public: String() :str(NULL) {} const char *get_str() { return str; } @@ -66,14 +75,14 @@ public: }; char* String::operator=(const char *s) -{ +{//閲嶈浇= -> obj="Hello"鑳藉鎴愮珛銆傝皟鐢ㄦ椂锛屾妸"Hello"鐨勯鍦板潃浼犻掕繘鏉 if (str) { delete[] str; } if (s) { - str = new char[strlen(s) + 1]; + str = new char[strlen(s) + 1];//+1鏄负浜嗗瓨鏀锯/0鈥 strcpy(str, s); } else @@ -101,18 +110,20 @@ String::~String() 娴呭鍒舵槸涓绉嶉愬瓧鑺傜殑澶嶅埗銆傚湪澶嶅埗鍐呭鍖呭惈鎸囬拡鏃朵細浜х敓闂銆 -```c++ +```cpp MyString S1, S2; S1 = "this"; S2 = "that"; S2 = S1; ``` -杩欐牱涓涓畝鍗曠殑绋嬪簭浼氬鑷碨1鍜孲2鐨刞char *` 鍙橀噺鎸囧悜鍚屼竴鍧楀唴瀛橈紝浼氶犳垚鍐呭瓨鍨冨溇锛屼互鍙婇噸澶峘delete`鐨勯殣鎮c +杩欐牱涓涓畝鍗曠殑绋嬪簭浼氬鑷碨1鍜孲2鐨刞char *` 鍙橀噺鎸囧悜鍚屼竴鍧楀唴瀛橈紝浼氶犳垚**鍐呭瓨鍨冨溇**锛屼互鍙婇噸澶峘delete`鐨勯殣鎮c + +[![ZIvBJU.png](https://s2.ax1x.com/2019/07/15/ZIvBJU.png)](https://imgchr.com/i/ZIvBJU) -鍒╃敤璧嬪艰繍绠楃閲嶈浇鍙互瀹炵幇娣卞鍒讹細灏嗕竴涓璞′腑鎸囬拡鍙橀噺鎸囧悜鐨勫唴瀹癸紝澶嶅埗鍒板彟涓涓璞′腑鎸囬拡鎴愬憳瀵硅薄鎸囧悜鐨勫湴鏂广 +鍒╃敤璧嬪艰繍绠楃閲嶈浇鍙互瀹炵幇**娣卞鍒**锛氬皢涓涓璞′腑鎸囬拡鍙橀噺鎸囧悜鐨勫唴瀹癸紝澶嶅埗鍒板彟涓涓璞′腑鎸囬拡鎴愬憳瀵硅薄鎸囧悜鐨勫湴鏂广 -```c++ +```cpp String& operator=(const String &s) { if (str == s.str) // 閽堝鑷韩璧嬪肩殑鎯呭喌 @@ -140,7 +151,7 @@ String& operator=(const String &s) 璧嬪艰繍绠楃閲嶈浇鏃讹紝杩斿洖鍊艰涓哄叾鎵浣滅敤鐨勫璞$殑寮曠敤锛屾槸绗﹀悎璧嬪艰繍绠楃浣跨敤涔犳儻鐨勫仛娉曘 -```c++ +```cpp a = b = c; // return void 浼氬鑷碼璧嬪煎け璐 (a = b) = c; // return string 浼氬鑷寸浜屼釜璧嬪兼棤鎰忎箟 ``` @@ -156,7 +167,7 @@ a = b = c; // return void 浼氬鑷碼璧嬪煎け璐 - 鎴愬憳鍑芥暟涓嶈兘婊¤冻浣跨敤瑕佹眰 - 鏅氬嚱鏁板張涓嶈兘璁块棶绫荤殑绉佹湁鎴愬憳 -```c++ +```cpp class Complex { double real, imag; @@ -171,7 +182,7 @@ public: ## 杩愮畻绗﹂噸杞界ず渚嬶細闀垮害鍙彉鐨勬暣褰㈡暟缁 -```c++ +```cpp #include using namespace std; @@ -303,7 +314,7 @@ int main() { //瑕佺紪鍐欏彲鍙橀暱鏁村瀷鏁扮粍绫伙紝浣夸箣鑳藉涓嬩娇鐢細 `cout` 鏄湪 `iostream` 瀹氫箟鐨 `ostream` 绫荤殑瀵硅薄銆 -```c++ +```cpp // 杩斿洖寮曠敤瀹炵幇杩炵画杈撳嚭 ostream & ostream::operator<<(int n) { @@ -312,19 +323,20 @@ ostream & ostream::operator<<(int n) } ``` -瀹氬埗鑷繁鐨 `cout`锛 鍙兘閲嶈浇鎴愬叏灞鍑芥暟 +鐢变簬鐩稿簲鐨勫嚱鏁板凡缁忓湪澶存枃浠堕噷瀹氫箟濂戒簡锛屽洜鑰屽畾鍒惰嚜宸辩殑 `cout`锛 鍙兘閲嶈浇鎴愬叏灞鍑芥暟銆傛鏃讹紝鎿嶄綔鏁扮殑鏁扮洰绛変簬鍑芥暟鐨勫弬鏁颁釜鏁般 -```c++ +```cpp ostream & operator<<(ostream &o, const CStudent &s) +//姝ゅ锛孋Student鐨勫紩鐢ㄥ拰CStudent閮借兘瀹炵幇鍔熻兘锛屾垜浠夋嫨浣跨敤寮曠敤鐨勫師鍥犲湪浜庯紝鎴戜滑涓嶉渶瑕佸舰鍙傚璞★紝鑰屽舰鍙傚璞$殑鐢熸垚闇瑕佽皟鐢ㄥ鍒舵瀯閫犲嚱鏁帮紝浜х敓寮閿 { - o << s.nAge; - return o; + o << s.nAge; + return o; //姝ゆ椂杩斿洖鍊间负ostream,褰撹皟鐢╟out< #include #include @@ -349,7 +361,7 @@ ostream & operator<<(ostream &os, const Complex &c) istream & operator>>(istream &is, Complex &c) { string s; - is >> s; + is >> s;//灏哸+bi浣滀负瀛楃涓茶鍏ワ紝涓棿涓嶈兘鏈夌┖鏍 int pos = s.find("+", 0); string sTmp = s.substr(0, pos); c.real = atof(sTmp.c_str()); @@ -384,7 +396,7 @@ int main() { ### 鍚庣疆杩愮畻绗︿綔涓轰簩鍏冭繍绠楃閲嶈浇 -- 澶氬啓涓涓弬鏁, 鏃犲叿浣撴剰涔 +- 澶氬啓涓涓弬鏁, 鍙槸鏍囪閲嶈浇鐨勬槸鍚庣疆杩愮畻绗︼紝鏃犲叿浣撴剰涔 - 閲嶈浇涓烘垚鍛樺嚱鏁帮細 - `T operator++(int); ` - `T operator--(int); ` @@ -393,7 +405,7 @@ int main() { - `T operator--(T &, int); ` - `obj++`, `obj.operator++(0)`, `operator++(obj,0)` 閮借皟鐢ㄤ笂鍑芥暟 -```c++ +```cpp #include #include #include @@ -462,7 +474,7 @@ int main() { ### 杩愮畻绗﹂噸杞界殑娉ㄦ剰浜嬮」 -- C++涓嶅厑璁稿畾涔夋柊鐨勮繍绠楃 +- cpp涓嶅厑璁稿畾涔夋柊鐨勮繍绠楃 - 閲嶈浇鍚庤繍绠楃鐨勫惈涔夊簲璇ョ鍚堟棩甯镐範鎯 - 杩愮畻绗﹂噸杞戒笉鏀瑰彉杩愮畻绗︾殑浼樺厛绾 - 浠ヤ笅杩愮畻绗︿笉鑳借閲嶈浇锛 `.`, `.*`, `::`, `?:`, `sizeof` diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week5/README.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week5/README.md" index 0a0da87..9ea891e 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week5/README.md" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week5/README.md" @@ -13,7 +13,7 @@ 渚嬶細浠ュ鐢熶负鍩虹被锛屾淳鐢熷嚭鐮旂┒鐢燂紝澶у鐢燂紝涓鐢熻繖浜涘瓙绫汇 -```c++ +```cpp class 娲剧敓绫诲悕: public 鍩虹被鍚 { ... @@ -46,7 +46,7 @@ class 娲剧敓绫诲悕: public 鍩虹被鍚 渚嬩竴锛氣濆渾鈥滅被鍖呭惈鈥濈偣鈥滅被锛岃繖鏄渾鍜岀偣灏辨槸澶嶅悎鍏崇郴 -```c++ +```cpp class Circle { double radius; @@ -56,7 +56,7 @@ class Circle 渚嬩簩锛氫汉鐙楀叧绯伙紒鏈濂界殑鍐欐硶鏄簰鐩告寚鍚 -```c++ +```cpp class Master; class Dog @@ -102,7 +102,7 @@ class Master 渚嬶細 -```c++ +```cpp class Father { private: @@ -148,7 +148,7 @@ int main() ### 鍒濆鍖栧弬鏁板垪琛 -```c++ +```cpp 鏋勯犲嚱鏁板悕锛堝舰鍙傝〃锛夛細鍩虹被鍚嶏紙鍩虹被鏋勯犲嚱鏁板疄鍙傝〃锛 { ... @@ -180,7 +180,7 @@ int main() **鏋愭瀯鍑芥暟鐨勮皟鐢ㄩ『搴忎笌鏋勯犲嚱鏁扮殑璋冪敤椤哄簭鐩稿弽** -```c++ +```cpp class Base { public: @@ -225,7 +225,7 @@ int main() ## public缁ф壙鐨勮祴鍊煎吋瀹硅鍒 -```c++ +```cpp class base {}; class derived : public base {}; base b; @@ -240,4 +240,8 @@ derived d; ## 鐩存帴鍩虹被鍜岄棿鎺ュ熀绫 -鍦ㄥ0鏄庢淳鐢熺被鏃讹紝鍙渶瑕佸垪鍑哄畠鐨勭洿鎺ュ熀绫伙紝娲剧敓绫绘部鐫绫荤殑灞傛鑷姩鍚戜笂缁ф壙瀹冪殑闂存帴鍩虹被 \ No newline at end of file +鍦ㄥ0鏄庢淳鐢熺被鏃讹紝鍙渶瑕佸垪鍑哄畠鐨勭洿鎺ュ熀绫伙紝娲剧敓绫绘部鐫绫荤殑灞傛鑷姩鍚戜笂缁ф壙瀹冪殑闂存帴鍩虹被 + +## Reference + +https://www.coursera.org/learn/cpp-chengxu-sheji/supplement/JYzTL/ke-cheng-slides \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_string\347\261\273.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_string\347\261\273.md" new file mode 100644 index 0000000..11b4eb6 --- /dev/null +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_string\347\261\273.md" @@ -0,0 +1,179 @@ +## string绫 + +`string`绫绘槸涓涓ā鏉跨被锛屽畠鐨勫畾涔夊涓嬶細 + +```cpp +typedef basic_string string; +``` + +浣跨敤string绫昏鍖呭惈澶存枃浠 `` + + string瀵硅薄鐨勫垵濮嬪寲: + +- ` string s1("Hello"); // 涓涓弬鏁扮殑鏋勯犲嚱鏁 ` +- `string s2(8, 鈥榵鈥);` + `// 涓や釜鍙傛暟鐨勬瀯閫犲嚱鏁;8浠h〃瀛楃鍑虹幇鐨勬鏁癭 +- ` string month = 鈥淢arch鈥; ` +- 涓嶆彁渚涗互瀛楃鍜屾暣鏁颁负鍙傛暟鐨勬瀯閫犲嚱鏁 锛屾晠鑰屼笅杩板垵濮嬪寲鏂规硶鍧囦负閿欒璋冪敤锛 +```cpp +string error1 = 'c'; +string error2('u'); +string error3 = 22; +string error4(8); +``` +褰撴垜浠渶瑕佹妸涓涓瓧绗﹁祴鍊肩粰`string`瀵硅薄鏃讹紝鍙互璋冪敤璧嬪煎嚱鏁帮紝鑰岄潪鏋勯犲嚱鏁帮細 +```cpp +string s; +s = 'p'; +``` +### 浣跨敤`string`绫绘椂鐨勪竴浜涙敞鎰忎簨椤癸細 +- 鏋勯犵殑string澶暱鑰屾棤娉曡〃杈句細鎶涘嚭`length_error`寮傚父 + +- string 瀵硅薄鐨勯暱搴︾敤鎴愬憳鍑芥暟 `length()` 璇诲彇锛 +```cpp +string s("hello"); +cout<>stringObject; +``` +- string 鏀寔 `getline` 鍑芥暟锛 +```cpp +string s; +getline(cin, s); +``` +### string鐨勫鍒朵笌杩炴帴 +#### 澶嶅埗 +- 鐢╜=`澶嶅埗锛 +```cpp +string s1("cat"),s2; +s2 = s1; +``` +- 鐢╜assign()`鎴愬憳鍑芥暟杩涜閮ㄥ垎澶嶅埗锛 + +```cpp +string s1("Karlsruhe"), s3; +s3.assign(s1, 1, 3); +//浠巗1 涓笅鏍囦负1鐨勫瓧绗﹀紑濮嬪鍒3涓瓧绗︾粰s3 +``` +- 鍗曚釜瀛楃鐨勫鍒讹細 +```cpp +s2[5] = s1[5] = 'a'; +``` +- 閫愪釜璁块棶string瀵硅薄涓殑瀛楃锛 +```cpp +string s1("Hello"); +for(int i = 0;i,>=... + - 杩斿洖鍊间负`bool`绫诲瀷锛屾垚绔嬩负`True` + ```cpp + string s1("hello"),s2("hell"); + bool b = (s1==s2); + ``` +- 鍒╃敤鎴愬憳鍑芥暟`compare()` + +```cpp +string s1("hello"), s2("hello"), s3("hell"); +int f1 = s1.compare(s2); +int f2 = s1.compare(s3); +int f3 = s3.compare(s1); +int f4 = s1.compare(1, 2, s3, 0, 3); //s1 1-2; s3 0-3 +int f5 = s1.compare(0, s1.size(), s3); //s1 0-end +cout << f1 << endl << f2 << endl << f3 << endl; +cout << f4 << endl << f5 << endl; + +杈撳嚭 +0 // hello == hello +1 // hello > hell +-1 // hell < hello +-1 // el < hell +1 // hello > hell +``` + +### 瀛愪覆 `substr(begin, size)` +```cpp +string s1("hello world"),s2; +s2 = s1.substr(4,5); +//涓嬫爣4寮濮嬶紝5涓瓧绗 +cout< +杩斿洖鍊肩被鍨 妯℃澘鍚(褰㈠弬琛) +{ + 鍑芥暟浣 +} +``` + +- 渚嬶細浜ゆ崲鍑芥暟 + +```c++ +template +void swap(T &x, T &y) +{ + T tmp = x; + a = b; + b = tmp; +} +// 缂栬瘧鍣ㄤ細鑷姩鐢熸垚鐩稿簲鐨勫嚱鏁 +int main() { + int n = 1, m = 2; + Swap(n,m); + //缂栬瘧鍣ㄨ嚜鍔ㄧ敓鎴恦oid Swap(int &, int &)鍑芥暟 + double f = 1.2, g =2.3; + //缂栬瘧鍣ㄨ嚜鍔ㄧ敓鎴恦oid Swap(double &,double &)鍑芥暟锛屽苟杩涜璋冪敤锛 + Swap(f,g); + return 0; +} + +``` +**Notes:** +- 鍑芥暟妯℃澘涓彲浠ユ湁涓嶆涓涓被鍨嬪弬鏁帮紝鍗冲嚱鏁扮被鍨嬩笉涓鑷淬傚涓嬮潰鐨勪緥瀛愶細 + ```cpp + template//閫氳繃瀹氫箟涓嶅悓绫诲瀷鐨勬ā鏉 + T2 print(T1 arg1, T2 arg2) { + cout< + void print(T1 arg1,T2 arg2) {} + + template + void print(T arg1, T arg2) {} + ``` + +## 缂栬瘧鍣ㄥ尮閰嶅嚱鏁扮殑椤哄簭 + +- **Step 1:** 鍏堟壘鍙傛暟瀹屽叏鍖归厤鐨勬櫘閫氬嚱鏁(闈炵敱妯℃澘瀹炰緥鍖栬屽緱鐨勫嚱鏁) +- **Step 2:** 鍐嶆壘鍙傛暟瀹屽叏鍖归厤鐨勬ā鏉垮嚱鏁 +- **Step 3:** 鍐嶆壘瀹炲弬缁忚繃鑷姩绫诲瀷杞崲鍚庤兘澶熷尮閰嶇殑鏅氬嚱鏁 +- **Step 4:** 涓婇潰鐨勯兘鎵句笉鍒, 鍒欐姤閿 + +涓嬮潰鎴戜滑鐢ㄤ竴涓緥瀛愭潵璇存槑锛屾ā鏉胯皟鐢ㄩ『搴忥細 +```cpp +template +T Max(T a, T b) { + cout<<"Template Max 1"< +T Max(T a, T2 b) { + cout<<"Template Max 2"< +T myFunction(T arg1, T arg2) +{ + cout<SomeFunction(); //璋冪敤鍝釜鍩哄嚱鏁板彇鍐充簬p鎸囧悜鍝绫诲瀷鐨勫璞 + return 0; +} +``` + +#### 寮曠敤澶氭 + +娲剧敓绫荤殑瀵硅薄鍙互璧嬬粰鍩虹被寮曠敤 + +閫氳繃鍩虹被寮曠敤璋冪敤鍩虹被鍜屾淳鐢熺被涓殑鍚屽悕铏氬嚱鏁版椂锛 + +- 鑻ヨ寮曠敤寮曠敤鐨勬槸涓涓熀绫荤殑瀵硅薄锛岄偅涔堣璋冪敤鏄熀绫荤殑铏氬嚱鏁 +- 鑻ヨ寮曠敤寮曠敤鐨勬槸涓涓淳鐢熺被鐨勫璞★紝閭d箞 琚皟鐢ㄧ殑鏄淳鐢熺被鐨勮櫄鍑芥暟銆 + +杩欑鏈哄埗涔熷彨鍋氣滃鎬佲濄 +```cpp +class CBase { + public: + virtual void SomeFunction() {} +}; + +class CDeruved: public CBase{ + public: + virtual void SomeFunction() {} +}; + +int main() { + CDerived ODerived; + CBase &r = ODerived;//鍩虹被鐨勫紩鐢 寮曠敤娲剧敓绫荤殑瀵硅薄 + r.SomeVirtualFunction();//璋冪敤鍝釜铏氬嚱鏁板彇鍐充簬r寮曠敤瀵硅薄鐨勭被鍨 + return 0; +} + +``` +### 澶氭佺殑浣滅敤 + +鍦ㄩ潰鍚戝璞$殑绋嬪簭璁捐涓娇鐢ㄥ鎬侊紝鑳藉澧炲己 绋嬪簭鐨**鍙墿鍏**鎬э紝鍗崇▼搴忛渶瑕佷慨鏀规垨澧炲姞鍔熻兘鐨勬椂鍊欙紝闇瑕佹敼鍔ㄥ拰澧炲姞鐨勪唬鐮佽緝灏戙 + +## 澶氭佸疄渚嬶細 鍑犱綍褰綋澶勭悊绋嬪簭 + +鍑犱綍褰綋澶勭悊绋嬪簭: 杈撳叆鑻ュ共涓嚑浣曞舰浣撶殑鍙傛暟锛 瑕佹眰鎸夐潰绉帓搴忚緭鍑恒傝緭鍑烘椂瑕佹寚鏄庡舰鐘躲 + +Input: + +绗竴琛屾槸鍑犱綍褰綋鏁扮洰n锛堜笉瓒呰繃100).涓嬮潰鏈塶琛岋紝姣忚浠ヤ竴涓瓧姣峜寮澶. + +鑻 c 鏄 鈥楻鈥欙紝鍒欎唬琛ㄤ竴涓煩褰紝鏈鍚庨潰璺熺潃涓や釜鏁存暟锛屽垎鍒槸鐭╁舰鐨勫鍜岄珮锛 + +鑻 c 鏄 鈥楥鈥欙紝鍒欎唬琛ㄤ竴涓渾锛屾湰琛屽悗闈㈣窡鐫涓涓暣鏁颁唬琛ㄥ叾鍗婂緞 + +鑻 c 鏄 鈥楾鈥欙紝鍒欎唬琛ㄤ竴涓笁瑙掑舰锛屾湰琛屽悗闈㈣窡鐫涓変釜鏁存暟锛屼唬琛ㄤ笁鏉¤竟鐨勯暱搴 + +Output: + +鎸夐潰绉粠灏忓埌澶т緷娆¤緭鍑烘瘡涓嚑浣曞舰浣撶殑绉嶇被鍙婇潰绉傛瘡琛屼竴涓嚑浣曞舰浣擄紝杈 鍑烘牸寮忎负锛 + +褰綋鍚嶇О锛氶潰绉 + +```c++ +#include +#include +#include +using namespace std; + +class Shape +{ +public: + virtual double Area() = 0; // 绾櫄鍑芥暟锛侊紒 + virtual void PrintInfo() = 0; +}; + +class Rectangle : public Shape +{ +public: + int w, h; + virtual double Area() { return w*h; } + virtual void PrintInfo() + { + cout << "Rectangle: " << Area() << endl; + } +}; + +class Circle :public Shape +{ +public: + int r; + virtual double Area() { return 3.14*r*r; } + virtual void PrintInfo() + { + cout << "Circle: " << Area() << endl; + } +}; + +class Triangle :public Shape +{ +public: + int a, b, c; + virtual double Area() + { + double p = (a + b + c) / 2.0; + return sqrt(p*(p - a)*(p - b)*(p - c)); + } + virtual void PrintInfo() + { + cout << "Triangle: " << Area() << endl; + } +}; + +int MyCompare(const void *s1, const void *s2) +{ + Shape **p1 = (Shape**)s1; + Shape **p2 = (Shape**)s2; + + double a1 = (*p1)->Area(); + double a2 = (*p2)->Area(); + + if (a1 < a2) + { + return -1; + } + else if (a1 > a2) + { + return 1; + } + else + { + return 0; + } +} + +int main() +{ + Shape *pShape[100]; + int n = 0; + cin >> n; + Rectangle *pr; + Circle *pc; + Triangle *pt; + for (int i = 0; i < n; i++) + { + char c; + cin >> c; + switch (c) + { + case 'R': + pr = new Rectangle(); + cin >> pr->w >> pr->h; + pShape[i] = pr; + break; + case 'C': + pc = new Circle(); + cin >> pc->r; + pShape[i] = pc; + break; + case 'T': + pt = new Triangle(); + cin >> pt->a >> pt->b >> pt->c; + pShape[i] = pt; + break; + } + } + qsort(pShape, n, sizeof(Shape*), MyCompare); + for (int i = 0; i < n; i++) + { + pShape[i]->PrintInfo(); + } + return 0; +} +``` +**灏廡ipps** +鐢ㄥ熀绫绘寚閽堟暟缁勫瓨鏀炬寚鍚戝悇绉嶆淳鐢熺被瀵硅薄鐨勬寚閽堬紝鐒跺悗閬嶅巻璇ユ暟缁勶紝灏辫兘瀵瑰悇涓淳鐢熺被瀵硅薄鍋氬悇绉嶆搷浣滐紝鏄緢甯哥敤鐨勫仛娉曘 + +### 鏋勯犲嚱鏁板拰鏋愭瀯鍑芥暟涓皟鐢ㄨ櫄鍑芥暟 + +鍦ㄦ瀯閫犲嚱鏁板拰鏋愭瀯鍑芥暟涓皟鐢ㄨ櫄鍑芥暟锛屼笉鏄鎬併傜紪璇戞椂鍗冲彲纭畾锛岃皟鐢ㄧ殑鍑芥暟鏄嚜宸辩殑绫绘垨鍩虹被涓畾涔夌殑鍑芥暟锛屼笉浼氱瓑鍒拌繍琛屾椂鎵嶅喅瀹氳皟鐢ㄨ嚜宸辩殑杩樻槸娲剧敓绫荤殑鍑芥暟銆 + +娲剧敓绫讳腑鍜屽熀绫讳腑铏氬嚱鏁板悓鍚嶅悓鍙傛暟琛ㄧ殑鍑芥暟锛屼笉鍔燻virtual`涔熻嚜鍔ㄦ垚涓鸿櫄鍑芥暟銆 + + + +## 澶氭佺殑瀹炵幇鍘熺悊 +鈥滃鎬佲濈殑鍏抽敭鍦ㄤ簬閫氳繃鍩虹被鎸囬拡鎴栧紩鐢ㄨ皟鐢ㄤ竴涓櫄鍑芥暟鏃讹紝缂栬瘧鏃朵笉纭畾鍒板簳璋冪敤鐨勬槸鍩虹被杩樻槸娲剧敓绫荤殑鍑芥暟锛岃繍琛屾椂鎵嶇‘瀹氾紝鍗充负鈥**鍔ㄦ佽仈缂**鈥濄傛垜浠潵鐪嬩笅闈㈢殑绋嬪簭锛 +```c++ +class Base { + public: + int i; + virtual void Print() {cout<<"Base print"<鐢辨娊璞$被娲剧敓鍑烘潵鐨勭被鐨勫璞 + +鎶借薄绫讳腑锛 + +- 鍦ㄦ垚鍛樺嚱鏁板唴鍙互璋冪敤绾櫄鍑芥暟 +- 鍦ㄦ瀯閫犲嚱鏁/鏋愭瀯鍑芥暟鍐呴儴涓嶈兘璋冪敤绾櫄鍑芥暟 + +濡傛灉涓涓被浠庢娊璞$被娲剧敓鑰屾潵,鍙湁瀹冨疄鐜颁簡鍩虹被涓殑鎵鏈夌函铏氬嚱鏁帮紝鎵嶈兘鎴愪负闈炴娊璞$被 +## 娣峰悎绫诲瀷 +鍦ㄤ竴涓▼搴忎腑锛屾淳鐢熺被涓庡熀绫诲彲鑳藉瓨鍦ㄥ绉嶆儏鍐电殑鍚屽悕鎴愬憳鍑芥暟锛屽弬瑙佷笅闈㈢▼搴忥細 +```cpp +#include +using namespace std; +class A { + private: + int nVal; + public: + void Fun() + { cout << "A::Fun" << endl; } + virtual void Do() + { cout << "A::Do" << endl; } +}; +class B:public A { + public: + virtual void Do() + { cout << "B::Do" << endl; } +}; +class C:public B { + public: + void Do( ) + { cout << "C::Do" << endl; } + void Fun() + { cout << "C::Fun" << endl; } +}; +void Call( A *p +// 鍦ㄦ澶勮ˉ鍏呬綘鐨勪唬鐮 + ) { + p->Fun(); p->Do(); +} +int main() { + Call( new A() ); + Call( new C() ); + return 0; +} +``` +璇ョ▼搴忕殑杈撳嚭缁撴灉涓猴細 +```cpp +A::Fun +A::Do +A::Fun +C::Do +``` +鍦ㄨ皟鐢╜Call(new C())`鏃讹紝鐢变簬鍩虹被涓殑`Fun()`鍑芥暟涓嶆槸铏氬嚱鏁帮紝涓嶆瀯鎴愬鎬侊紝鑰宍Call()`鍑芥暟涓紝`p`鐨勭被鍨嬩负`A * {C}`,鍗虫寚鍚戞淳鐢熺被`C`鐨勫熀绫籤A`鎸囬拡锛屾晠鑰岃皟鐢ㄥ熀绫籄涓嬬殑`Fun()`鍑芥暟銆 + +## References +- [1] https://github.com/chiuchiuuu/programming-and-algorithm +- [2] https://www.coursera.org/learn/cpp-chengxu-sheji/lecture \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_\346\226\207\344\273\266\345\244\204\347\220\206.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_\346\226\207\344\273\266\345\244\204\347\220\206.md" new file mode 100644 index 0000000..220a240 --- /dev/null +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_\346\226\207\344\273\266\345\244\204\347\220\206.md" @@ -0,0 +1,245 @@ +# Week7锛氭枃浠舵搷浣滃拰妯℃澘 + +## 鏂囦欢鎿嶄綔 + +椤哄簭鏂囦欢锛氫竴涓湁闄愬瓧绗︽瀯鎴愮殑椤哄簭瀛楃娴 + +鏂囦欢娴**绫**锛氱敤浜庢枃浠舵搷浣 + +- `ifstream`:璇诲彇鏁版嵁 +- `ofsteam`:鍐欏叆鏁版嵁 +- `fstream`锛氳鍐欐暟鎹 + +浣跨敤鐢/鍒涘缓鏂囦欢鐨勫熀鏈祦绋 + +- 鎵撳紑鏂囦欢 + - 閫氳繃鍒跺畾鏂囦欢鍚嶏紝寤虹珛鏂囦欢鍜屾枃浠舵祦瀵硅薄鐨勫叧鑱 + - 鎸囨槑鏂囦欢鐨勪娇鐢ㄦ柟寮 +- 璇诲啓鏂囦欢 + - 鍒╃敤璇/鍐欐寚閽堣繘琛岀浉搴斾綅缃殑鎿嶄綔 +- 鍏抽棴鏂囦欢 + +### 鎵撳紑鏂囦欢 + +- 鍒濆鍖栧璞℃椂鎵撳紑 +- 鍒濆鍖栧悗鐢╜open`鍑芥暟鎵撳紑 + +```c++ +#include +ofstream outFile("clients.dat", ios::out | ios::binary)锛 +//ofstream鏃秄stream涓畾涔夌殑绫 +//outFile鏃惰嚜瀹氫箟鐨刼fstream绫荤殑瀵硅薄 +//xx.dat鏄皢瑕佸缓绔嬬殑鏂囦欢鐨勬枃浠跺悕 +//ios::xxx 鎵撳紑骞跺缓绔嬫枃浠剁殑閫夐」 + + +ofstream fout; +fout.open("test.out", ios::out | ios::binary); +//绗簩绉嶅缓绔嬮『搴忔枃浠剁殑鏂规硶锛氬厛鍒涘缓ofstream瀵硅薄锛屽啀鐢╫pen鍑芥暟鎵撳紑 +``` + +鎵撳紑閫夐」 + +- `ios::out` 锛氳緭鍑哄埌鏂囦欢锛屽垹闄ゅ師鏈夊唴瀹 +- `ios::app`锛氳緭鍑哄埌鏂囦欢锛屼繚鐣欏師鏈夊唴瀹癸紝鏂板姞鍏ョ殑鍐呭鎬绘槸鍦ㄥ熬閮ㄦ坊鍔 +- `ios::binary`锛氫互浜岃繘鍒舵枃浠舵牸寮忔墦寮鏂囦欢 + +鏂囦欢鍚嶏細 + +- 缁濆璺緞 +- 鐩稿璺緞 +- 濡傛灉娌℃湁浜や唬璺緞淇℃伅锛屽氨鏄湪褰撳墠鏂囦欢涓嬫壘鏂囦欢 + +鍒ゆ柇鎵撳紑鏄惁鎴愬姛 + +```c++ +if (!fout) +{ + cerr << "File open error!!" << endl; +} +``` + +### 鏂囦欢鐨勮鍐欐寚閽 + +- 瀵逛簬杈撳叆鏂囦欢锛屾湁涓涓鎸囬拡 +- 瀵逛簬杈撳嚭鏂囦欢锛屾湁涓涓啓鎸囬拡 +- 瀵逛簬杈撳叆杈撳嚭鏂囦欢锛屾湁涓涓鍐欐寚閽 + +璇/鍐欐寚閽堢敤浜庢爣璇嗘枃浠舵搷浣滅殑褰撳墠浣嶇疆, 璇ユ寚閽堝湪鍝噷锛岃鍐欐搷浣滃氨鍦ㄥ摢閲岃繘琛 銆 + +鐩稿叧鍑芥暟锛 + +- `tellp()`, `seekp()` +- `tellg()`, `seekg()` + +```c++ +//鍐欐寚閽 +ofstream fout("a1.out", ios::app); +long location = fout.tellp(); //鍙栧緱鍐欐寚閽堢殑浣嶇疆 +location = 10L; // 鍙负璐熷笺10L鏄洜涓簂ocation鏄竴涓猯ong鍨嬪彉閲 +fout.seekp(location); // 灏嗗啓鎸囬拡绉诲姩鍒扮10涓瓧鑺傚銆俿eek postion +fout.seekp(location, ios::beg); // 浠庡ご鏁發ocation銆 +fout.seekp(location, ios::cur); // 浠庡綋鍓嶄綅缃暟location +fout.seekp(location, ios::end); // 浠庡熬閮ㄦ暟location +``` + +```c++ +//璇绘寚閽 +ifstream fin(鈥渁1.in鈥,ios::in); +long location = fin.tellg(); //鍙栧緱璇绘寚閽堢殑浣嶇疆 +location = 10L; +fin.seekg(location); //灏嗚鎸囬拡绉诲姩鍒扮10涓瓧 +fin.seekg(location, ios::beg); //浠庡ご鏁發ocation +fin.seekg(location, ios::cur); //浠庡綋鍓嶄綅缃暟location +fin.seekg(location, ios::end); //浠庡熬閮ㄦ暟location +``` + +### 浜岃繘鍒舵枃浠惰鍐 + +浜岃繘鍒舵枃浠惰鍐欐槸鐩存帴璇诲啓浜岃繘鍒舵暟鎹紝濡傛灉鐩存帴鎷胯浜嬫湰鏉ョ湅锛岀粨鏋滄湭蹇呮纭 + +```c++ +int x=10; +fout.seekp(20, ios::beg);//浠巄eginning寮濮嬬殑20涓瓧鑺傚紑濮嬪啓璧 +fout.write((const char *)(&x), sizeof(int)); +//鍐欏叆x +fin.seekg(0, ios::beg); +fin.read((char *)(&x), sizeof(int)); +``` + +### 渚嬪瓙锛氫簩杩涘埗鏂囦欢璇诲啓 +#### 浜岃繘鍒舵枃浠跺啓鍏 +```cpp +#include +#include +#include +using namespace std; +class CStudent { +public: + char szName[20]; + int nScore; +}; +int main() { + CStudent s; + ofstream OutFile("students.dat",ios::out | ios::binary); + //娉ㄦ剰鍐欏叆褰㈠紡涓轰簩杩涘埗褰㈠紡 + while (cin >> s.szName >> s.nScore) { + if (stricmp(s.szName, "exit") == 0) + break;//鍚嶅瓧涓篹xit鍒欑粨鏉 + OutFile.write((char *) &s, sizeof(s)); + //鍐欏叆鐨勫璞℃槸s鐨勫紩鐢 + } + OutFile.close(); + return 0; +} +``` +**Note:** +鏂囨湰鏂囦欢/浜岃繘鍒舵枃浠舵墦寮鏂囦欢鐨勫尯鍒細 +- 鍦║nix涓嶭inux涓嬶紝浜岃呬竴鑷达紝娌℃湁鍖哄埆锛 +- 鍦╓indows涓嬶紝鏂囨湰鏂囦欢鏄互`"\r\n"`浣滀负鎹㈣绗 + - 璇诲嚭鏃讹紝绯荤粺浼氬皢`0x0d0a`鍙鍏0x0a`(`0x`浠h〃浜岃繘鍒) + - 鍐欏叆鏃讹紝瀵逛簬`0x0a`绯荤粺浼氳嚜鍔ㄥ啓鍏0x0d` + +#### 浜岃繘鍒舵枃浠惰鍑 +```cpp +#include +#include +#include +using namespace std; +class CStudent { +public: + char szName[20]; + int nScore; +}; +int main() { + CStudent s; + ifstream inFile("studen.dat", ios::in | ios::binary); + if (!inFile) {//鍒ゆ柇鏂囦欢鏄惁鎵撳紑鎴愬姛 + cout << "error" << endl; + return 0; + } + while (inFile.read((char *) &s, sizeof(s))) + { + int nReadedBytes = inFile.gcount();//璇诲彇浜嗗灏戝瓧鑺 + cout << s.szName << " " << s.nScore << endl; + } + inFile.close(); + return 0; +} +```` +#### 浜岃繘鍒舵枃浠惰鍐 +```cpp +#include +#include +#include +using namespace std; +class CStudent { +public: + char szName[20]; + int nScore; +}; +int main() { + CStudent s; + fstream iofile("students.dat",ios::in | ios::out | ios::binary); + if (!inFile) {//鍒ゆ柇鏂囦欢鏄惁鎵撳紑鎴愬姛 + cout << "error" << endl; + return 0; + } + iofile.seekp(2*sizeof(s),ios::beg); + iofile.write("Mike",strlen("Mike")+1); + iofile.seekg(0,ios::beg); + while (inFile.read((char *) &s, sizeof(s))) + { + cout << s.szName << " " << s.nScore << endl; + } + iofile.close(); + return 0; +} +``` + +#### 鏂囦欢鎷疯礉绀轰緥绋嬪簭 + +```c++ +#include +#include +using namespace std; +//鐢ㄦ硶绀轰緥锛 +//mycopy src.dat dest.dat +//濡傛灉dest.dat鍘熸潵灏辨湁锛屽垯琚鐩 + +int main(int argc, char *argv[]) +//鍛戒护琛屽弬鏁扮殑浣跨敤 +{ + if (argc != 3) + { + cerr << "File name missing!" << endl; + return 1; + } + ifstream inFile(argv[1], ios::binary | ios::in);//鎵撳紑鏂囦欢鐢ㄤ簬璇诲彇 + if (!inFile) + { + cout << "inFile open error!" << endl; + return 2; + } + + ofstream outFile(argv[2], ios::binary | ios::out); + if (!outFile) + { + cout << "outFile open error!" << endl; + inFile.close(); + return 3; + } + + char c; + while (inFile.get(c)) + { + outFile.put(c); + } + outFile.close(); + inFile.close(); + return 0; +} +``` +## Reference +- [1] https://www.coursera.org/learn/cpp-chengxu-sheji/ +- [2] https://github.com/chiuchiuuu/programming-and-algorithm \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_\350\276\223\345\205\245\344\270\216\350\276\223\345\207\272.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_\350\276\223\345\205\245\344\270\216\350\276\223\345\207\272.md" new file mode 100644 index 0000000..9cc7873 --- /dev/null +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/cpp_\350\276\223\345\205\245\344\270\216\350\276\223\345\207\272.md" @@ -0,0 +1,119 @@ + +## 杈撳叆鍜岃緭鍑 + +### 涓庤緭鍏ヨ緭鍑烘祦鎿嶄綔鐩稿叧鐨勭被 + +- `istream`鏄敤浜庤緭鍏ョ殑娴佺被锛宍cin`灏辨槸璇ョ被鐨勫璞° +- `ostream`鏄敤浜庤緭鍑虹殑娴佺被锛宍cout`灏辨槸璇ョ被鐨勫璞° +- `ifstream`鏄敤浜庝粠鏂囦欢璇诲彇鏁版嵁鐨勭被銆 +- `ofstream`鏄敤浜庡悜鏂囦欢鍐欏叆鏁版嵁鐨勭被銆 +- `iostream`鏄棦鑳界敤浜庤緭鍏ワ紝鍙堣兘鐢ㄤ簬杈撳嚭鐨勭被銆 +- `fstream `鏄棦鑳戒粠鏂囦欢璇诲彇鏁版嵁锛屽張鑳藉悜鏂囦欢鍐欏叆鏁版嵁鐨勭被銆 + +杩欎簺绫荤殑娲剧敓鍏崇郴鍙互鐢ㄤ笅鍥炬潵琛ㄨ揪锛 + +```mermaid +graph TD; +A[ios] --> B[istream] +A --> C[ostream] +B --> D[ifstream] +B --> E[iostream] +C --> E +C --> F[ofstream] +E --> G[fstream] +``` + +### 鏍囧噯娴佸璞 + + 杈撳叆娴佸璞: + +- `cin`涓庢爣鍑嗚緭鍏ヨ澶囩浉杩 + +杈撳嚭娴佸璞★細 + +- `cout`涓庢爣鍑嗚緭鍑鸿澶囩浉杩 +- `cerr`涓庢爣鍑嗛敊璇緭鍑鸿澶囩浉杩 +- `clog`涓庢爣鍑嗛敊璇緭鍑鸿澶囩浉杩 + +Tipps: Why we need cerr? + +閫氳繃閲嶅畾鍚戯紝浣垮緱`cout`鍜宍cerr`闈㈠悜涓嶅悓鐨勫湴鏂硅緭鍑烘暟鎹紝杩欏氨鑳借揪璋冭瘯淇℃伅鍜岀▼搴忕湡姝e簲璇ヨ緭鍑虹殑缁撴灉鍒嗗紑杈撳嚭鐨勭洰鐨勩 + +`cin`瀵瑰簲浜庢爣鍑嗚緭鍏ユ祦锛岀敤浜庝粠閿洏璇诲彇鏁版嵁锛屼篃鍙互琚**閲嶅畾鍚** 涓轰粠鏂囦欢涓鍙栨暟鎹 + +`cout` 瀵瑰簲浜庢爣鍑嗚緭鍑烘祦锛岀敤浜庡悜灞忓箷杈撳嚭鏁版嵁锛屼篃鍙互琚**閲嶅畾鍚**涓哄悜鏂囦欢鍐欏叆鏁版嵁銆 + +`cerr` 瀵瑰簲浜庢爣鍑嗛敊璇緭鍑烘祦锛岀敤浜庡悜灞忓箷杈撳嚭鍑洪敊淇℃伅 + +`clog` 瀵瑰簲浜庢爣鍑嗛敊璇緭鍑烘祦锛岀敤浜庡悜灞忓箷杈撳嚭鍑洪敊淇℃伅 + +> `cerr`鍜宍clog`鐨勫尯鍒湪浜巂cerr`涓嶄娇鐢ㄧ紦鍐插尯,鐩存帴鍚戞樉绀哄櫒杈撳嚭淇 鎭紱鑰岃緭鍑哄埌`clog`涓殑淇℃伅鍏堜細琚瓨鏀惧湪缂撳啿鍖,缂撳啿鍖烘弧鎴栬 鍒锋柊鏃舵墠杈撳嚭鍒板睆骞曘 + +### 閲嶅畾鍚 + +杈撳嚭閲嶅畾鍚 + +```cpp +#indlude +using namespace std; +int main(){ + int x,y; + cin>>x>>y; + freopen("test.txt","w",stdout); + //stdout榛樿涓哄湪灞忓箷涓婃墦鍗 + //灏嗘爣鍑嗚緭鍑洪噸瀹氬悜鍒皌est.txt鏂囦欢锛寃浠h〃鍐 + if(y==0) //鍦ㄥ睆骞曚笂杈撳嚭閿欒淇℃伅 + cerr<<"error."<> x) +{ + ... +} +//绗簩绉嶆柟娉曪細 +if (!cin.getline()) +//娉ㄦ剰锛歡etline()璇诲埌鐣欏湪娴佷腑鐨刓n灏变細杩斿洖 +``` +- 濡傛灉浠庢枃浠惰緭鍏ワ紝閭d箞璇诲埌鏂囦欢灏鹃儴灏辩畻缁撴潫锛 +- 濡傛灉鏃堕敭鐩樿緭鍏ワ紝鍒欏湪鍗曠嫭涓琛岃緭鍏ctrl+z`浠h〃杈撳叆缁撴潫 +`cin` 浣滀负鏉′欢鍒ゆ柇璇彞鍏跺疄鏃跺仛浜嗕竴涓己鍒剁被鍨嬭浆鎹紒娌℃湁璇诲埌杈撳叆鏃惰繑鍥瀈false` + +### `istream`绫荤殑鎴愬憳鍑芥暟 + +```cpp +istream & getline(char * buf, int bufSize); +``` + +浠庤緭鍏ユ祦涓鍙 `bufSize-1` 涓瓧绗﹀埌缂撳啿鍖 `buf`锛屾垨璇诲埌纰板埌 `\n` 涓烘锛堝摢涓厛鍒扮畻鍝釜锛夈 + +```cpp +istream & getline(char * buf, int bufSize,char delim); +``` + +浠庤緭鍏ユ祦涓鍙 `bufSize-1` 涓瓧绗﹀埌缂撳啿鍖 `buf`锛屾垨璇诲埌纰板埌 `delim` 瀛楃涓烘锛堝摢涓厛鍒扮畻鍝釜锛夈 + +涓や釜鍑芥暟閮戒細鑷姩鍦 `buf` 涓鍏ユ暟鎹殑缁撳熬娣诲姞 `\0`銆俙\n` 鎴 `delim` 閮戒笉浼氳璇诲叆 `buf`锛屼絾**浼氳浠庤緭鍏ユ祦涓彇璧,鍗冲垹鎺**銆傚鏋滆緭鍏ユ祦涓 `\n` 鎴 `delim` 涔嬪墠鐨勫瓧绗︿釜鏁拌揪鍒版垨瓒呰繃浜 `bufSize` 涓紝灏卞鑷磋鍏ュ嚭閿欙紝鍏剁粨鏋滃氨鏄細铏界劧鏈璇诲叆宸茬粡瀹屾垚锛屼絾浼氬奖鍝嶄箣鍚庣殑鎿嶄綔锛屽嵆鏄箣鍚庣殑璇诲叆灏遍兘浼氬け璐ヤ簡銆 + +- `bool eof()`锛氬垽鏂緭鍏ユ祦鏄惁缁撴潫 +- `int peek()`锛氳繑鍥炰笅涓涓瓧绗,浣嗕笉浠庢祦涓幓鎺夈備娇鐢ㄥ満鏅細鏍规嵁涓嬩竴涓瓧绗︾殑绫诲瀷鍐冲畾鎿嶄綔锛屽悓鏃朵笉褰卞搷瀛楃涓茬殑瀹屾暣鎬с +- `istream& putback(char c);`锛氬皢瀛楃 `ch` 鏀惧洖杈撳叆娴 +- `istream& ignore(int nCount = 1, int delim = EOF);`锛氫粠娴佷腑鍒犳帀鏈澶 `nCount` 涓瓧绗︼紝閬囧埌 `EOF` 鏃剁粨鏉熴 + +## Reference +- [1] https://www.coursera.org/learn/cpp-chengxu-sheji/ +- [2] https://github.com/chiuchiuuu/programming-and-algorithm \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" index 335ffee..c397b20 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week7/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" @@ -1,67 +1,56 @@ #include -#include -#include -#include -#include +#include +#include +#include using namespace std; string str[21]; -int NumOfStr = 0; +int num_of_str = 0; -bool isNum(string s) -{ - for (int i = 0; i < s.length(); i++) - { - if (!isdigit(s[i])) - { +int string_to_int(string string1) { + int number; + istringstream num(string1); + num >> number; + return number; +} +bool is_Num(string s) { + for (int i = 0; i < s.length(); i++) { + if (!isdigit(s[i])) { return false; } } return true; } -int find() -{ +int rfind() { char s; int n; cin >> s >> n; - return str[n].find(s); + return str[n].rfind(s); } - -int rfind() -{ +int find() { char s; int n; cin >> s >> n; - return str[n].rfind(s); + return str[n].find(s); } - -int get_int() -{ +int get_int() { string str; cin >> str; - if (isNum(str)) - { - return stoi(str); + if (is_Num(str)) { + return string_to_int(str); } - else - { - if (str == "find") - { - return find(); - } - else if (str == "rfind") - { - return rfind(); - } + else if (str == "rfind") { + return rfind(); + } + else if (str == "find") { + return find(); } - return 0; } +string add();//此处的函数用来进行占位,防止报错 - -string copy() -{ +string copy() { int n, x, l; n = get_int(); x = get_int(); @@ -69,33 +58,32 @@ string copy() return str[n].substr(x, l); } -string add(); - -string get_str() -{ +string get_char() { string str; cin >> str; - if (str == "copy") - { + if (str == "copy") { return copy(); } - else if (str == "add") - { + else if (str == "add") { return add(); } - else - { + else { return str; } } -string add() -{ - string s1 = get_str(); - string s2 = get_str(); - if (isNum(s1) && isNum(s2) && stoi(s1) >= 0 && stoi(s1) <= 99999 && stoi(s2) >= 0 && stoi(s2) <= 99999) +string int_to_string(int d) { + ostringstream ss; + ss << d; + return ss.str(); +} + +string add() { + string s1 = get_char(); + string s2 = get_char(); + if (is_Num(s1) && is_Num(s2) && string_to_int(s1) >= 0 && string_to_int(s1) <= 99999 && string_to_int(s2) >= 0 && string_to_int(s2) <= 99999) { - return to_string(stoi(s1) + stoi(s2)); + return int_to_string(string_to_int(s1) + string_to_int(s2)); } else { @@ -105,40 +93,37 @@ string add() -void insert() -{ - string s = get_str(); - int n = get_int(); - int x = get_int(); - str[n].insert(x, s); -} -void reset() -{ - string s = get_str(); - int n = get_int(); - str[n] = s; + + +void insert() { + string S = get_char(); + int num_str = get_int(); + int num_word = get_int(); + str[num_str].insert(num_word, S); } -void print() -{ - int n = get_int(); +void print() { + int n; + cin >> n; cout << str[n] << endl; } -void printall() -{ - for (int i = 1; i <= NumOfStr; i++) - { +void printall() { + for (int i = 1; i <= num_of_str; i++) cout << str[i] << endl; - } } +void reset() { + string s = get_char(); + int n = get_int(); + str[n].replace(str[n].begin(), str[n].end(), s); +} int main() { - cin >> NumOfStr; - for (int i = 1; i <= NumOfStr; i++) - { + cin >> num_of_str; + + for (int i = 1; i <= num_of_str; i++) { cin >> str[i]; } while (true) @@ -166,6 +151,4 @@ int main() break; } } - return 0; - } \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week8/cpp_\346\240\207\345\207\206\346\250\241\346\235\277\345\272\223 STL\346\246\202\350\277\260.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week8/cpp_\346\240\207\345\207\206\346\250\241\346\235\277\345\272\223 STL\346\246\202\350\277\260.md" new file mode 100644 index 0000000..d58305b --- /dev/null +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week8/cpp_\346\240\207\345\207\206\346\250\241\346\235\277\345\272\223 STL\346\246\202\350\277\260.md" @@ -0,0 +1,828 @@ +# 鏍囧噯妯℃澘搴 STL + +## 姒傝堪 + +STL锛歴tandard template library + +c++閲岀殑閲嶇敤 + +- 闈㈠悜瀵硅薄锛氱户鎵裤佸鎬併佹爣鍑嗙被搴 +- 娉涘瀷绋嬪簭璁捐锛氭ā鏉挎満鍒讹紝STL + +### 娉涘瀷绋嬪簭璁捐 + +灏嗕竴浜涘父鐢ㄧ殑鏁版嵁缁撴瀯锛堟瘮濡傞摼琛紝鏁扮粍锛屼簩鍙夋爲锛夊拰绠楁硶锛堟瘮濡傛帓搴忥紝鏌ユ壘锛夊啓鎴愭ā鏉匡紝浠ュ悗鍒欎笉璁烘暟鎹粨鏋勯噷鏀剧殑鏄粈涔堝璞★紝绠楁硶閽堝浠涔堟牱鐨勫璞★紝鍒欓兘涓嶅繀閲嶆柊瀹炵幇鏁版嵁缁撴瀯锛岄噸鏂扮紪鍐欑畻娉曘 + +### 鍩烘湰姒傚康 + +- 瀹瑰櫒锛氬绾冲悇绉嶆暟鎹被鍨嬬殑閫氱敤鏁版嵁缁撴瀯锛屾槸绫绘ā鏉 +- 杩唬鍣細鍙敤浜庝緷娆″瓨鍙栧鍣ㄤ腑鍏冪礌锛岀被浼兼寚閽 +- 绠楁硶锛氱敤鏉ユ搷浣滃鍣ㄤ腑鐨勫厓绱犵殑鍑芥暟妯℃澘 + - $sort()$ 鏉ュ涓涓*vector* 涓殑鏁版嵁杩涜鎺掑簭 + - *find()*鏉ユ悳绱*list()*涓殑瀵硅薄 + - 绠楁硶鏈韩涓庝粬浠搷浣滅殑鏁版嵁鐨勭被鍨嬫棤鍏 + +```c++ +int array[100]; +sort(array, array+70); //灏嗗墠70涓厓绱犺繘琛屾帓搴 +// 瀹瑰櫒锛氭暟缁 +// 杩唬鍣紝int*绫诲瀷鐨勬寚閽 +// 绠楁硶锛歴ort +``` + +## 瀹瑰櫒 + +鍙互鐢ㄤ簬瀛樻斁鍚勭绫诲瀷鐨勬暟鎹紙鍩烘湰绫诲瀷鐨勫彉閲忥紝瀵硅薄绛夛級鐨勬暟鎹粨鏋勶紝閮芥槸**绫绘ā鏉** + +- 椤哄簭瀹瑰櫒锛歚vector鍔ㄦ佹暟缁刞, `deque鍙屽悜闃熷垪`, `list鍙屽悜閾捐〃` +- 鍏宠仈瀹瑰櫒锛歚set`, `multiset`, `map`, `multimap` +- 瀹瑰櫒閫傞厤鍣細`stack`, `queue`, `priority_queue` + +瀵硅薄琚彃鍏ュ鍣ㄤ腑鏃讹紝琚彃鍏ョ殑鏄璞$殑涓涓鍒跺搧銆傝澶氱畻娉曪紝姣斿鎺掑簭锛屾煡鎵撅紝瑕佹眰瀵瑰鍣ㄤ腑鐨勫厓绱犺繘琛屾瘮杈冿紝鏈夌殑瀹瑰櫒鏈韩灏辨槸鎺掑簭鐨勶紝鎵浠ワ紝鏀惧叆瀹瑰櫒鐨勫璞℃墍灞炵殑绫伙紝寰寰杩樺簲璇ラ噸杞 `==` 鍜 `<` 杩愮畻绗︺ + +### 椤哄簭瀹瑰櫒 + +瀹瑰櫒骞堕潪鎺掑簭鐨勶紝鍏冪礌鐨勬彃鍏ヤ綅缃悓鍏冪礌鐨勫兼棤鍏炽 + +- `vector`锛氬ご鏂囦欢 `` + - 鍔ㄦ佹暟缁勩傚厓绱犲湪鍐呭瓨杩炵画瀛樻斁銆傞殢鏈哄瓨鍙栦换浣曞厓绱犻兘鑳藉湪甯告暟鏃堕棿瀹屾垚銆傚湪**灏剧**澧炲垹鍏冪礌鍏锋湁杈冧匠鐨勬ц兘(澶ч儴鍒嗘儏鍐典笅鏄父鏁版椂闂达紝濡傛灉瓒呭嚭棰勫垎閰嶇┖闂寸殑澶у皬鏃朵负$O(n)$,姝ゆ椂闇瑕侀噸鏂板垎閰嶇┖闂达級銆傚湪寮澶存垨鑰呬腑闂存彃鍏ヤ负$O(n)$. +- `deque`锛氬ご鏂囦欢 `` + - 鍙屽悜闃熷垪銆傚厓绱犲湪鍐呭瓨杩炵画瀛樻斁銆傞殢鏈哄瓨鍙栦换浣曞厓绱犻兘鑳藉湪甯告暟鏃堕棿瀹屾垚(浣嗘浜巚ector: 鑰冭檻鎯呭喌锛$tail$鎸囬拡鍦$head$鐨勫墠鏂癸紝姝ゆ椂鍦ㄦ暟鎹鍙栫殑杩囩▼涓紝闇瑕佸涓姝ョ殑閫昏緫鍒ゆ柇)銆傜浉杈冧簬$vector$鍦**涓ょ**澧炲垹鍏冪礌鍏锋湁杈冧匠鐨勬ц兘(澶ч儴鍒嗘儏鍐典笅鏄父鏁版椂闂达級 + - 鏈変袱涓寚閽堬細`head`, `tail` +- `list`锛氬ご鏂囦欢 `` + - 鍙屽悜閾捐〃銆傚厓绱犲湪鍐呭瓨涓嶈繛缁瓨鏀俱傚湪浠讳綍浣嶇疆澧炲垹鍏冪礌閮借兘鍦ㄥ父鏁版椂闂村畬鎴( 鍏冪礌浣嶇疆纭畾鐨勬儏鍐典笅)銆**涓嶆敮鎸侀殢鏈哄瓨鍙**锛屽嵆涓嶈兘閫氳繃涓嬫爣$i$鐩存帴璁块棶鍏冪礌銆 + +### 鍏宠仈瀹瑰櫒 + +- 鍏冪礌鏄**鎺掑簭**鐨 +- 鎻掑叆浠讳綍鍏冪礌锛岄兘鎸夌浉搴旂殑鎺掑簭瑙勫垯鏉ョ‘瀹氬叾浣嶇疆 +- 鍦ㄦ煡鎵炬椂鍏锋湁闈炲父濂界殑鎬ц兘 +- 閫氬父浠ュ钩琛′簩鍙夋爲鏂瑰紡瀹炵幇锛屾彃鍏ュ拰妫绱㈢殑鏃堕棿閮芥槸 $O(\log(N))$ +- `set/muitiset`锛氬ご鏂囦欢 `` + - 闆嗗悎銆俿et涓笉鍏佽鐩稿悓鍏冪礌锛宮ultiset涓厑璁稿瓨鍦ㄧ浉鍚岀殑鍏冪礌銆 +- `map/multimap`锛 澶存枃浠 `` + - map涓巗et鐨勪笉鍚屽湪浜巑ap涓瓨鏀剧殑鍏冪礌鏈変笖浠呮湁涓や釜鎴愬憳鍙橀噺锛屼竴涓悕涓 `first`,鍙︿竴涓悕涓 `second`, `map`鏍规嵁 `first` 鍊煎鍏冪礌杩涜浠庡皬鍒板ぇ鎺掑簭锛 骞跺彲蹇熷湴鏍规嵁 `first` 鏉ユ绱㈠厓绱犮 $first$鐩稿綋浜庡叧閿瓧锛$second$鐩稿綋浜$value$鍊笺 + - `map` 鍚 `multimap` 鐨勪笉鍚屽湪浜庢槸鍚﹀厑璁哥浉鍚 `first` 鍊肩殑鍏冪礌銆 + +### 瀹瑰櫒閫傞厤鍣 + +- `stack`锛氬ご鏂囦欢 `` + - 鏍堛傛槸椤圭殑鏈夐檺搴忓垪锛屽苟婊¤冻搴忓垪涓鍒犻櫎銆佹绱㈠拰淇敼鐨勯」鍙兘鏄渶杩戞彃鍏 搴忓垪鐨勯」锛堟爤椤剁殑椤癸級銆**鍚庤繘鍏堝嚭**銆 +- `queue`锛氬ご鏂囦欢 `` + - 鍗曞悜闃熷垪銆傛彃鍏ュ彧鍙互鍦ㄥ熬閮ㄨ繘琛岋紝 鍒犻櫎銆佹绱㈠拰淇敼鍙厑璁镐粠澶撮儴杩涜銆**鍏堣繘鍏堝嚭**銆 +- `priority_queue`锛 澶存枃浠 `` + - 浼樺厛绾ч槦鍒椼傛渶楂樹紭鍏堢骇鍏冪礌鎬绘槸绗竴涓嚭鍒 + +### 鎴愬憳鍑芥暟 + +椤哄簭瀹瑰櫒鍜屽叧鑱斿鍣ㄥ叡鏈夛細 + +- `begin` 杩斿洖鎸囧悜瀹瑰櫒涓涓涓厓绱犵殑杩唬鍣 +- `end` 杩斿洖鎸囧悜瀹瑰櫒涓渶鍚庝竴涓厓绱**鍚庨潰**鐨勪綅缃殑杩唬鍣 +- `rbegin` 杩斿洖鎸囧悜瀹瑰櫒涓渶鍚庝竴涓厓绱犵殑杩唬鍣 +- `rend` 杩斿洖鎸囧悜瀹瑰櫒涓涓涓厓绱犲墠闈㈢殑浣嶇疆鐨勮凯浠e櫒 +- `erase` 浠庡鍣ㄤ腑鍒犻櫎涓涓垨鍑犱釜鍏冪礌 +- `clear` 浠庡鍣ㄤ腑鍒犻櫎鎵鏈夊厓绱 + +椤哄簭瀹瑰櫒锛 + +- `front` :杩斿洖瀹瑰櫒涓涓涓厓绱犵殑寮曠敤 +- `back` : 杩斿洖瀹瑰櫒涓渶鍚庝竴涓厓绱犵殑寮曠敤 +- **`push_back`** : 鍦ㄥ鍣ㄦ湯灏惧鍔犳柊鍏冪礌 +- `pop_back` : 鍒犻櫎瀹瑰櫒鏈熬鐨勫厓绱 +- `erase` :鍒犻櫎杩唬鍣ㄦ寚鍚戠殑鍏冪礌(鍙兘浼氫娇璇ヨ凯浠e櫒澶辨晥锛夛紝鎴栧垹闄や竴涓尯闂达紝杩斿洖琚垹闄ゅ厓绱犲悗闈㈢殑閭d釜鍏冪礌鐨勮凯浠e櫒 + +## 杩唬鍣 + +- 鐢ㄤ簬鎸囧悜椤哄簭瀹瑰櫒鍜屽叧鑱斿鍣ㄤ腑鐨勫厓绱 +- 杩唬鍣ㄧ敤娉曞拰鎸囬拡绫讳技 +- 鏈 `const` 鍜岄潪 `const` 涓ょ +- 閫氳繃杩唬鍣ㄥ彲浠ヨ鍙栧畠鎸囧悜鐨勫厓绱 +- 閫氳繃闈 `const` 杩唬鍣ㄨ繕鑳戒慨鏀瑰叾鎸囧悜鐨勫厓绱 + +瀹氫箟涓涓鍣ㄧ被鐨勮凯浠e櫒鐨勬柟娉曪細锛堝鍣ㄧ被锛氫粠瀹瑰櫒妯℃澘瀹炰緥鍖栧嚭鏉ョ殑绫伙級 + +```c++ +瀹瑰櫒绫诲悕::iterator 鍙橀噺鍚; +瀹瑰櫒绫诲悕::const_interator 鍙橀噺鍚; +``` + +璁块棶涓涓凯浠e櫒鎸囧悜鐨勫厓绱狅細 + +```c++ +*杩唬鍣ㄥ彉閲忓悕 +``` + +杩唬鍣ㄤ笂鍙互鎵ц `++` 鎿嶄綔, 浠ヤ娇鍏舵寚鍚戝鍣ㄤ腑鐨勪笅涓涓厓绱犮 濡傛灉杩唬鍣ㄥ埌杈句簡瀹瑰櫒涓殑鏈鍚庝竴涓厓绱犵殑鍚庨潰锛屾鏃跺啀浣跨敤瀹冿紝灏变細鍑洪敊锛岀被浼间簬浣跨敤NULL鎴栨湭鍒濆鍖栫殑鎸囬拡涓鏍枫 + +渚 + +```c++ +#include +#include +using namespace std; + +int main() +{ + vector v; //涓涓瓨鏀緄nt鍏冪礌鐨勬暟缁勶紝涓寮濮嬮噷闈㈡病鏈夊厓绱犮 + v.push_back(1); + v.push_back(2); + v.push_back(3);//鍦ㄥ熬閮ㄦ坊鍔 + vector::const_iterator i; //甯搁噺杩唬鍣ㄣ俢onst:鍙兘璁块棶锛屼笉鑳戒慨鏀 + for (i = v.begin(); i != v.end(); i++) //娉ㄦ剰v.end()杩斿洖鐨勬槸鏈鍚庝竴涓厓绱犵殑鍚庝竴涓綅缃 + { + cout << *i << ","; + } + cout << endl; + // 鍙嶅悜杩唬鍣 + vector::reverse_iterator r;//涓巌terator绫诲瀷涓嶅吋瀹 + for (r = v.rbegin(); r != v.rend(); r++)//瀵瑰弽鍝嶈凯浠e櫒鐨++鎿嶄綔锛屼細鎸囧悜鍓嶄竴涓厓绱 + { + cout << *r << ","; + } + cout << endl; + vector::iterator j;//闈炲父閲忚凯浠e櫒 + for (j = v.begin(); j != v.end(); j++) + { + *j = 100; + } + for (i = v.begin(); i != v.end(); i++) + { + cout << *i << ","; + } + return 0; +} +``` + +STL甯哥敤鐨勮凯浠e櫒鏈変袱绉嶏細鍙屽悜杩唬鍣ㄥ拰闅忔満璁块棶杩唬鍣ㄣ + +### 鍙屽悜杩唬鍣 + +鍙繘琛岀殑鎿嶄綔锛 + +- `++p`, `p++` 浣 `p` 鎸囧悜瀹瑰櫒涓笅涓涓厓绱 +- `--p`, `p--` 浣 `p` 鎸囧悜瀹瑰櫒涓笂涓涓厓绱 +- `*p` 鍙朻p`鎸囧悜鐨勫 +- 璧嬪硷紝鍒ゆ柇鐩哥瓑 `p==p1` `p!=p1` + +### 闅忔満璁块棶杩唬鍣 + +鍙繘琛岀殑鎿嶄綔 + +- 鍙屽悜杩唬鍣ㄧ殑鎵鏈夋搷浣 +- `p += i` 灏唒鍚戝悗绉诲姩`i`涓厓绱 +- `p -= i` 灏 `p` 鍚戝悜鍓嶇Щ鍔╜i`涓厓绱 +- `p + i` 鍊间负: 鎸囧悜 `p` 鍚庨潰鐨勭`i`涓厓绱犵殑**杩唬鍣** +- `p - i` 鍊间负: 鎸囧悜 `p` 鍓嶉潰鐨勭`i`涓厓绱犵殑杩唬鍣 +- `p[i]` 鍊间负: `p`鍚庨潰鐨勭`i`涓厓绱犵殑**寮曠敤** +- 姣旇緝澶у皬`< >` `p v(100); +for (int i = 0; i < v.size(); i++) +{ + cou << v[i];//閫氳繃涓嬫爣闅忔満璁块棶 +} +// 杩唬鍣 +vector::const_iterator i; +for (i = v.begin(); i < v.end(); i++) +{ + cout << *i; +} +// 闂撮殧涓涓緭鍑 +i = v.begin(); +while (i < v.end()) +{ + cout << *i; + i += 2; +} +``` + +#### 閬嶅巻 `list` + +```c++ +list v; +list::const_iterator i; +for (i = v.begin(); i != v.end(); i++) // 涓嶅彲鍐欐垚 i < v.end()銆傚洜涓簂ist涓婇潰鐨勮凯浠e櫒鏄弻鍚戣凯浠e櫒锛屽弻鍚戣凯浠e櫒涓嶈兘鐢<杩涜姣旇緝銆 +//list娌℃湁[]鎴愬憳鍑芥暟 +{ + cout << *i; +} +``` + +## 绠楁硶绠浠 + +- 绠楁硶灏辨槸涓涓釜鍑芥暟妯℃澘, 澶у鏁板湪 `` 涓畾涔 +- STL涓彁渚涜兘鍦ㄥ悇绉嶅鍣ㄤ腑閫氱敤鐨勭畻娉曪紝姣斿鏌ユ壘锛屾帓搴忕瓑 +- 绠楁硶閫氳繃杩唬鍣ㄦ潵鎿嶇旱瀹瑰櫒涓殑鍏冪礌銆傝澶氱畻娉曞彲浠ュ瀹瑰櫒涓殑涓涓 灞閮ㄥ尯闂磋繘琛屾搷浣滐紝鍥犳闇瑕佷袱涓弬鏁帮紝涓涓槸璧峰鍏冪礌鐨勮凯浠e櫒锛 涓涓槸缁堟鍏冪礌鐨勫悗闈竴涓厓绱犵殑杩唬鍣ㄣ傛瘮濡傦紝鎺掑簭鍜屾煡鎵 +- 鏈夌殑绠楁硶杩斿洖涓涓凯浠e櫒銆傛瘮濡 `find()` 绠楁硶锛屽湪瀹瑰櫒涓煡鎵句竴涓厓 绱狅紝骞惰繑鍥炰竴涓寚鍚戣鍏冪礌鐨勮凯浠e櫒 +- 绠楁硶鍙互澶勭悊瀹瑰櫒锛屼篃鍙互澶勭悊鏅氭暟缁 + +### 绀轰緥锛歚find()` + +鍘熷瀷锛 + +```c++ +template +InIt find(InIt first, InIt last, const T &val); +``` + +- `first` 鍜 `last` 杩欎袱涓弬鏁伴兘鏄鍣ㄧ殑杩唬鍣紝瀹冧滑缁欏嚭浜嗗鍣ㄤ腑鐨 鏌ユ壘鍖洪棿璧风偣鍜岀粓鐐筦[first,last)`锛**宸﹂棴鍙冲紑**銆傚尯闂寸殑璧风偣鏄綅浜庢煡鎵捐寖鍥翠箣涓 鐨勶紝鑰岀粓鐐逛笉鏄俙find()`鍦╗first,last)鏌ユ壘绛変簬`val`鐨勫厓绱 +- 鐢╜==`杩愮畻绗﹀垽鏂浉绛 +- 鍑芥暟杩斿洖鍊兼槸涓涓凯浠e櫒銆傚鏋滄壘鍒帮紝鍒欒杩唬鍣ㄦ寚鍚戣鎵惧埌鐨勫厓绱犮 濡傛灉鎵句笉鍒帮紝鍒欒杩唬鍣ㄧ瓑浜 `last` +- 椤哄簭鏌ユ壘鐨勬椂闂村鏉傚害鏄$O(n)$ + +```c++ +#include +#include +#include +using namesapce std; + +int main() +{ + int array[10] = { 10,20,30,40 }; + vector v; + v.push_back(1); + v.push_back(2); + v.push_back(3); + v.push_back(4); + vector::iterator p; + p = find(v.begin(), v.end(), 3); //娉ㄦ剰find()鍑芥暟鐨勮繑鍥炲兼槸涓涓凯浠e櫒 + //v.begin()涔熸槸涓涓凯浠e櫒 + if (p != v.end()) + { + cout << *p << endl; + } + p = find(v.begin(), v.end(), 9); + if (p == v.end()) + { + cout << "not found" << endl; + } + p = find(v.begin() + 1, v.end() - 2, 1);//鏌ユ壘鍖洪棿锛 [2,3);鏌ユ壘涓嶅埌锛屼細杩斿洖鏌ユ壘鏃跺湴last,姝ゅ涓3 + if (p != v.end()) + { + cout << *p << endl; + } + int *pp = find(array, array + 4, 20); // 鏁扮粍鍚嶅氨鏄凯浠e櫒锛屽洜涓烘暟缁勫悕灏辨槸int*绫诲瀷鐨勬寚閽 + cout << *pp << endl; + return 0; +} +``` + +## STL涓殑澶у皬涓庣浉绛 + +### 澶у皬 + +- 鍏宠仈瀹瑰櫒鍐呴儴鐨勫厓绱犳槸浠庡皬鍒板ぇ鎺掑簭鐨 +- 鏈変簺绠楁硶瑕佹眰鍏舵搷浣滅殑鍖洪棿鏄粠灏忓埌澶ф帓搴忕殑锛岀О涓衡滄湁搴忓尯闂寸畻娉曗 渚嬶細binary_search +- 鏈変簺绠楁硶浼氬鍖洪棿杩涜浠庡皬鍒板ぇ鎺掑簭锛岀О涓衡滄帓搴忕畻娉曗 渚嬶細 sort +- 鍙互璁や负瀵**浠庡皬鍒板ぇ**鐨勬蹇佃繘琛屽畾涔夛紝姣斿鎸夌収鏁板瓧鐨勪釜浣嶆暟瀛楃殑澶у皬鍏崇郴杩涜姣旇緝鈥斺旇嚜瀹氫箟澶у皬 +- 杩樻湁涓浜涘叾浠栫畻娉曚細鐢ㄥ埌鈥滃ぇ鈥濓紝鈥滃皬鈥濈殑姒傚康 +- 浣跨敤STL鏃讹紝鍦ㄧ己鐪佺殑鎯呭喌涓嬶紝浠ヤ笅涓変釜璇存硶绛変环锛 + - x姣攜灏 + - `op(x, y)` 杩斿洖鍊间负 `true`锛坄op`涓鸿嚜瀹氫箟鐨勬瘮杈冨嚱鏁帮級 + - y姣攛澶 + +### 鐩哥瓑 + +x鍜寉鐩哥瓑琛ㄧず + +- `x==y` + - 渚嬶細鍦ㄦ湭鎺掑簭鐨勫尯闂翠笂杩涜鐨勭畻娉曪紝濡傞『搴忔煡鎵緁ind +- x灏忎簬y**鍜**y灏忎簬x鍚屾椂涓哄亣锛屾敞鎰忔澶勭殑灏忎簬鍙互鑷畾涔 + - 鏈夊簭鍖洪棿绠楁硶锛屽 `binary_search` + - 鍏宠仈瀹瑰櫒鑷韩鐨勬垚鍛樺嚱鏁癴ind + +### 绀轰緥锛 STL涓滅浉绛夆濇蹇垫紨绀 + +```c++ +#include +#include +using namespace std; + +class A +{ + int v; +public: + A(int n):v(n){} + bool operator<(const A &a2) const //杩愮畻绗﹂噸杞:涓や釜class A鐨勫璞★紝涓涓兘涓嶄細灏忎簬鍙︿竴涓 + { + cout << v << "<" << a2.v << "?" << endl; + return false; + } + bool operator==(const A &a2) const //鍒ゆ柇涓や釜绫绘槸鍚︾浉绛 + { + cout << v << "==" << a2.v << "?" << endl; + return v == a2.v; + } +}; + +int main() +{ + A a[] = { A(1),A(2),A(3),A(4),A(5) }; + cout << binary_search(a, a+4, A(9)); // 鐢 < 鍋氭瘮杈冿紝鎶樺崐鏌ユ壘 + return 0锛 +} +``` + +杈撳嚭缁撴灉锛 + +``` +3<9? +2<9? +1<9? +9<1? +1 +``` + +娉ㄦ剰鍒板湪杈撳嚭涓病鏈夌敤鍒癭==`鍙凤紝鑰屾槸澶氭璋冪敤`<`銆傚绋嬪簭鑰岃█锛屽鏋渀1<9`涓嶆垚绔嬶紝鍚屾椂`9<1`涓嶆垚绔嬶紝灏变細璁や负`9=1` + +## `vector` + +- 鍙彉闀跨殑鍔ㄦ佹暟缁 +- 蹇呴』澶存枃浠 `#include ` +- 鏀寔闅忔満璁块棶杩唬鍣 + - 鏍规嵁涓嬫爣闅忔満璁块棶鏌愪釜鍏冪礌鏃堕棿涓哄父鏁 + - 鍦ㄥ熬閮ㄦ坊鍔犻熷害寰堝揩锛堣秴鍑洪鐣欑┖闂撮渶瑕侀噸鏂板垎閰嶇┖闂达紝閫氬父涓32涓鐣欑┖闂达級 + - 鍦ㄤ腑闂存彃鍏ユ參锛堥渶瑕佺Щ鍔ㄥ悗闈㈡墍鏈夌殑鍏冪礌锛 +- 鎵鏈塖TL绠楁硶閮借兘瀵 `vector` 鎿嶄綔 + +### 鎴愬憳鍑芥暟 + +#### 鏋勯犲嚱鏁板垵濮嬪寲 + +| 鎴愬憳鍑芥暟 | 浣滅敤 | +| --------------------------------------- | :----------------------------------------------------------- | +| `vector()` | 鏃犲弬鏋勯犲嚱鏁, 灏嗗鍣ㄥ垵濮嬪寲鎴愮┖ | +| `vector(int n)` | 灏嗗鍣ㄥ垵濮嬪寲鎴愭湁n涓厓绱犵殑鏁扮粍 | +| `vector(int n, const T &val)` | 鍋囧畾鍏冪礌绫诲瀷鏄疶, 灏嗗鍣ㄥ垵濮嬪寲鎴愭湁n涓厓绱, 姣忎釜鍏冪礌鐨勫奸兘鏄痸al | +| `vector(iterator first, iterator last)` | 灏嗗鍣ㄥ垵濮嬪寲涓轰笌**鍒殑**瀹瑰櫒涓婂尯闂 [first, last**)**涓鑷寸殑鍐呭 | + +#### 鍏朵粬甯哥敤鍑芥暟 + +| 鎴愬憳鍑芥暟 | 浣滅敤 | +| ------------------------------ | ---------------------------- | +| `void pop_back()` | 鍒犻櫎瀹瑰櫒鏈熬鐨勫厓绱 | +| `void push_back(const T &val)` | 灏唙al娣诲姞鍒板鍣ㄦ湯灏 | +| `int size()` | 杩斿洖瀹瑰櫒涓厓绱犵殑涓暟 | +| `T &front()` | 杩斿洖瀹瑰櫒涓涓涓厓绱犵殑寮曠敤 | +| `T &back()` | 杩斿洖瀹瑰櫒涓渶鍚庝竴涓厓绱犵殑寮曠敤 | + +### 涓缁村姩鎬佹暟缁 + +```cpp +#include +#include +using namespace std; + +int main() +{ + int a[5] = { 1,2,3,4,5 }; + vector v(5);//鍔ㄦ佹暟缁勶紝鍏朵腑鐨勬瘡涓涓厓绱犻兘鏄痠nt绫诲瀷鐨勶紝鍖呭惈鏈5涓厓绱 + cout << v.end() - v.begin() << endl; // 杩斿洖vector澶у皬锛屾敞鎰弙ector瀵瑰簲鐨勮凯浠e櫒鏄殢鏈鸿凯浠e櫒 + for (int i = 0; i < v.size(); i++) + { + v[i] = i;//浣跨敤涓嬫爣璁块棶 + } + v.at(4) = 100;//绗簲涓厓绱 + for (int i = 0; i < v.size(); i++) + { + cout << v[i] << ","; + } + cout << endl; + vector v2(a, a + 5);//浣跨敤绗洓绉嶆柟娉曞垵濮嬪寲锛歷ector(iterator first, iterator last) + v2.insert(v2.begin() + 2, 13); + for (int i = 0; i < v2.size(); i++) + { + cout << v2.at(i) << ","; + } +} +``` + +### 浜岀淮鍔ㄦ佹暟缁 + +```c++ +vector> v(3); +// v鏈変笁涓厓绱 +// 姣忎釜鍏冪礌閮芥槸 vector +``` + +浣跨敤渚嬪瓙 + +```c++ +#include +#include +using namespace std; +int main() +{ + vector> v(3);//v鏈変笁涓厓绱狅紝姣忎釜鍏冪礌閮芥槸vector瀹瑰櫒 + for (int i = 0; i < v.size(); i++) + { + for (int j = 0; j < 4; j++) + { + v[i].push_back(j); + } + } + for (int i = 0; i < v.size(); i++) + { + for (int j = 0; j < 4; j++) + { + cout << v[i][j] << ","; + } + cout << endl; + } + return 0锛 +} +``` + + + +## `list` + +- 鍙屽悜閾捐〃 +- `#include ` +- 鍦ㄤ换浣曚綅缃彃鍏/鍒犻櫎閮芥槸甯哥啛鏃堕棿 +- 涓嶆敮鎸佹牴鎹笅鏍囬殢鏈哄瓨鍙栧厓绱 +- 鍏锋湁鎵鏈夐『搴忓鍣ㄧ殑鎴愬憳鍑芥暟 +- ![ZCCoJe.png](https://s2.ax1x.com/2019/06/23/ZCCoJe.png) + + + +### 鎴愬憳鍑芥暟 + +| 鎴愬憳鍑芥暟 | 浣滅敤 | +| ------------ | :----------------------------------------------------------- | +| `push_front` | 鍦ㄩ摼琛ㄦ渶鍓嶉潰鎻掑叆 | +| `pop_front` | 鍒犻櫎閾捐〃鏈鍓嶉潰鐨勫厓绱 | +| `sort` | 鎺掑簭 (list 涓嶆敮鎸 STL 鐨勭畻娉 sort) | +| `remove` | 鍒犻櫎鍜屾寚瀹氬肩浉绛夌殑鎵鏈夊厓绱 | +| `unique` | 鍒犻櫎鎵鏈夊拰鍓嶄竴涓厓绱犵浉鍚岀殑鍏冪礌 | +| `merge` | 鍚堝苟涓や釜閾捐〃, 骞舵竻绌哄叾涓鍚堝苟鐨勯摼琛 | +| `reverse` | 棰犲掗摼琛 | +| `splice` | 鍦ㄦ寚瀹氫綅缃墠闈㈡彃鍏ュ彟涓閾捐〃涓殑涓涓垨澶氫釜鍏冪礌, 骞跺湪鍙︿竴閾捐〃涓垹闄よ鎻掑叆鐨勫厓绱 | + +> 鐢变簬`list`鐨勮凯浠e櫒涓嶆敮鎸侀殢鏈鸿闂紝鏁呬笉鑳界敤STL涓殑`sort`鍑芥暟瀵瑰畠杩涜鎺掑簭 +> +> `list` 鑷甫鎺掑簭鍑芥暟sort鎴愬憳鍑芥暟 +> +> ```cpp +> list classname +> classname.sort(compare); // 鑷畾涔夋瘮杈冨嚱鏁 +> classname.sort() // 缂虹渷鎯呭喌涓嬫寜 < 鎺掑簭 +> ``` + +姝ゅ锛岀敱浜巂list`瀹瑰櫒鍙兘浣跨敤鍙屽悜杩唬鍣紝鏁呰屼笉鏀寔澶у皬/灏忎簬姣旇緝杩愮畻绗︼紝[]涓嬫爣杩愮畻绗﹀拰闅忔満绉诲姩锛堝鈥渋+2鈥濓級銆 + + + +### 搴旂敤涓句緥 + +```cpp +#include +#include +#include +#include +using namespace std; +class A { +private: + int n; +public: + A(int n_) { n = n_; } + friend bool operator<(const A & a1, const A& a2); + friend bool operator==(const A & a1, const A& a2); + friend ostream &operator<<( ostream & o, const A& a);//閲嶈浇杈撳嚭鎿嶄綔绗︼紝涓轰簡涓嶪O搴撲繚鎸佷竴鑷达紝閲嶈浇鍑芥暟 + //搴旇鎶妎stream& 浣滀负鍏剁涓涓弬鏁帮紝鎶婄被绫诲瀷const瀵硅薄鐨勫紩鐢ㄤ綔涓虹浜屼釜鍙傛暟锛屽苟杩斿洖瀵筼stream褰㈠弬鐨勫紩鐢 + + +}; +bool operator<(const A &a1, const A &a2) { + return a1.n < a2.n; +}//褰撳鍣ㄤ腑鍏冪礌鏄爣鍑嗙被鍨嬶紙int float char)鎴栬卻tring鏃讹紝鍙互鐩存帴浣跨敤杩欎簺鍑芥暟妯℃澘銆 +//浣嗗鏋滄槸鑷繁瀹氫箟鐨勭被鍨嬫垨鑰呴渶瑕佹寜鐓у叾浠栨柟寮忔帓搴忥紝鍙互鏈変袱绉嶆柟娉曟潵杈惧埌鏁堟灉锛 +//涓绉嶆槸鑷繁鍐欐瘮杈冨嚱鏁般傚彟涓绉嶆槸閲嶈浇绫诲瀷鐨'<'鎿嶄綔璧嬨 +bool operator==(const A &a1, const A & a2) { + return a1.n == a2.n; +} +ostream & operator<<(ostream &o, const A& a) { + o << a.n; + return o; +} +//瀹氫箟鍑芥暟妯℃澘锛屾墦鍗板垪琛ㄤ腑鐨勫璞 +template +void PrintList(const list &lst) { + int tmp = lst.size(); + if (tmp > 0) { + typename list::const_iterator i;//typename鐢ㄦ潵璇存槑list::const_isterator鏄釜绫诲瀷 + i = lst.begin(); + for (i = lst.begin(); i != lst.end(); i++) + cout << *i << ",";//姝ゅ鐨刢out<<璋冪敤浜嗚緭鍑虹閲嶈浇 + for (i = lst.begin(); i != lst.end(); i++) + cout << *i << ","; + } +} +int main() { + listlst1, lst2; + lst1.push_back(1); + lst1.push_back(2); + lst1.push_back(2); + lst1.push_back(4); + lst1.push_back(2); + lst2.push_back(10); lst2.push_back(20); lst2.push_back(30); lst2.push_back(30); + lst2.push_back(30); lst2.push_back(40); lst2.push_back(40); + cout << "1)"; PrintList(lst1); cout << endl; + cout << "2)"; PrintList(lst2); cout << endl; + lst2.sort();// + cout << "3)"; PrintList(lst2); cout << endl; + lst2.pop_front(); + cout << "4)"; PrintList(lst2); cout << endl; + lst1.remove(2); + cout << "5)"; PrintList(lst1); cout << endl; + lst1.unique(); + cout << "6)"; PrintList(lst1); cout << endl; + lst1.merge(lst2); + cout << "7)"; PrintList(lst1); cout << endl; + cout << "8)"; PrintList(lst2); cout << endl; + lst1.reverse(); + cout << "9)"; PrintList(lst1); cout << endl; + lst2.push_back(100); lst2.push_back(200); + lst2.push_back(300); lst2.push_back(400); + list::iterator p1, p2, p3; + p1 = find(lst1.begin(), lst1.end(), 3); + p2 = find(lst2.begin(), lst2.end(), 200); + p3 = find(lst2.begin(), lst2.end(), 400); + lst1.splice(p1, lst2, p2, p3); + cout << "11)"; PrintList(lst1); cout << endl; + cout << "12)"; PrintList(lst2); cout << endl; + system("pause"); +} +``` + + + +杩愯缁撴灉濡備笅锛 + +[![ZkCysH.md.png](https://s2.ax1x.com/2019/06/24/ZkCysH.md.png)](https://imgchr.com/i/ZkCysH) + +## `deque` + +- 鍙屽悜**闃熷垪** +- `#include ` +- 鎵鐢ㄩ傜敤浜巂vector`鐨勬搷浣滈兘閫傜敤浜巂deque` +- 鐗规湁鐨勫嚱鏁帮細`push_front()`, `pop_front()` + + + +## 鍑芥暟瀵硅薄 + +濡傛灉涓涓被閲嶈浇浜嗚繍绠楃鈥渀()`鈥濆垯璇ョ被鐨勫璞″氨鎴愪负鍑芥暟瀵硅薄 + +### 鍑芥暟瀵硅薄鐨勫紩鐢 + +#### 渚1 + +```c++ +class MyAverage +{ +public: + double operator()(int a1, int a2, int a2) + { + return (double)(a1 + a2 + a3) / 3; + } +}; +MyAverage average; // 鍑芥暟瀵硅薄 +cout << average(3,2,3); // 绫讳技璋冪敤鍑芥暟 +//绛変环浜庯細average.operator()(3,2,3) +``` + +#### 渚2锛歚accumulate`婧愮爜 + +```c++ +//鐗堟湰涓锛 +template +_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) +{ + for (; __first != __last; ++__first) + { + __init = __init + *__first;//杩涜绱姞宸ヤ綔 + } + return __init +} +//鐗堟湰浜岋細 +template +_Tp accumulate(_InputIterator __first, _InputIterator __last, _BinaryOPeartion __binary_op) +{ + for (;__first != __last; __first++) + { + __init = __binary_op(__init, *__first); + } + return __init; +} +// _binary_op瀵瑰簲鐨勫疄鍙傚彲浠ユ槸涓嚱鏁帮紝鍑芥暟鎸囬拡鎴栧嚱鏁板璞 +``` + +#### 渚3锛氭眰n娆℃柟鐨勫拰 + +```cpp +#include +#include +#include +#include +#include +using namespace std; + +int SumSquares(int total, int value) +{ + return total + value * value; +} + +// 杈撳嚭鍖洪棿[first,last)涓殑鍏冪礌 +template +void PrintInterval(T first, T last) +{ + for (; first != last; first++) + { + cout << *first << " "; + } + cout << endl; +} + +// 姹俻ower娆℃柟鐨勫嚱鏁板璞$殑绫绘ā鏉 +template +class SumPowers +{ +private: + int power; +public: + SumPowers(int n) :power(n) {} //鏋勯犲嚱鏁 + const T operator()(const T &total, const T &value) + {//璁$畻value鐨刾ower娆℃柟锛屽苟鍔犲埌total涓婇潰 + T v = value; + for (int i = 0; i < power - 1; i++) + { + v = v * value; + } + return total + v; + } +}; + +int main() +{ + const int SIZE = 10; + int a1[] = { 1,2,3,4,5,6,7,8,9,10 }; + vector v(a1, a1 + SIZE); + cout << "1)"; + PrintInterval(v.begin(), v.end()); + int result = accumulate(v.begin(), v.end(), 0, SumSquares); + cout << "2)骞虫柟鍜岋細" << result << endl; + result = accumulate(v.begin(), v.end(), 0, SumPowers(3));//鍑芥暟瀵硅薄 + //SumPowers:绫绘ā鏉 + //SumPowers:妯℃澘绫 + //SumPowers(3):(涓存椂)瀵硅薄 + cout << "3)绔嬫柟鍜岋細" << result << endl; + result = accumulate(v.begin(), v.end(), 0, SumPowers(4)); + cout << "4)鍥涙鏂瑰拰锛" << result << endl; + return 0; +} +``` + +鍏朵腑锛岀粡鐢 + +```cpp +int result = accumulate(v.begin(), v.end(), 0, SumSquares); +//娉ㄦ剰锛氬嚱鏁板悕瀛桽umSquares鍙互鍜屽嚱鏁版寚閽堢浉鍖归厤 +``` + +瀹炰緥鍖栫殑杩囩▼濡備笅锛 + +```cpp +int accumulate(vector::iterator first, vector::iterator last, int init, int (*op)(int,int)) { + for(;, first != last; ++first) + init = op(init, *first); //鍑芥暟鎸囬拡鍦ㄦ鐩稿綋浜庤皟鐢ㄤ簡SumSquares + return init +} +``` + +鑰岋紝缁忕敱 + +```cpp +result = accumulate(v.begin(), v.end(), 0, SumPowers(3)); +//鏈鍚庝竴涓负鍑芥暟瀵硅薄 +``` + +瀹炰緥鍖栫殑杩囩▼濡備笅锛 + +```cpp +int accumulate(vector::iterator first, vector::iterator last, int init, int SumPowersop) { + for(;, first != last; ++first) + init = op(init, *first); //op鐨勬垚鍛樺彉閲忔槸3 + return init +} +``` + +涓婅堪渚嬪瓙鍙互寰堝ソ鐨勮鏄庡嚱鏁板璞$殑閲嶈鎬с傚鏋滄垜浠笉閫傜敤鍑芥暟瀵硅薄锛屽憿涔堝浜庝笉鍚岀殑娆℃暟锛屾垜浠渶瑕佸啓涓嶅悓鐨勬眰鍜屽嚱鏁帮紝濡俙SumSquares`骞跺垎鍒姞浠ヨ皟鐢ㄣ傛垨鑰呭0鏄庡叏灞鍙橀噺锛屽浜庝笉鍚岀殑鍑芥暟锛屼慨鏀瑰叏灞鍙橀噺鐨勫煎熸杈惧埌涓嶅悓鍑芥暟鍔熻兘鐨勭洰鐨勶紝浣嗗叏灞鍙橀噺鐨勪娇鐢ㄨ繚鑳屼簡OOP鐨勬剰涔夈 + + + +### 鍑芥暟瀵硅薄绫绘ā鏉 + +- `equal_to` +- `greater` +- `less` +- ... + +澶存枃浠讹細`` + +#### `greater` 鐨勫簲鐢 + +鍘熷瀷 + +```cpp +template +struct greater:public binary_function//娲剧敓绫 +{ + bool operator()(const T &x, const T &y) const//閲嶈浇鍦嗘嫭鍙锋垚鍛樺嚱鏁 + { + return x > y;//濡傛灉x>y杩斿洖鍊间负True. + } +} +//娉ㄦ剰锛屽浜庝竴鑸殑瀹瑰櫒锛屽鏋滆繑鍥炲间负True锛屽垯浼氳涓篨 + + void sort(Compare op); + + 灏唋ist涓殑鍏冪礌鎸夌収op瑙勫畾鐨勬瘮杈冩柟娉曞崌搴忔帓鍒椼傚嵆瑕佹瘮杈儀,y澶у皬鏃讹紝鐪媜p(x,y)鐨勮繑鍥炲硷紝涓簍rue鍒欒涓簒灏忎簬y銆 + +```cpp +#include +#include +#include +using namespace std; + +class Myless +{ +public: + bool operator()(const int &c1, const int &c2) + { + return (c1 % 10) < (c2 % 10); //涓や釜鏁存暟锛岃皝鐨勪釜浣嶆暟灏忥紝璋佸氨灏 + } +}; + +template +void Print(T first, T last) +{ + for (; first != last; first++) + { + cout << *first << ","; + } + cout << endl; +} + +int main() +{ + const int SIZE = 5; + int a[SIZE] = { 5,21,14,2,3 }; + list lst(a, a + SIZE); + lst.sort(Myless()); // 涓綅鏁板皬鐨勬帓鍓嶉潰 + //缁欏嚭鍑芥暟瀵硅薄锛岀敤鏃犲弬鏋勯犲嚱鏁板垵濮嬪寲 + Print(lst.begin(), lst.end()); + lst.sort(greater()); // 浠庡ぇ鍒板皬鎺掑簭 + //greater鏄釜绫绘ā鏉 + //greater绫 + //greater()鐢熸垚涓涓复鏃跺嚱鏁板璞 + Print(lst.begin(), lst.end()); + return 0; +} +``` + +**`Note:`** + +## 鍦⊿TL涓娇鐢ㄨ嚜瀹氫箟鐨勨滃ぇ鈥濓紝鈥滃皬鈥濆叧绯 + +鍏宠仈瀹瑰櫒鍜宍STL`涓殑璁稿绠楁硶锛岄兘鏄彲浠ヤ娇鐢ㄥ嚱鏁版垨鑰呭嚱鏁板璞¤嚜瀹氫箟姣旇緝鍣ㄧ殑銆傚湪鑷畾涔変簡姣旇緝鍣╜op`鐨勬儏鍐典笅锛屼互涓嬩笁绉嶈娉曟槸绛変环鐨勶細 + +1. x灏忎簬y +2. $op$(x,y)鐨勮繑鍥炲间负True +3. y澶т簬x \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/README.md" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/README.md" index fb12d49..6b37bbf 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/README.md" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/README.md" @@ -2,7 +2,7 @@ ## `pair`妯℃澘 -```c++ +```cpp template struct pair { @@ -11,16 +11,27 @@ struct pair _T1 first; _T2 second; pair():first(), second(){}; + //鏃犲弬鏋勯犲嚱鏁 pair(const _T1 &__a, const _T2 &__b):first(__a), second(__b) {} template pair(const pair<_U1, _U2 &__p):first(__p.first), second(__p.second) {} + //鐢ㄥ嚱鏁版ā鏉胯繘琛屽垵濮嬪寲 }; ``` -- map/multimap 閲屾斁鐨勯兘鏄痐pari`妯℃澘绫荤殑瀵硅薄锛屼笖鎸塦first`浠庡皬鍒板ぇ鎺掑簭 -- 绗笁涓瀯閫犲嚱鏁扮敤娉曠ず渚嬶細`pair p(pair(5.5, 4.6))` +- map/multimap 閲屾斁鐨勯兘鏄痐pair`妯℃澘绫荤殑瀵硅薄锛屼笖鎸塦first`浠庡皬鍒板ぇ鎺掑簭 + +- 绗笁涓瀯閫犲嚱鏁扮敤娉曠ず渚嬶細 + + ```cpp + pairp(pair(5.5, 4.6))` + //p.first = 5 + //p.second = 4 + ``` + + ## 鍏宠仈瀹瑰櫒 @@ -33,44 +44,46 @@ struct pair 鍘熷瀷 -```c++ +```cpp template, class A = allocator > class multiset { ... }; +//key鍐冲畾浜嗗厓绱犵殑绫诲瀷 ``` -- `Pred`绫诲瀷鐨勫彉閲忓喅瀹氫簡`multiset` 涓殑鍏冪礌锛屸滀竴涓瘮鍙︿竴涓皬鈥濇槸鎬庝箞瀹氫箟鐨勩 `multiset`杩愯杩囩▼涓紝姣旇緝涓や釜鍏冪礌x,y鐨勫ぇ灏忕殑鍋氭硶锛屽氨鏄敓鎴愪竴涓 `Pred`绫诲瀷鐨 鍙橀噺锛屽亣瀹氫负 `op`,鑻ヨ〃杈惧紡`op(x,y)` 杩斿洖鍊间负true,鍒 x姣攜灏忋 Pred鐨勭己鐪佺被鍨嬫槸 `less`銆 +- `Pred`绫诲瀷鐨勫彉閲忓喅瀹氫簡`multiset` 涓殑鍏冪礌锛屸滀竴涓瘮鍙︿竴涓皬鈥濇槸鎬庝箞瀹氫箟鐨勩 `multiset`杩愯杩囩▼涓紝姣旇緝涓や釜鍏冪礌x,y鐨勫ぇ灏忕殑鍋氭硶锛屽氨鏄敓鎴愪竴涓 `Pred`绫诲瀷鐨勫彉閲忥紝鍋囧畾涓 `op`,鑻ヨ〃杈惧紡`op(x,y)` 杩斿洖鍊间负true,鍒 x姣攜灏忋 Pred鐨勭己鐪佺被鍨嬫槸 `less`銆俙op`鍙互鏄竴涓嚱鏁板悕瀛楋紝鍑芥暟鎸囬拡鎴栬呭嚱鏁板璞★紝澶у鏁版儏鍐典笅涓哄嚱鏁板璞°傚綋鍏朵负鍑芥暟瀵硅薄鏄紝`op(x,y)`鏄皟鐢╜op`杩欎釜瀵硅薄鐨刼perator()鎴愬憳鍑芥暟銆 > `less`妯℃澘鐨勫畾涔 > -> ```c++ +> ```cpp > template > struct less : public binary_function > { > bool operator()(const T &x, const T &y) const -> { -> return x < y; -> } -> }; +> { +> return x < y; +> } +> };//less妯℃澘鏄潬<鏉ユ瘮杈冨ぇ灏忕殑 > ``` -| 鎴愬憳鍑芥暟 | 浣滅敤 | -| ---------------------------------------- | ---------------------------------------- | -| `iterator find(const T &val)` | 鍦ㄥ鍣ㄤ腑鏌ユ壘鍊间负val鐨勫厓绱狅紝杩斿洖鍏惰凯浠e櫒銆傚鏋滄壘涓嶅埌锛岃繑鍥瀍nd()銆 | -| `iterator insert(const T & val);` | 灏唙al鎻掑叆鍒板鍣ㄤ腑骞惰繑鍥炲叾杩唬鍣ㄣ | -| `void insert( iterator first,iterator last);` | 灏嗗尯闂碵first,last)鎻掑叆瀹瑰櫒銆 | -| `iterator lower_bound(const T & val);` | 鏌ユ壘涓涓渶澶х殑浣嶇疆 it,浣垮緱[begin(),it) 涓墍鏈夌殑鍏冪礌閮芥瘮 val 灏忋 | -| `iterator upper_bound(const T & val);` | 鏌ユ壘涓涓渶灏忕殑浣嶇疆 it,浣垮緱[it,end()) 涓墍鏈夌殑鍏冪礌閮芥瘮 val 澶с | -| `pair equal_range(const T & val);` | 鍚屾椂姹傚緱lower_bound鍜寀pper_bound銆 | -| `iterator erase(iterator it);` | 鍒犻櫎it鎸囧悜鐨勫厓绱狅紝杩斿洖鍏跺悗闈㈢殑鍏冪礌鐨勮凯浠e櫒(Visual studio 2010涓婂姝わ紝浣嗘槸鍦 C++鏍囧噯鍜孌ev C++涓紝杩斿洖鍊间笉鏄繖鏍)銆 | +| 鎴愬憳鍑芥暟 | 浣滅敤 | +| ----------------------------------------------------- | :----------------------------------------------------------- | +| `iterator find(const T &val)` | 鍦ㄥ鍣ㄤ腑鏌ユ壘鍊间负val鐨勫厓绱狅紝杩斿洖鍏惰凯浠e櫒銆傚鏋滄壘涓嶅埌锛岃繑鍥瀍nd()銆傛敞鎰忔澶勭殑绛変簬骞朵笉鏄洿鎺ヤ娇鐢╜==`鑰屾槸`x equal_range(const T & val);` | 鍚屾椂姹傚緱`lower_bound`鍜宍upper_bound`銆傛敞鎰忚繑鍥炲紁air绫绘ā鏉跨殑 | +| `iterator erase(iterator it);` | 鍒犻櫎it鎸囧悜鐨勫厓绱狅紝杩斿洖鍏跺悗闈㈢殑鍏冪礌鐨勮凯浠e櫒(Visual studio 2010涓婂姝わ紝浣嗘槸鍦 C++鏍囧噯鍜孌ev C++涓紝杩斿洖鍊间笉鏄繖鏍)銆 | +| int count(const T &val) | 缁熻鏈夊灏戜釜鍏冪礌鐨勫煎拰val鐩哥瓑 | -鎻掑叆鍏冪礌鏃讹紝multiset浼氬皢琚彃鍏ュ厓绱犲拰宸叉湁鍏冪礌杩涜姣旇緝銆傜敱浜巐ess妯℃澘鏄敤 < 杩涜 姣旇緝鐨勶紝鎵浠,杩欓兘瑕佹眰瀵硅薄鑳界敤 < 姣 杈冿紝鍗抽傚綋閲嶈浇浜 < +鎻掑叆鍏冪礌鏃讹紝multiset浼氬皢琚彃鍏ュ厓绱犲拰宸叉湁鍏冪礌杩涜姣旇緝銆傜敱浜巐ess妯℃澘鏄敤 < 杩涜 姣旇緝鐨勶紝鎵浠,杩欓兘瑕佹眰瀵硅薄鑳界敤 < 姣 杈冿紝鍗抽傚綋閲嶈浇浜 <銆傛敞鎰忥紝`find` `insert` `upper_bound` `lower_bound`鐨勬椂闂村鏉傚害閮芥槸`log(n)`銆傝宍insert(鍖洪棿)`鐨勬椂闂村鏉傚害涓篳O(nlogn)` 搴旂敤绋嬪簭 -```c++ +```cpp #include #include using namespace std; @@ -93,7 +106,7 @@ public: A(int n_) :n(n_) {} friend bool operator<(const A &a1, const A &a2) { - return a1.n < a2.n; + return a1.n < a2.n; } friend ostream &operator<<(ostream &o, const A &a) { @@ -106,34 +119,38 @@ public: // 鑷畾涔夋瘮杈冨璞″嚱鏁 struct MyLess { - bool operator()(const A &a1, const A &a2) + bool operator()(const A &a1, const A &a2)//閲嶈浇鍦嗘嫭鍙 { return (a1.n % 10) < (a2.n % 10); + //鎸変釜浣嶆暟姣斿ぇ灏 } }; +typedef multiset MEST1; //MEST1鐢╜<`姣斿ぇ灏 +typedef multiset MEST2; //MEST2鐢∕yLess::operator()姣旇緝澶у皬 int main() { const int SIZE = 6; A a[SIZE] = { 4,22,19,8,33,40 }; - multiset m1; // 榛樿姣旇緝鍑芥暟 less + MEST1 m1; // 榛樿姣旇緝鍑芥暟 less m1.insert(a, a + SIZE); m1.insert(22); cout << "1)" << m1.count(22) << endl; cout << "2)"; Print(m1.begin(), m1.end()); - multiset::iterator i = m1.find(19); + MEST1::iterator i = m1.find(19); if (i != m1.end()) // true 琛ㄧず鎵惧埌 { cout << "found" << endl; } cout << "3)" << *m1.lower_bound(22) << "," << *m1.upper_bound(22) << endl; + //閫氳繃*鎶婅凯浠e櫒瀵瑰簲鐨勫厓绱 i = m1.erase(m1.lower_bound(22), m1.upper_bound(22)); // 杩斿洖琚垹闄ゅ厓绱犵殑涓嬩竴涓厓绱犵殑杩唬鍣 cout << "4)"; Print(m1.begin(), m1.end()); cout << "5)" << *i << endl; - multiset m2; // 鑷畾涔夋瘮杈冨嚱鏁 MyLess + MEST2 m2; // 鑷畾涔夋瘮杈冨嚱鏁 MyLess m2.insert(a, a + SIZE); cout << "6)"; Print(m2.begin(), m2.end()); @@ -166,14 +183,14 @@ int main() { typedef set::iterator IT; int a[5] = { 3,4,6,1,2 }; - set myset(a, a + 5); + set myset(a, a + 5);//st: 1 2 3 4 6 pair result; result = myset.insert(5); - if (result.second) + if (result.second) //鎻掓洸鎴愬姛鍒欒緭鍑鸿鎻掑叆鐨勫厓绱 { cout << *result.first << " inserted" << endl; } - if (myset.insert(5).second) + if (myset.insert(5).second) //鍐嶆灏濊瘯鎻掑叆5 { cout << *result.first << endl; } @@ -191,12 +208,13 @@ int main() ### `multimap` -- `multimap`涓殑鍏冪礌鐢 `<鍏抽敭瀛楋紝 鍊>`缁勬垚锛屾瘡涓厓绱犻兘鏄竴涓猔pair`瀵硅薄锛屽叧閿瓧灏辨槸`first`锛屽叾绫诲瀷涓篳Key` +- `multimap`涓殑鍏冪礌鐢 `<鍏抽敭瀛楋紝 鍊>`缁勬垚锛屾瘡涓厓绱犻兘鏄竴涓猔pair`妯℃澘绫荤殑**瀵硅薄**锛屽叧閿瓧灏辨槸`first`锛屽叾绫诲瀷涓篳Key` - `multimap` 涓厑璁稿涓厓绱犵殑鍏抽敭瀛楃浉鍚屻傚厓绱犳寜鐓first`鎴愬憳鍙橀噺浠庡皬鍒板ぇ 鎺掑垪锛岀己鐪佹儏鍐典笅鐢 `less` 瀹氫箟鍏抽敭瀛楃殑鈥滃皬浜庘濆叧绯汇 +- 鍏冪礌鐨刦irst鎴愬憳鍙橀噺涓嶈兘琚慨鏀 鍘熷瀷 -```c++ +```cpp template, class A = allocator > class multimap { @@ -208,7 +226,7 @@ class multimap 绀轰緥 -```c++ +```cpp #include #include using namespace std; @@ -217,9 +235,14 @@ int main() { typedef multimap > mmid; mmid pairs; - cout << "1)" << pairs.count(15) << endl; - pairs.insert(mmid::value_type(15, 2.7)); // pair + cout << "1)" << pairs.count(15) << endl;//鍏抽敭瀛楃瓑浜15.杈撳嚭0 + pairs.insert(mmid::value_type(15, 2.7)); + //mmid::value_type 鎶妅ey鏇挎崲鎴恑nt, T鏇挎崲鎴恉ouble,寰楀埌pair妯℃澘绫汇傚悗闈㈢殑鍙傛暟瀵瑰簲鏋勯犲嚱鏁帮紝鐢熸垚涓存椂鐨勫璞° + //typedef pairvalue_type; + //瑕佹彃鍏ョ殑鍏冪礌蹇呭畾鏄痯air妯℃澘绫荤殑瀵硅薄 + // pair pairs.insert(mmid::value_type(15, 99.3)); + //Multimap鍏佽鏈夊涓厓绱犵殑first鐩稿悓 cout << "2)" << pairs.count(15) << endl; pairs.insert(mmid::value_type(30, 111.11)); pairs.insert(mmid::value_type(10, 22.22)); @@ -234,12 +257,93 @@ int main() } ``` +鍏宠仈瀹瑰櫒寰堥傚悎鐢ㄤ簬闇瑕佷笉鏂湴鏇存柊鏁版嵁锛屼笉鏂湴鍦ㄦ暟鎹噷闈㈣繘琛屾煡璇㈢殑杩囩▼銆傚洜涓哄叾鍦ㄦ煡璇㈡垨鑰呭鍒犳椂锛屾椂闂寸殑澶嶆潅搴﹀潎涓篳O(log(n))`. + +```cpp +#include +#include +#include +using namespace std; +class CStudent +{ +public: + struct CInfo //绫荤殑鍐呴儴杩樺彲浠ュ畾涔夌被 + { + int id; + string name; + }; + int score; + CInfo info;//CInfo绫诲瀷鐨勫璞′綔涓烘垚鍛樺彉閲 +}; + +typedef multimap MAP_STD; +int main() { + MAP_STD mp; + CStudent st; + string cmd; + while (cin >> cmd) { + if (cmd == "Add") { + cin >> st.info.name >> st.info.id >> st.score; + mp.insert(MAP_STD::value_type(st.score, st.info));//pair妯℃澘绫荤殑瀵硅薄鏉ュ瓨鏀句俊鎭 + + } + else if(cmd == "Query") + { + int score; + cin >> score; + MAP_STD::iterator p = mp.lower_bound(score); + if (p != mp.begin()) {//鑻ヤ负begin锛岃鏄庢煡鎵句笉鍒 + --p; //杩斿洖鍊间负宸﹂棴鍙冲紑 + score = p->first;//姣旇鏌ヨ鍒嗘暟浣庣殑鏈楂樺垎 + MAP_STD::iterator maxp = p; + int maxid = p->second.id; + for (; p != mp.begin() && p->first == score; --p) { + if (p->second.id > maxid) { + maxp = p; + maxid = p->second.id; + } + } + if (p->first == score) { + //濡傛灉涓婇潰鐨勫惊鐜槸鍥犱负p==mp.begin()鑰屽仠姝紝鍒檖鎸囧悜鐨勫厓绱犱篃瑕佽澶勭悊} + //濡傛灉涓婇潰鐨勫惊鐜槸鍥犱负p==mp.begin()鑰屽仠姝紝鍒檖鎸囧悜鐨勫厓绱犱篃瑕佽澶勭悊 + if (p->second.id > maxid) { + maxp = p; + maxid = p->second.id; + } + } + cout << maxp->second.name << " " << maxp->second.id << " " << maxp->first << endl; + } + else + cout << "Nobodz" << endl; + } + } + return 0; +} +``` + +娉ㄦ剰锛 +```cpp +mp.insert(MAP_STD::value_type(st.score, st.info)); +//涔熷彲浠ュ啓浣滐細 +mp.insert(make_pair(st.score,st.info)); +``` + +`make_pair`杩斿洖鍊兼槸`pair`妯℃澘绫 ### `map` - 鍏抽敭瀛梎first`鍚勪笉鐩稿悓 -- `map[key]`杩斿洖瀵瑰叧閿瓧绛変簬`key`鐨勫厓绱犵殑鍊(`second`鎴愬憳鍙橀噺锛夌殑**寮曠敤**銆傝嫢娌℃湁鍏抽敭瀛椾负`key`鐨勫厓绱狅紝鍒欎細寰`map`閲屾彃鍏ヤ竴涓叧閿瓧涓篳key`鐨勫厓绱狅紝鍏跺肩敤鏃犲弬鏋勯犲嚱鏁板垵濮嬪寲锛屽苟杩斿洖鍏跺肩殑寮曠敤. + +- 鑻airs涓簃ap妯℃澘绫荤殑瀵硅薄锛宍pairs[key]`杩斿洖瀵瑰叧閿瓧绛変簬`key`鐨勫厓绱犵殑鍊(`second`鎴愬憳鍙橀噺锛夌殑**寮曠敤**銆傝嫢娌℃湁鍏抽敭瀛椾负`key`鐨勫厓绱狅紝鍒欎細寰`pairs`閲屾彃鍏ヤ竴涓叧閿瓧涓篳key`鐨勫厓绱狅紝鍏跺肩敤鏃犲弬鏋勯犲嚱鏁板垵濮嬪寲锛屽苟杩斿洖鍏跺肩殑寮曠敤. + + ```cpp + map pairs; + pairs[50] = 5; //浼氫慨鏀筽airs涓叧閿瓧涓50鐨勫厓绱狅紝浣垮叾鍊煎彉涓50 + //鑻ヤ笉瀛樺湪鍏抽敭瀛楃瓑浜50鐨勫厓绱狅紝鍒欐彃鍏ユ鍏冪礌锛屽苟浣垮叾鍊间负5 + ``` + + 绀轰緥 @@ -257,13 +361,13 @@ ostream& operator<<(ostream &o, const pair &p) int main() { - typedef map > mmid; + typedef map > mmid;//涓や釜>>涔嬮棿鏈濂藉啓涓涓┖鏍硷紝閬垮厤缂栬瘧鍣ㄦ棤娉曡瘑鍒 mmid pairs; cout << "1) " << pairs.count(15) << endl; pairs.insert(mmid::value_type(15, 2.7)); pairs.insert(make_pair(15, 99.3)); //make_pair鐢熸垚涓涓猵air瀵硅薄 cout << "2) " << pairs.count(15) << endl; - pairs.insert(mmid::value_type(20,9.3)); + pairs.insert(mmid::value_type(20,9.3));//濡備綍鍒ゅ畾insert鏄惁鎴愬姛锛屽彲浠ヨ嚜宸卞畾涔塸air mmid::iterator i; cout << "3) "; for (i = pairs.begin(); i != pairs.end(); i++) @@ -272,7 +376,8 @@ int main() } cout << endl; cout << "4) "; - int n = pairs[40];//濡傛灉娌℃湁鍏抽敭瀛椾负40鐨勫厓绱狅紝鍒欐彃鍏ヤ竴涓 + int n = pairs[40];//濡傛灉娌℃湁鍏抽敭瀛椾负40鐨勫厓绱狅紝鍒欐彃鍏ヤ竴涓. + //鍏朵腑绗簩涓厓绱犵敱鏃犲弬鏋勯犲嚱鏁拌繘琛屽垵濮嬪寲 for (i = pairs.begin(); i != pairs.end(); i++) { cout << *i << ","; @@ -294,10 +399,11 @@ int main() - 鍙互鐢ㄦ煇绉嶉『搴忓鍣ㄦ潵瀹炵幇 (璁╁凡鏈夌殑椤哄簭瀹瑰櫒浠ユ爤/闃熷垪鐨勬柟寮忓伐浣) - 瀹瑰櫒閫傞厤鍣ㄤ笂娌℃湁杩唬鍣細STL涓悇绉嶆帓搴, 鏌ユ壘, 鍙樺簭绛夌畻娉曢兘涓嶉傚悎瀹瑰櫒閫傞厤鍣 +- 閮芥湁涓変釜鎴愬憳鍑芥暟锛歱ush,top,pop ### `stack` -- `stack` 鏄悗杩涘厛鍑虹殑鏁版嵁缁撴瀯 +- `stack` 鏄**鍚庤繘鍏堝嚭**鐨勬暟鎹粨鏋 - 鍙兘鎻掑叆, 鍒犻櫎, 璁块棶鏍堥《鐨勫厓绱 - 鍙敤 `vector`, `list`, `deque`鏉ュ疄鐜 - 缂虹渷鎯呭喌涓, 鐢╜deque`瀹炵幇 @@ -313,11 +419,11 @@ class stack } ``` -| 鎴愬憳鍑芥暟 | 浣滅敤 | -| ------------------------ | ---------------------------------------- | -| `void push(const T &x);` | 灏唜鍘嬪叆鏍堥《 | -| `void pop();` | 寮瑰嚭(鍗冲垹闄)鏍堥《鍏冪礌 | -| `T& top();` | 杩斿洖鏍堥《鍏冪礌鐨勫紩鐢. 閫氳繃璇ュ嚱鏁, 鍙互璇诲彇鏍堥《 鍏冪礌鐨勫, 涔熷彲浠ヤ慨鏀规爤椤跺厓绱 | +| 鎴愬憳鍑芥暟 | 浣滅敤 | +| ------------------------ | ------------------------------------------------------------ | +| `void push(const T &x);` | 灏唜鍘嬪叆鏍堥《 | +| `void pop();` | 寮瑰嚭(鍗冲垹闄)鏍堥《鍏冪礌 | +| `T& top();` | 杩斿洖鏍堥《鍏冪礌鐨勫紩鐢. 閫氳繃璇ュ嚱鏁, 鍙互璇诲彇鏍堥《鍏冪礌鐨勫, 涔熷彲浠ヤ慨鏀规爤椤跺厓绱 | @@ -325,6 +431,7 @@ class stack - 鍜宍stack` 鍩烘湰绫讳技锛屽彲浠ョ敤 `list`鍜宍deque`瀹炵幇 - 缂虹渷鎯呭喌涓嬬敤`deque`瀹炵幇 +- 鍏堣繘鍏堝嚭 鍘熷瀷 @@ -336,10 +443,10 @@ class queue } ``` -| 鎴愬憳鍑芥暟 | 浣滅敤 | -| ------------------------ | ---------------------------------------- | -| `void push(const T &x);` | 灏唜鍔犲叆闃熷熬 | -| `void pop();` | 寮瑰嚭(鍗冲垹闄)闃熷ご鍏冪礌 | +| 鎴愬憳鍑芥暟 | 浣滅敤 | +| ------------------------ | ------------------------------------------------------------ | +| `void push(const T &x);` | 灏唜鍔犲叆闃熷熬 | +| `void pop();` | 寮瑰嚭(鍗冲垹闄)闃熷ご鍏冪礌 | | `T& top();` | 杩斿洖闃熷ご鍏冪礌鐨勫紩鐢. 閫氳繃璇ュ嚱鏁, 鍙互璇诲彇闃熷ご 鍏冪礌鐨勫, 涔熷彲浠ヤ慨鏀归槦澶村厓绱 | @@ -347,13 +454,22 @@ class queue ### `priority_queue` - 鍜 `queue`绫讳技, 鍙互鐢╜vector`鍜宍deque`瀹炵幇 + - 缂虹渷鎯呭喌涓嬬敤`vector`瀹炵幇 -- `priority_queue` 閫氬父鐢**鍫嗘帓搴**鎶鏈疄鐜, 淇濊瘉鏈澶х殑鍏冪礌鎬绘槸鍦ㄦ渶鍓嶉潰 锛堝叾浠栧厓绱犱笉涓瀹氭帓搴忥級 + +- `priority_queue` 閫氬父鐢**鍫嗘帓搴**鎶鏈疄鐜, 淇濊瘉鏈澶х殑鍏冪礌鎬绘槸鍦ㄦ渶鍓嶉潰 锛堝叾浠栧厓绱犱笉涓瀹氬畬鍏ㄦ湁搴忥級 - 鎵ц`pop`鎿嶄綔鏃, 鍒犻櫎鐨勬槸鏈澶х殑鍏冪礌 - 鎵ц`top`鎿嶄綔鏃, 杩斿洖鐨勬槸鏈澶у厓绱犵殑寮曠敤 -- 榛樿鐨勫厓绱犳瘮杈冨櫒鏄 `less` + +- 榛樿鐨勫厓绱犳瘮杈冨櫒鏄 `less`锛岄渶瑕佷繚璇佽兘浣跨敤`<`杩涜姣旇緝 + +- 鏈楂樹紭鍏堢骇鐨勫厓绱犳绘槸绗竴涓嚭鍒 + +- 閫傜敤浜庝笉鍋滃湴鍙栨渶澶у厓绱 + + @@ -378,36 +494,36 @@ STL涓殑绠楁硶澶ц嚧鍙互鍒嗕负浠ヤ笅涓冪被: - 璇ョ被绠楁硶涓嶄細淇敼绠楁硶鎵浣滅敤鐨勫鍣ㄦ垨瀵硅薄 - 閫傜敤浜庨『搴忓鍣ㄥ拰鍏宠仈瀹瑰櫒 -- 鏃堕棿澶嶆潅搴﹂兘鏄疧(n) - -| 绠楁硶 | 鍔熻兘 | -| ----------------------- | ---------------------------------------- | -| min | 姹備袱涓璞′腑杈冨皬鐨(鍙嚜瀹氫箟姣旇緝鍣) | -| max | 姹備袱涓璞′腑杈冨ぇ鐨(鍙嚜瀹氫箟姣旇緝鍣) | -| min_element | 姹傚尯闂翠腑鐨勬渶灏忓(鍙嚜瀹氫箟姣旇緝鍣) | -| max_element | 姹傚尯闂翠腑鐨勬渶澶у(鍙嚜瀹氫箟姣旇緝鍣) | -| for_each | 瀵瑰尯闂翠腑鐨勬瘡涓厓绱犻兘鍋氭煇绉嶆搷浣 | -| count | 璁$畻鍖洪棿涓瓑浜庢煇鍊肩殑鍏冪礌涓暟 | -| count_if | 璁$畻鍖洪棿涓鍚堟煇绉嶆潯浠剁殑鍏冪礌涓暟 | -| find | 鍦ㄥ尯闂翠腑鏌ユ壘绛変簬鏌愬肩殑鍏冪礌 | -| find_if | 鍦ㄥ尯闂翠腑鏌ユ壘绗﹀悎鏌愭潯浠剁殑鍏冪礌 | -| find_end | 鍦ㄥ尯闂翠腑鏌ユ壘鍙︿竴涓尯闂存渶鍚庝竴娆″嚭鐜扮殑浣 缃(鍙嚜瀹氫箟姣旇緝鍣) | -| find_first_of | 鍦ㄥ尯闂翠腑鏌ユ壘绗竴涓嚭鐜板湪鍙︿竴涓尯闂翠腑鐨 鍏冪礌 (鍙嚜瀹氫箟姣旇緝鍣) | -| adjacent_find | 鍦ㄥ尯闂翠腑瀵绘壘绗竴娆″嚭鐜拌繛缁袱涓浉绛夊厓绱 鐨勪綅缃(鍙嚜瀹氫箟姣旇緝鍣) | -| search | 鍦ㄥ尯闂翠腑鏌ユ壘鍙︿竴涓尯闂寸涓娆″嚭鐜扮殑浣嶇疆(鍙 鑷畾涔夋瘮杈冨櫒) | -| search_n | 鍦ㄥ尯闂翠腑鏌ユ壘绗竴娆″嚭鐜扮瓑浜庢煇鍊肩殑杩炵画n涓厓 绱(鍙嚜瀹氫箟姣旇緝鍣) | -| equal | 鍒ゆ柇涓ゅ尯闂存槸鍚︾浉绛(鍙嚜瀹氫箟姣旇緝鍣) | +- 鏃堕棿澶嶆潅搴﹂兘鏄疧(n)锛屽洜涓哄線寰浼氶亶鍘嗗厓绱 + +| 绠楁硶 | 鍔熻兘 | +| ----------------------- | ------------------------------------------------------------ | +| min | 姹備袱涓璞′腑杈冨皬鐨(鍙嚜瀹氫箟姣旇緝鍣) | +| max | 姹備袱涓璞′腑杈冨ぇ鐨(鍙嚜瀹氫箟姣旇緝鍣) | +| min_element | 姹傚尯闂翠腑鐨勬渶灏忓(鍙嚜瀹氫箟姣旇緝鍣) | +| max_element | 姹傚尯闂翠腑鐨勬渶澶у(鍙嚜瀹氫箟姣旇緝鍣) | +| for_each | 瀵瑰尯闂翠腑鐨勬瘡涓厓绱犻兘鍋氭煇绉嶆搷浣 | +| count | 璁$畻鍖洪棿涓瓑浜庢煇鍊肩殑鍏冪礌涓暟`==` | +| count_if | 璁$畻鍖洪棿涓鍚堟煇绉嶆潯浠剁殑鍏冪礌涓暟 | +| **find** | 鍦ㄥ尯闂翠腑鏌ユ壘绛変簬鏌愬肩殑鍏冪礌`==` | +| find_if | 鍦ㄥ尯闂翠腑鏌ユ壘绗﹀悎鏌愭潯浠剁殑鍏冪礌 | +| find_end | 鍦ㄥ尯闂翠腑鏌ユ壘鍙︿竴涓尯闂存渶鍚庝竴娆″嚭鐜扮殑浣 缃(鍙嚜瀹氫箟姣旇緝鍣) | +| find_first_of | 鍦ㄥ尯闂翠腑鏌ユ壘绗竴涓嚭鐜板湪鍙︿竴涓尯闂翠腑鐨 鍏冪礌 (鍙嚜瀹氫箟姣旇緝鍣) | +| adjacent_find | 鍦ㄥ尯闂翠腑瀵绘壘绗竴娆″嚭鐜拌繛缁袱涓浉绛夊厓绱 鐨勪綅缃(鍙嚜瀹氫箟姣旇緝鍣) | +| search | 鍦ㄥ尯闂翠腑鏌ユ壘鍙︿竴涓尯闂寸涓娆″嚭鐜扮殑浣嶇疆(鍙 鑷畾涔夋瘮杈冨櫒) | +| search_n | 鍦ㄥ尯闂翠腑鏌ユ壘绗竴娆″嚭鐜扮瓑浜庢煇鍊肩殑杩炵画n涓厓 绱(鍙嚜瀹氫箟姣旇緝鍣) | +| equal | 鍒ゆ柇涓ゅ尯闂存槸鍚︾浉绛(鍙嚜瀹氫箟姣旇緝鍣) | | mismatch | 閫愪釜姣旇緝涓や釜鍖洪棿鐨勫厓绱狅紝杩斿洖绗竴娆″彂鐢熶笉 鐩哥瓑鐨勪袱涓厓绱犵殑浣嶇疆(鍙嚜瀹氫箟姣旇緝鍣) | | lexicographical_compare | 鎸夊瓧鍏稿簭姣旇緝涓や釜鍖洪棿鐨勫ぇ灏(鍙嚜瀹氫箟姣旇緝鍣) | #### find -```c++ +```cpp template InIt find(InIt first, InIt last, const T &val); ``` -- 杩斿洖鍖洪棿 [first,last) 涓殑杩唬鍣╜i`锛屼娇寰 `*i == val` +- 杩斿洖鍖洪棿 [first,last) 涓殑杩唬鍣╜i`锛屼娇寰 `*i == val`銆傚鏋滄病鎵惧埌锛岃繑鍥瀈last` #### find_if @@ -465,29 +581,58 @@ FwdIt max_element(FwdIt first, FwdIt last); - 杩斿洖[first,last) 涓渶澶у厓绱(涓嶅皬浜庝换浣曞叾浠栧厓绱)鐨勮凯浠e櫒 - 浠 `<` 浣滄瘮杈冨櫒 +#### 鍏充簬<鐨勪緥瀛 +```cpp +#include +#include +using namespace std; +class A { +public: + int n; + A(int i) : n(i) {} +}; +bool operator<(const A &a1, const A &a2) { + cout << "n << endl; + cout << max_element(aa, aa + 5)->n << endl; + return 0; +} +``` + +鍏惰緭鍑虹粨鏋滃涓嬶細 + +![ZNludJ.png](https://s2.ax1x.com/2019/07/04/ZNludJ.png) +鎴戜滑娉ㄦ剰鍒板叾min_element鐨勮緭鍑轰负3銆傚叾宸ヤ綔娴佺▼濡備笅锛屾垜浠渶寮濮嬪亣璁3涓烘渶灏忓硷紝涔嬪悗杩涜渚濇閬嶅巻姣旇緝锛5<3锛熸牴鎹垜浠噸杞界殑姣斿ぇ灏忚〃杈惧紡锛屾樉鐒朵笉涓虹湡锛3淇濇寔涓烘渶灏忓硷紝骞朵緷娆$被鎺:7<3,etc. +鑰宮ax_element鐨勬瘮杈冮『搴忎负锛屽亣瀹3鏈澶э紝鐒跺悗姣旇緝3<5... ### 鍙樺肩畻娉 - 姝ょ被绠楁硶浼氫慨鏀规簮鍖洪棿鎴栫洰鏍囧尯闂村厓绱犵殑鍊 - 鍊艰淇敼鐨勯偅涓尯闂, 涓嶅彲浠ユ槸灞炰簬鍏宠仈瀹瑰櫒鐨勶紙鍥犱负鍏宠仈瀹瑰櫒鏄湁搴忕殑锛 -| 绠楁硶 | 鍔熻兘 | -| --------------- | --------------------------------- | -| for_each | 瀵瑰尯闂翠腑鐨勬瘡涓厓绱犻兘鍋氭煇绉嶆搷浣 | -| copy | 澶嶅埗涓涓尯闂村埌鍒 | -| copy_backward | 澶嶅埗涓涓尯闂村埌鍒, 浣嗙洰鏍囧尯鍓嶆槸浠庡悗寰鍓 琚慨鏀圭殑 | -| transform | 灏嗕竴涓尯闂寸殑鍏冪礌鍙樺舰鍚庢嫹璐濆埌鍙︿竴涓尯闂 | -| swap_ranges | 浜ゆ崲涓や釜鍖洪棿鍐呭 | -| fill | 鐢ㄦ煇涓煎~鍏呭尯闂 | -| fill_n | 鐢ㄦ煇涓兼浛鎹㈠尯闂翠腑鐨刵涓厓绱 | -| generate | 鐢ㄦ煇涓搷浣滅殑缁撴灉濉厖鍖洪棿 | -| generate_n | 鐢ㄦ煇涓搷浣滅殑缁撴灉鏇挎崲鍖洪棿涓殑n涓厓绱 | -| replace | 灏嗗尯闂翠腑鐨勬煇涓兼浛鎹负鍙︿竴涓 | -| replace_if | 灏嗗尯闂翠腑绗﹀悎鏌愮鏉′欢鐨勫兼浛鎹㈡垚鍙︿竴涓 | -| replace_copy | 灏嗕竴涓尯闂存嫹璐濆埌鍙︿竴涓尯闂达紝鎷疯礉鏃舵煇涓 瑕佹崲鎴愭柊鍊兼嫹杩囧幓 | +| 绠楁硶 | 鍔熻兘 | +| --------------- | ------------------------------------------------------------ | +| for_each | 瀵瑰尯闂翠腑鐨勬瘡涓厓绱犻兘鍋氭煇绉嶆搷浣 | +| **copy** | 澶嶅埗涓涓尯闂村埌鍒 | +| copy_backward | 澶嶅埗涓涓尯闂村埌鍒, 浣嗙洰鏍囧尯鍓嶆槸浠庡悗寰鍓嶈淇敼鐨 | +| transform | 灏嗕竴涓尯闂寸殑鍏冪礌鍙樺舰鍚庢嫹璐濆埌鍙︿竴涓尯闂 | +| swap_ranges | 浜ゆ崲涓や釜鍖洪棿鍐呭 | +| fill | 鐢ㄦ煇涓煎~鍏呭尯闂 | +| fill_n | 鐢ㄦ煇涓兼浛鎹㈠尯闂翠腑鐨刵涓厓绱 | +| generate | 鐢ㄦ煇涓搷浣滅殑缁撴灉濉厖鍖洪棿 | +| generate_n | 鐢ㄦ煇涓搷浣滅殑缁撴灉鏇挎崲鍖洪棿涓殑n涓厓绱 | +| replace | 灏嗗尯闂翠腑鐨勬煇涓兼浛鎹负鍙︿竴涓 | +| replace_if | 灏嗗尯闂翠腑绗﹀悎鏌愮鏉′欢鐨勫兼浛鎹㈡垚鍙︿竴涓 | +| replace_copy | 灏嗕竴涓尯闂存嫹璐濆埌鍙︿竴涓尯闂达紝鎷疯礉鏃舵煇涓 瑕佹崲鎴愭柊鍊兼嫹杩囧幓 | | replace_copy_if | 灏嗕竴涓尯闂存嫹璐濆埌鍙︿竴涓尯闂达紝鎷疯礉鏃剁鍚堟煇 鏉′欢鐨勫艰鎹㈡垚鏂板兼嫹杩囧幓 | #### transform @@ -505,6 +650,64 @@ OutIt transform(InIt first, InIt last, OutIt x, Unop uop); #### copy +```cpp +#include +#include +#include +#include +#include +#include +using namespace std; +class CLessThen9 { +public: + bool operator() (int n) { return n < 9; } +}; +void outputSquare(int value) { cout << value * value; } +int calculateCube(int value) { return value * value*value; } +int main() { + const int SIZE = 10; + int a1[] = { 1,2,3,4,5,6,7,8,9,10 }; + int a2[] = { 100,2,8,1,50,3,8,0,10,2 }; + vectorv(a1, a1 + SIZE); + ostream_iteratoroutput(cout, " ");//涔嬪悗璋冪敤output鏃讹紝鐩稿綋浜庤皟鐢╟out锛 + //骞朵笖鎸囧嚭杈撳嚭鐨勯兘鏄痠nt绫诲瀷锛岃屼笖姣忚緭鍑轰竴涓暣鏁帮紝鍚庨潰閮戒細鍔犱竴涓┖鏍笺 + random_shuffle(v.begin(), v.end());//鍙樺簭绠楁硶锛岄殢鏈烘墦涔变竴涓暟缁 + cout << endl << "1)"; + copy(v.begin(), v.end(), output);//閫氳繃copy杩涜杈撳嚭 + copy(a2, a2 + SIZE, v.begin());//閫氳繃copy杩涜copy锛屾敞鎰忕洰鏍囧尯闂撮渶瑕佹湁瓒冲鐨勭┖闂 + cout << endl << "2)"; + cout << count(v.begin(), v.end(), 8);//鏁8鍑虹幇鐨勪釜鏁 + cout << endl << "3)"; + cout << count_if(v.begin(), v.end(), CLessThen9());//CLessThen9鏄被鐨勫悕瀛楋紝鍔犱笂()鍚庢槸瀵硅薄鐨勫悕瀛 + //cout_if鍦ㄥ伐浣滄椂锛岃冨療op(e)鏄惁鏄痶rue锛岃繖閲宱p()灏辨槸CLessThen9()锛宔鏄瘡涓涓厓绱犮傚鏋渙p(e)=true锛岋紝鍗 + //op.operator(e) 鍒檆ount_if宸ヤ綔,鍦ㄨ繖閲屾槸缁熺骇灏忎簬9鐨勪釜鏁 + cout << endl << "4)"; + cout << *(min_element(v.begin(), v.end())); + cout << endl << "5)"; + cout << *(max_element(v.begin(), v.end())); + cout << endl << "6)"; + cout << accumulate(v.begin(), v.end(), 0);//姹傚拰 + cout << endl << "7)"; + for_each(v.begin(), v.end(), outputSquare); + vectorcubes(SIZE); + transform(a1, a1 + SIZE, cubes.begin(), calculateCube); + cout << endl << "8)"; + copy(cubes.begin(), cubes.end(), output); + return 0; +} +``` + +[![ZNlJsO.png](https://s2.ax1x.com/2019/07/04/ZNlJsO.png)](https://imgchr.com/i/ZNlJsO) + +娉ㄦ剰鍒颁笂闈唬鐮佸畾涔変簡濡備笅鐨勭壒娈婄敤娉曪細 + +```cpp +ostream_iteratoroutput(cout," "); +//瀹氫箟浜嗕竴涓猳stream_iterator瀵硅薄锛屽彲浠ラ氳繃cout杈撳嚭浠" "绌烘牸鍒嗛殧鐨勪竴涓釜鏁存暟 +copy(v.begin(),v.end(),output); +//浣縱鐨勫唴瀹瑰湪cout涓婅緭鍑 +``` + 鍘熷瀷 ```c++ @@ -516,8 +719,8 @@ OutIt copy(InIt first, InIt last, OutIt x); 婧愪唬鐮侊紙绠鍖栵級 -```c++ -template +```cpp +template //杈撳叆/杈撳嚭杩唬鍣 inline _OI copy(_II _F, _II _L, _OI _X) { for (; _F != _L; ++_X, ++_F) @@ -528,7 +731,7 @@ inline _OI copy(_II _F, _II _L, _OI _X) ##### copy鐨勭壒娈婄敤娉 -```c++ +```cpp #include #include #include @@ -541,11 +744,11 @@ template class My_ostream_iterator :public iterator { private: - string sep; // 鍒嗛殧绗 - ostream &os; // 鏈変簺ostream瀵硅薄鐨勬瀯閫犲嚱鏁版槸鏈夊弬鐨勶紝鎵浠ョ敤寮曠敤姣旇緝鏂逛究 + string sep; // 鍒嗛殧绗︼紝鍗* + ostream &os; // 鏈変簺ostream瀵硅薄鐨勬棤鍙傛瀯閫犲嚱鏁版槸绉佹湁鐨 public: My_ostream_iterator(ostream &o, string s) :sep(s), os(o) {} - void operator++() {} + void operator++() {};//++鍙渶瑕佹湁瀹氫箟鍗冲彲 My_ostream_iterator& operator*() { return *this; @@ -560,7 +763,7 @@ public: int main() { int a[4] = { 1,2,3,4 }; - My_ostream_iterator oit(cout, "*"); + My_ostream_iterator oit(cout, "*");//杈撳嚭鐨勫唴瀹瑰嚭鐜板湪cout閲岄潰 copy(a, a + 4, oit); //杈撳嚭 1*2*3*4* ofstream oFile("text.txt", ios::out); My_ostream_iterator oitf(oFile, "*"); @@ -571,70 +774,20 @@ int main() ``` -#### 鍙樺肩畻娉曠ず渚 +涓婇潰绋嬪簭璋冪敤鈥渃opy(a,a+4,oit)鈥濆疄渚嬪寲鍚庡緱鍒癱opy濡備笅锛 -```c++ -#include -#include -#include -#include -#include -#include -using namespace std; - -class LessThen9 -{ -public: - bool operator()(int n) - { - return n < 9; - } -}; - -void ouputSquare(int value) -{ - cout << value * value << " "; -} - -int calculateCube(int value) -{ - return value * value * value; -} - -int main() -{ - const int SIZE = 10; - int a1[] = { 1,2,3,4,5,6,7,8,9,10 }; - int a2[] = { 100,2,8,1,50,3,8,9,10,2 }; - vector v(a1, a1 + SIZE); - ostream_iterator output(cout, " "); - random_shuffle(v.begin(), v.end()); // 闅忔満鍙樻崲椤哄簭锛堜吉闅忔満锛 - cout << endl << "1)"; - copy(v.begin(), v.end(), output); // 鐩稿綋浜庡仛涓涓茶緭鍑 - copy(a2, a2 + SIZE, v.begin()); - cout << endl << "2)"; - cout << count(v.begin(), v.end(), 8); - cout << endl << "3)"; - cout << count_if(v.begin(), v.end(), LessThen9()); - cout << endl << "4)"; - cout << *(min_element(v.begin(), v.end())); - cout << endl << "5)"; - cout << *(max_element(v.begin(), v.end())); - cout << endl << "6)"; - cout << accumulate(v.begin(), v.end(), 0); // 姹傚拰 - cout << endl << "7)"; - for_each(v.begin(), v.end(), ouputSquare); - vector cubes(SIZE); - transform(a1, a1 + SIZE, cubes.begin(), calculateCube); - cout << endl << "8)"; - copy(cubes.begin(), cubes.end(), output); - return 0; +```cpp +My_ostream_itreatorcopy(int *_F, int *_L, My_ostream_iterator _X){ + for(;_F!=_L;++_X;++_F)//鍥犱负闇瑕侀噸杞++ + *_X=*_F;//閲嶈浇* 鍜 = + //鍙互閫氳繃閲嶈浇锛屼娇寰梍F寰楀埌杈撳嚭 + //*閲嶈浇涔嬪悗锛屽彲浠ヨ涓烘槸涓涓嚱鏁帮紝鑰屼竴涓嚱鏁拌皟鐢ㄥ彲浠ュ嚭鐜板湪=宸﹁竟锛屽垯璇存槑鍏惰繑鍥炲兼槸涓涓紩鐢紱 + //寮曠敤x鑷繁銆傛鏃跺彲浠ョ浉褰撲簬x.operator=(*F) + return (_X); } +//娉ㄦ剰a鏄痠nt *绫诲瀷 ``` -- `ostream_iterator output(cout ,鈥 鈥); `瀹氫箟浜嗕竴涓 `ostream_iterator` 瀵硅薄, 鍙互閫氳繃`cout`杈撳嚭浠 鈥 鈥(绌烘牸) 鍒嗛殧鐨勪竴涓釜鏁存暟 -- `copy (v.begin(), v.end(), output);` 瀵艰嚧v鐨勫唴瀹瑰湪 `cout`涓婅緭鍑 - ### 鍒犻櫎绠楁硶 @@ -643,22 +796,41 @@ int main() - **鍒犻櫎涓嶄細浣垮鍣ㄩ噷鐨勫厓绱犲噺灏** - 灏嗘墍鏈夊簲璇ヨ鍒犻櫎鐨勫厓绱犵湅鍋氱┖浣嶅瓙 - 鐢ㄧ暀涓嬬殑鍏冪礌浠庡悗寰鍓嶇Щ, 渚濇鍘诲~绌轰綅瀛 - - 鍏冪礌寰鍓嶇Щ鍚, 瀹冨師鏉ョ殑浣嶇疆涔熷氨绠楁槸绌轰綅瀛 - - 涔熷簲鐢卞悗闈㈢殑鐣欎笅鐨勫厓绱犳潵濉笂 + - 鍏冪礌寰鍓嶇Щ鍚, 瀹冨師鏉ョ殑浣嶇疆涔熷氨绠楁槸绌轰綅瀛愪篃搴旂敱鍚庨潰鐨勭暀涓嬬殑鍏冪礌鏉ュ~涓 - 鏈鍚, 娌℃湁琚~涓婄殑绌轰綅瀛, 缁存寔鍏跺師鏉ョ殑鍊间笉鍙 - 鍒犻櫎绠楁硶涓嶅簲浣滅敤浜庡叧鑱斿鍣 -| 绠楁硶 | 鍔熻兘 | -| -------------- | ---------------------------------------- | -| remove | 鍒犻櫎鍖洪棿涓瓑浜庢煇涓肩殑鍏冪礌 | -| remove_if | 鍒犻櫎鍖洪棿涓弧瓒虫煇绉嶆潯浠剁殑鍏冪礌 | -| remove_copy | 鎷疯礉鍖洪棿鍒板彟涓涓尯闂. 绛変簬鏌愪釜鍊肩殑鍏冪礌涓嶆嫹璐 | -| remove_copy_if | 鎷疯礉鍖洪棿鍒板彟涓涓尯闂. 绗﹀悎鏌愮鏉′欢鐨勫厓绱犱笉鎷疯礉 | -| unique | 鍒犻櫎鍖洪棿涓繛缁浉绛夌殑鍏冪礌, 鍙暀涓嬩竴涓(鍙嚜瀹氫箟姣旇緝鍣) | +| 绠楁硶 | 鍔熻兘 | +| -------------- | ------------------------------------------------------------ | +| remove | 鍒犻櫎鍖洪棿涓瓑浜庢煇涓肩殑鍏冪礌 | +| remove_if | 鍒犻櫎鍖洪棿涓弧瓒虫煇绉嶆潯浠剁殑鍏冪礌 | +| remove_copy | 鎷疯礉鍖洪棿鍒板彟涓涓尯闂. 绛変簬鏌愪釜鍊肩殑鍏冪礌涓嶆嫹璐 | +| remove_copy_if | 鎷疯礉鍖洪棿鍒板彟涓涓尯闂. 绗﹀悎鏌愮鏉′欢鐨勫厓绱犱笉鎷疯礉 | +| unique | 鍒犻櫎鍖洪棿涓繛缁浉绛夌殑鍏冪礌, 鍙暀涓嬩竴涓(鍙嚜瀹氫箟姣旇緝鍣) | | unique_copy | 鎷疯礉鍖洪棿鍒板彟涓涓尯闂. 杩炵画鐩哥瓑鐨勫厓绱, 鍙嫹璐濈涓涓埌鐩 鏍囧尯闂 (鍙嚜瀹氫箟姣旇緝鍣) | - 澶嶆潅搴﹂兘鏄 $O(n)$ +```cpp +int main() { + int a[5] = { 1,2,3,2,5 }; + int b[6] = { 1,2,3,2,5,6 }; + ostream_iterator oit(cout, ","); + int * p = remove(a, a + 5, 2); + cout << "1) "; copy(a, a + 5, oit); cout << endl; //杈撳嚭 1) 1,3,5,2,5, + cout << "2) " << p - a << endl; //杈撳嚭 2) 3 + vector v(b, b + 6); + remove(v.begin(), v.end(), 2); + cout << "3) "; copy(v.begin(), v.end(), oit); cout << endl; + //杈撳嚭 3) 1,3,5,6,5,6, + cout << "4) "; cout << v.size() << endl; + //v涓殑鍏冪礌娌℃湁鍑忓皯,杈撳嚭 4) 6 + return 0; +} +``` + + + #### unique 鍘熷瀷1 @@ -687,19 +859,19 @@ FwdIt unique(FwdIt first, FwdIt last, Pred pr); - 鍙樺簭绠楁硶鏀瑰彉瀹瑰櫒涓厓绱犵殑椤哄簭 - 浣嗘槸涓嶆敼鍙樺厓绱犵殑鍊 -- 鍙樺簭绠楁硶涓嶉傜敤浜庡叧鑱斿鍣 -- 绠楁硶澶嶆潅搴﹂兘鏄 $O(n)$鐨 - -| 绠楁硶 | 鍔熻兘 | -| ---------------- | ---------------------------------- | -| reverse | 棰犲掑尯闂寸殑鍓嶅悗娆″簭 | -| reverse_copy | 鎶婁竴涓尯闂撮鍊掑悗鐨勭粨鏋滄嫹璐濆埌鍙︿竴涓尯闂达紝 婧愬尯闂翠笉鍙 | -| rotate | 灏嗗尯闂磋繘琛屽惊鐜乏绉 | -| rotate_copy | 灏嗗尯闂翠互棣栧熬鐩告帴鐨勫舰寮忚繘琛屾棆杞悗鐨勭粨鏋 鎷疯礉鍒板彟涓涓尯闂达紝婧愬尯闂翠笉鍙 | -| next_permutation | 灏嗗尯闂存敼涓轰笅涓涓帓鍒(鍙嚜瀹氫箟姣旇緝鍣) | -| prev_permutation | 灏嗗尯闂存敼涓轰笂涓涓帓鍒(鍙嚜瀹氫箟姣旇緝鍣) | -| random_shuffle | 闅忔満鎵撲贡鍖洪棿鍐呭厓绱犵殑椤哄簭 | -| partition | 鎶婂尯闂村唴婊¤冻鏌愪釜鏉′欢鐨勫厓绱犵Щ鍒板墠闈紝涓嶆弧瓒宠 鏉′欢鐨勭Щ鍒板悗闈 | +- 鍙樺簭绠楁硶涓嶉傜敤浜**鍏宠仈瀹瑰櫒** +- 绠楁硶澶嶆潅搴﹀ぇ閮ㄥ垎閮芥槸 $O(n)$鐨 + +| 绠楁硶 | 鍔熻兘 | +| -------------------- | ------------------------------------------------------------ | +| reverse | 棰犲掑尯闂寸殑鍓嶅悗娆″簭 | +| reverse_copy | 鎶婁竴涓尯闂撮鍊掑悗鐨勭粨鏋滄嫹璐濆埌鍙︿竴涓尯闂达紝 婧愬尯闂翠笉鍙 | +| rotate | 灏嗗尯闂磋繘琛屽惊鐜乏绉 | +| rotate_copy | 灏嗗尯闂翠互棣栧熬鐩告帴鐨勫舰寮忚繘琛屾棆杞悗鐨勭粨鏋 鎷疯礉鍒板彟涓涓尯闂达紝婧愬尯闂翠笉鍙 | +| **next_permutation** | 灏嗗尯闂存敼涓轰笅涓涓帓鍒(鍙嚜瀹氫箟姣旇緝鍣) | +| **prev_permutation** | 灏嗗尯闂存敼涓轰笂涓涓帓鍒(鍙嚜瀹氫箟姣旇緝鍣) | +| **random_shuffle** | 闅忔満鎵撲贡鍖洪棿鍐呭厓绱犵殑椤哄簭 | +| partition | 鎶婂尯闂村唴婊¤冻鏌愪釜鏉′欢鐨勫厓绱犵Щ鍒板墠闈紝涓嶆弧瓒宠 鏉′欢鐨勭Щ鍒板悗闈 | #### stable_partition @@ -742,7 +914,7 @@ bool next_permutaion (Init first,Init last); 绀轰緥 -```c++ +```cpp #include #include #include @@ -752,9 +924,9 @@ int main() { string str = "231"; char szStr[] = "324"; - while (next_permutation(str.begin(), str.end())) + while (next_permutation(str.begin(), str.end()))//鍙傛暟涓鸿捣鐐癸紝缁堢偣 { - cout << str << endl; + cout << str << endl;//312锛321 } cout << "****" << endl; while (next_permutation(szStr, szStr + 3)) @@ -771,24 +943,41 @@ int main() } ``` +`next_permutation`涓嶅崟鍗曞彲浠ョ敤鍦ㄦ暟瀛椾笂锛屼篃鍙互鐢ㄥ湪鑷畾涔夌殑鏁版嵁绫诲瀷涓娿 + +```cpp +using namespace std; +int main() { + int a[] = {8,7,10}; + listls(a, a + 3); + while (next_permutation(ls.begin(), ls.end())) { + list::iterator i; + for (i + ls.begin; i != ls.end(); i++) { + cout << *i << " "; + cout << endl; + } + } +} +``` + ### 鎺掑簭绠楁硶 - 姣斿墠闈㈢殑鍙樺簭绠楁硶澶嶆潅搴︽洿楂, 涓鑸槸 $O(n\log(n))$ - 鎺掑簭绠楁硶闇瑕侀殢鏈鸿闂凯浠e櫒鐨勬敮鎸 -- 涓嶉傜敤浜庡叧鑱斿鍣ㄥ拰list - -| 绠楁硶 | 鍔熻兘 | -| ----------------- | ---------------------------------------- | -| sort | 灏嗗尯闂翠粠灏忓埌澶ф帓搴(鍙嚜瀹氫箟姣旇緝鍣) | -| stable_sort | 灏嗗尯闂翠粠灏忓埌澶ф帓搴, 骞朵繚鎸佺浉绛夊厓绱犻棿鐨勭浉瀵规搴(鍙嚜瀹氫箟姣旇緝鍣) | -| partial_sort | 瀵瑰尯闂撮儴鍒嗘帓搴, 鐩村埌鏈灏忕殑n涓厓绱犲氨浣(鍙嚜瀹氫箟姣旇緝鍣) | -| partial_sort_copy | 灏嗗尯闂村墠n涓厓绱犵殑鎺掑簭缁撴灉鎷疯礉鍒板埆澶, 婧愬尯闂翠笉鍙(鍙嚜瀹氫箟姣旇緝鍣) | +- 涓嶉傜敤浜庡叧鑱斿鍣ㄥ拰`list`(`list`鏄弻鍚戣凯浠e櫒,瑕佷娇鐢╜list::sort`) + +| 绠楁硶 | 鍔熻兘 | +| ----------------- | ------------------------------------------------------------ | +| sort | 灏嗗尯闂翠粠灏忓埌澶ф帓搴(鍙嚜瀹氫箟姣旇緝鍣) | +| stable_sort | 灏嗗尯闂翠粠灏忓埌澶ф帓搴, 骞朵繚鎸佺浉绛夊厓绱犻棿鐨勭浉瀵规搴(鍙嚜瀹氫箟姣旇緝鍣) | +| partial_sort | 瀵瑰尯闂撮儴鍒嗘帓搴, 鐩村埌鏈灏忕殑n涓厓绱犲氨浣(鍙嚜瀹氫箟姣旇緝鍣) | +| partial_sort_copy | 灏嗗尯闂村墠n涓厓绱犵殑鎺掑簭缁撴灉鎷疯礉鍒板埆澶, 婧愬尯闂翠笉鍙(鍙嚜瀹氫箟姣旇緝鍣) | | nth_element | 瀵瑰尯闂撮儴鍒嗘帓搴, 浣垮緱绗琻灏忕殑鍏冪礌(n浠0寮濮嬬畻)灏变綅, 鑰屼笖姣 瀹冨皬鐨勯兘鍦ㄥ畠鍓嶉潰, 姣斿畠澶х殑閮藉湪瀹冨悗闈(鍙嚜瀹氫箟姣旇緝鍣) | -| make_heap | 浣垮尯闂存垚涓轰竴涓滃爢鈥(鍙嚜瀹氫箟姣旇緝鍣) | -| push_heap | 灏嗗厓绱犲姞鍏ヤ竴涓槸鈥滃爢鈥濆尯闂(鍙嚜瀹氫箟姣旇緝鍣) | -| pop_heap | 浠庘滃爢鈥濆尯闂村垹闄ゅ爢椤跺厓绱(鍙嚜瀹氫箟姣旇緝鍣) | +| make_heap | 浣垮尯闂存垚涓轰竴涓滃爢鈥(鍙嚜瀹氫箟姣旇緝鍣) | +| push_heap | 灏嗗厓绱犲姞鍏ヤ竴涓槸鈥滃爢鈥濆尯闂(鍙嚜瀹氫箟姣旇緝鍣) | +| pop_heap | 浠庘滃爢鈥濆尯闂村垹闄ゅ爢椤跺厓绱(鍙嚜瀹氫箟姣旇緝鍣) | | sort_heap | 灏嗕竴涓滃爢鈥濆尯闂磋繘琛屾帓搴忥紝鎺掑簭缁撴潫鍚庯紝璇ュ尯闂村氨鏄櫘 閫氱殑鏈夊簭鍖洪棿锛屼笉鍐嶆槸 鈥滃爢鈥濅簡(鍙嚜瀹氫箟姣旇緝鍣) | #### sort @@ -801,7 +990,7 @@ void sort(RanIt first, RanIt last); ``` - 鎸夊崌搴忔帓搴 -- 鍒ゆ柇x鏄惁搴旀瘮y闈犲墠, 灏辩湅 x < y 鏄惁涓簍rue +- 娌℃湁鑷畾涔夋瘮杈冨櫒鏃讹紝鍒ゆ柇x鏄惁搴旀瘮y闈犲墠, 灏辩湅 x < y 鏄惁涓簍rue 鍘熷瀷2 @@ -811,12 +1000,12 @@ void sort(RanIt first, RanIt last, Pred pr); ``` - 鎸夊崌搴忔帓搴 -- 鍒ゆ柇x鏄惁搴旀瘮y闈犲墠, 灏辩湅 `pr(x,y)`鏄惁涓簍rue +- 鍒ゆ柇x鏄惁搴旀瘮y闈犲墠, 灏辩湅 `pr(x,y)`鏄惁涓簍rue锛屾鏃禶pr`涓哄嚱鏁板璞℃垨鍑芥暟鎸囬拡 sort 瀹為檯涓婃槸蹇熸帓搴, 鏃堕棿澶嶆潅搴 $O(n\log(n)) $ - 骞冲潎鎬ц兘鏈浼 -- 浣嗘槸鏈鍧忕殑鎯呭喌涓, 鎬ц兘鍙兘闈炲父宸 +- 浣嗘槸鏈鍧忕殑鎯呭喌涓, 鎬ц兘鍙兘闈炲父宸$O(n^2)$ 濡傛灉瑕佷繚璇 鈥滄渶鍧忔儏鍐典笅鈥 鐨勬ц兘, 閭d箞鍙互浣跨敤 `stable_sort ` @@ -824,6 +1013,50 @@ sort 瀹為檯涓婃槸蹇熸帓搴, 鏃堕棿澶嶆潅搴 $O(n\log(n)) $ - 鍦ㄦ湁瓒冲瀛樺偍绌洪棿鐨勬儏鍐典笅, 澶嶆潅搴︿负 $n\log(n)$, 鍚﹀垯澶嶆潅搴︿负 $n\log(n)\log(n)$ - `stable_sort` 鐢ㄦ硶鍜 `sort`鐩稿悓銆 +```cpp +#include +#include +#include + +using namespace std; +class MyLess { +public: + bool operator() (int n1, int n2) { + return (n1 % 10) < (n2 % 10); + } +}; +class MyGreater { +public: + bool operator() (int n1, int n2) { + return n1 > n2; + } +}; +int main() { + int a[] = { 14,2,9,111,78 }; + sort(a, a + 5, MyLess()); + int i; + for (i = 0; i < 5; i++) { + cout << a[i] << " "; + } + cout << endl; + sort(a, a + 5, greater()); + for (i = 0; i < 5; i++) + cout << a[i] << " "; + cout<姝ゅ鐨勭浉绛夋槸`==`锛屼綋鐜板湪绋嬪簭閲屾槸x OutIt merge(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr); ``` @@ -917,16 +1150,21 @@ OutIt merge(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred #### includes -```c++ +```cpp template bool includes(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2); ``` -```c++ +```cpp template bool includes(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, Pred pr); ``` +鍒ゆ柇[First2, last2)涓殑姣忎釜鍏冪礌锛屾槸鍚﹂兘鍦╗First1,last1)涓細 + +- 绗竴涓敤`<`浣滀负姣旇緝鍣紝x bst;` - `bst`鏄竴涓敱40浣嶇粍鎴愮殑瀵硅薄 - 鐢╞itset鐨勫嚱鏁板彲浠ユ柟渚垮湴璁块棶浠讳綍涓浣 - -浼楀鎴愬憳鍑芥暟瑙丳PT \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/1.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/1.cpp" deleted file mode 100644 index 1968e9c..0000000 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/1.cpp" +++ /dev/null @@ -1,15 +0,0 @@ -sort(v.begin(), v.end()); - for (i = v.begin(); i < v.end() - 1; i++) - { - for (j = i + 1; j < v.end(); j++) - { - if (binary_search(v.begin(), v.end(), Point(j->x, i->y)) && - binary_search(v.begin(), v.end(), Point(i->x, j->y)) && - i->x != j->x && - i->y != j->y ) - { - nTotalNum++; - } - } - } - cout << nTotalNum / 2; \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/2.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/2.cpp" deleted file mode 100644 index b2500b0..0000000 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/2.cpp" +++ /dev/null @@ -1,20 +0,0 @@ -template -class CMyistream_iterator -{ -private: - T content; - istream &is; -public: - CMyistream_iterator(istream &i):is(i) - { - i >> content; - } - T operator*() - { - return content; - } - void operator++(int) - { - is >> content; - } -}; \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/istream_iterator.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/istream_iterator.cpp" index bd9339c..6fb9a2c 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/istream_iterator.cpp" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/istream_iterator.cpp" @@ -4,26 +4,23 @@ using namespace std; // 在此处补充你的代码 template -class CMyistream_iterator -{ +class CMyistream_iterator : public iterator { private: - T content; - istream &is; + T n; + istream & os; public: - CMyistream_iterator(istream &i):is(i) - { - i >> content; + CMyistream_iterator(istream &os) : os(os) { + os >> n; } - T operator*() - { - return content; + T operator*() { + return n; } - void operator++(int) - { - is >> content; + void operator++(int) { + os >> n; } -}; + //构造函数执行过程中就会要求输入,然后每次执行++,则读取输入流中的下一个项目,执行 * 则返回上次从输入流中读取的项目 +}; int main() { CMyistream_iterator inputInt(cin); diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/pirorty_queue.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/pirorty_queue.cpp" new file mode 100644 index 0000000..185ec06 --- /dev/null +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/pirorty_queue.cpp" @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +int calculPrimeFactors(int n) { + setPrimeFactors; + if (n < 2) { + return 0; + } + int temp = n; + for (int i = 2; i*i <= temp; i++) { + while (temp%i == 0) { + PrimeFactors.insert(i); + temp = temp / i; + } + } + if (temp != n && temp != 1) { + PrimeFactors.insert(n); + } + return PrimeFactors.size(); +} +struct MyLess +{ + bool operator()(const int &n1, const int &n2) { + int f1 = calculPrimeFactors(n1); + int f2 = calculPrimeFactors(n2); + if (f1 == f2) { + return n1 < n2; + } + else { + return f1 < f2; + } + } + +}; +int main() +{ + setmy_set; + int num; + cin >> num; + while (num--) { + for (int i = 0; i < 10; i++) { + int tmp; + cin >> tmp; + my_set.insert(tmp); + } + cout << *(--my_set.end()) << " " << *(my_set.begin()) << endl; + //Returns an iterator referring to the past-the-end element in the set container. + //鏁呰岄渶瑕--锛屽惁鍒欎細鍑虹幇瓒婄晫 + my_set.erase(my_set.begin()); + my_set.erase(--my_set.end()); + } + return 0; +} \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" new file mode 100644 index 0000000..a974e58 --- /dev/null +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234.cpp" @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include + +using namespace std; + +string str[20]; +string command; +int N; + +inline string Copy(); +inline string Add(); +inline int Find(); +inline int Rfind(); +inline void Insert(); +inline void Reset(); +inline string GetString(); +inline void PrintAll(); +inline void Print(); +inline int GetInt(); +inline bool strProcess(); + +int main() { + cin >> N; + for (int i = 1; i <= N; i++) { + cin >> str[i]; + } + while (cin >> command) { + if (command == "over") + break; + switch (command.at(1)) + { + case 'n':Insert(); break; + case 'e': Reset(); break; + case 'r': + if (command == "print") + Print(); + else + PrintAll(); + break; + default: + break; + } + } +} + + +string GetString() { + string temp_command; + cin >> temp_command; + if (temp_command == "copy") + return Copy(); + else if (temp_command == "add") + { + return Add(); + } + else + { + return temp_command; + } +} +int MyAtoi(string s) { + int m = 0; + for (int i = 0; i < s.length(); i++) { + if ((s.at(i) >= '0') && (s.at(i) <= '9')) { + m = m * 10 + s.at(i) - '0'; + } + } + return m; +} +int GetInt() { + string temp_command; + cin >> temp_command; + if (temp_command == "find") { + return Find(); + } + else if (temp_command == "rfind") + return Rfind(); + else + { + return MyAtoi(temp_command); + } +} +void Insert() //insert S N X: 鍦ㄧN涓瓧绗︿覆鐨勭X涓綅缃彃鍏瀛楃涓 +{ + string s; + int n, x; + s = GetString(); + n = GetInt(); + x = GetInt(); + str[n].insert(x, s); +} +void Reset() { //Reset s n; 灏嗙N涓瓧绗︿覆鍙樹负S + string s; + int n; + s = GetString(); + n = GetInt(); + str[n].assign(s); +} +int Rfind() {//rfing S N 鍦ㄧN涓瓧绗︿覆浠庡彸鍚戝乏瀵绘壘S瀛楃涓诧紝杩斿洖鍏剁涓娆″嚭鐜扮殑浣嶇疆锛岃嫢娌℃湁鎵惧埌 + //杩斿洖瀛楃涓查暱搴 + string s; + int n, idex; + s = GetString(); + n = GetInt(); + idex = str[n].rfind(s); + if (idex == string::npos) { + return str[n].length(); + } + else + { + return idex; + } +} +int Find() {//绗琋涓瓧绗︿覆涓粠宸﹀紑濮嬫壘瀵籗瀛楃涓诧紝杩斿洖鍏剁涓娆″嚭鐜扮殑浣嶇疆锛岃嫢娌℃湁鎵惧埌锛岃繑鍥炲瓧绗︿覆鐨勯暱搴 + string s; + int n, idex; + s = GetString(); + n = GetInt(); + idex = str[n].find(s); + if (idex == string::npos) { + return str[n].length(); + } + else + { + return idex; + } +} + +string Copy() { + int n, x, l; + n = GetInt(); + x = GetInt(); + l = GetInt(); + return str[n].substr(x, l); +} + +int GetString_Judge(string s) { + int i,m=0; + for ( i=0; i < s.length(); i++) { + if ((s.at(i) >= '0') && (s.at(i) <= '9')) { + m = m * 10 + s.at(i) - '0'; + } + else + { + break; + } + } + if (i != s.length() || m > 9999) { + m = -1; + } + return m; +} + +string Add() {//add S1 S2锛氬垽鏂璖1锛孲2鏄惁涓0-99999涔嬮棿鐨勬暣鏁帮紝鑻ユ槸鍒欏皢鍏惰浆鍖栦负鏁存暟鍋氬姞娉曪紝 + //鑻ヤ笉鏄紝鍒欎綔瀛楃涓插姞娉曪紝杩斿洖鐨勫间负涓瀛楃涓层 + string s1, s2; + int m1, m2; + s1 = GetString(); + s2 = GetString(); + m1 = GetString_Judge(s1); + m2 = GetString_Judge(s2); + if (m1 == -1 || m2 == -1) { + return (s1 + s2); + } + else + { + m1 += m2; + string s1; + stringstream out; + out << m1; + s1 = out.str(); + return s1; + } + +} +void Print() { + int n; + n = GetInt(); + cout << str[n] << endl; +} + +void PrintAll() { + for (int i = 1; i <= N; i++) { + cout << str[i] << endl; + } +} \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\346\240\207\345\207\206\347\237\251\345\275\242.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\346\240\207\345\207\206\347\237\251\345\275\242.cpp" index 3700d0a..5b2e6bc 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\346\240\207\345\207\206\347\237\251\345\275\242.cpp" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\346\240\207\345\207\206\347\237\251\345\275\242.cpp" @@ -2,14 +2,11 @@ #include #include using namespace std; - -struct Point -{ +struct Point { int x; int y; Point(int x_, int y_) :x(x_), y(y_) { } }; - bool operator < (const Point & p1, const Point & p2) { if (p1.y < p2.y) @@ -19,31 +16,25 @@ bool operator < (const Point & p1, const Point & p2) else return false; } - int main() { int t; int x, y; cin >> t; vector v; - while (t--) - { + while (t--) { cin >> x >> y; v.push_back(Point(x, y)); } vector::iterator i, j; int nTotalNum = 0; - // 在此处补充你的代码 sort(v.begin(), v.end()); - for (i = v.begin(); i < v.end() - 1; i++) - { - for (j = i + 1; j < v.end(); j++) - { + for (i = v.begin(); i != v.end(); i++) { + for (j = i + 1; j != v.end(); j++) { if (binary_search(v.begin(), v.end(), Point(j->x, i->y)) && - binary_search(v.begin(), v.end(), Point(i->x, j->y)) && + binary_search(v.begin(), v.end(), Point(j->x, i->y)) && i->x != j->x && - i->y != j->y ) - { + i->y != j->y) { nTotalNum++; } } diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\203\255\350\241\200\346\240\274\346\226\227\345\234\272.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\203\255\350\241\200\346\240\274\346\226\227\345\234\272.cpp" index ee97c5d..6806323 100644 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\203\255\350\241\200\346\240\274\346\226\227\345\234\272.cpp" +++ "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\203\255\350\241\200\346\240\274\346\226\227\345\234\272.cpp" @@ -1,28 +1,17 @@ -#include -#include +#include using namespace std; - -int main() -{ - typedef map power_id_map; - power_id_map members; - members[1000000000] = 1; - - int n; - cin >> n; - while (n--) - { - int id, power; - cin >> id >> power; - power_id_map::iterator i_less, i_geq, i_match; - i_less = members.lower_bound(power); - i_geq = i_less; - if (i_less != members.begin()) - { - i_less--; - } - i_match = (power - i_less->first) <= (i_geq->first - power) ? i_less : i_geq; - cout << id << " " << i_match->second << endl; - members[power] = id; +class Number { +public: + int num; + Number(int n = 0) : num(n) {} + Number & operator * (const Number n1, const Number n2) { + return n1.num * n2.num; } +}; + +int main() { + Number n1(10), n2(20); + Number n3; n3 = n1 * n2; + cout << int(n3) << endl; + return 0; } \ No newline at end of file diff --git "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\273\203\344\271\240\351\242\230.cpp" "b/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\273\203\344\271\240\351\242\230.cpp" deleted file mode 100644 index ce94e32..0000000 --- "a/3.C++\347\250\213\345\272\217\350\256\276\350\256\241/week9/\347\274\226\347\250\213\347\273\203\344\271\240/\347\273\203\344\271\240\351\242\230.cpp" +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -using namespace std; - -int calculatePrimeFactors(int n) -{ - set PrimeFactors; - if (n < 2) - { - return 0; - } - int tmp = n; - for (int i = 2; i*i <= tmp; i++) - { - while (tmp % i == 0) - { - PrimeFactors.insert(i); - tmp /= i; - } - } - if (tmp != n && tmp != 1) - { - PrimeFactors.insert(n); - } - return PrimeFactors.size(); -} - -struct myLess -{ - bool operator()(const int &n1, const int &n2) - { - int f1 = calculatePrimeFactors(n1); - int f2 = calculatePrimeFactors(n2); - if (f1 == f2) - { - return n1 < n2; - } - else - { - return f1 < f2; - } - } -}; - -int main() -{ - set mySet; - int num; - cin >> num; - while (num--) - { - for (int i = 0; i < 10; i++) - { - int tmp; - cin >> tmp; - mySet.insert(tmp); - } - cout << *(mySet.rbegin()) << " " << *(mySet.begin()) << endl; - mySet.erase(mySet.begin()); - mySet.erase(--mySet.end()); - } -} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/Bomb_in_Brazil.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/Bomb_in_Brazil.cpp" new file mode 100644 index 0000000..3572bd5 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/Bomb_in_Brazil.cpp" @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +using namespace std; + +multimap Map; +multimap::iterator mi; +multiset Set; +multiset::iterator si; +int mark[1035][1035] = {0}; //鏍囪鍨冨溇鐨勪綅缃強鏁伴噺 +int bomb[1035][1035] = {0}; //鏍囪鐐稿脊鍦ㄦ澶勫彲娓呯悊澶氬皯鍨冨溇 + +int main() { + int caseN; + cin >> caseN; + while (caseN) { + --caseN; + + memset(mark, 0, sizeof(mark)); + memset(bomb, 0, sizeof(bomb)); + Map.clear(); + Set.clear(); + + int d, n; + cin >> d >> n; + int x, y, num; + for (int i = 0; i < n; ++i) { + cin >> x >> y >> num; + mark[x][y] = num; + Map.insert(map::value_type(x, y)); //鎶婄偣鏀捐繘鍘 + } + + int tempsum = 0, sum = 0; + for (mi = Map.begin(); mi != Map.end(); ++mi) { + int xstart = max(mi->first - d, 0), xend = min(mi->first + d, 1024), + ystart = max(mi->second - d, 0), yend = min(mi->second + d, 1024); + while (1) { + if (ystart > yend) { + ++xstart; + ystart = max(mi->second - d, 0); + if (xstart > xend) { + break; + } + } + if (bomb[xstart][ystart] > 0) { + ++ystart; + continue; + } + int bombxs = max(xstart - d, 0), bombxe = min(xstart + d, 1024), + bombys = max(ystart - d, 0), bombye = min(ystart + d, 1024); + for (int i = bombxs; i <= bombxe; ++i) { + for (int j = bombys; j <= bombye; ++j) { + bomb[xstart][ystart] += mark[i][j]; + } + } + if (bomb[xstart][ystart] >= sum) { + sum = bomb[xstart][ystart]; + Set.insert(sum); //鏀惧叆鐩墠鏈澶х殑鑳藉琚竻鐞嗙殑鍨冨溇鏁伴噺 + } + ++ystart; + } + } + si = Set.end(); + --si; + int count = Set.count(*si); + cout << count << " " << *si << endl; + } + + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/annoying_frog.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/annoying_frog.cpp" new file mode 100644 index 0000000..8fe1705 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/annoying_frog.cpp" @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +int r, c, rnum; +int sum; +int n; +int ma[53][53] = {}; +int ans = 14; +struct point { + int x; + int y; + bool operator<(const point &a) const { + if (a.x == x) + return y < a.y; + return x < a.x; + } + point(){}; +} p[702]; +struct route { + point start; + int dx; + int dy; + int step; + bool operator<(const route &a) const { return step > a.step; }; + route(point a, int b, int c, int d) { + start = a; + dx = b; + dy = c; + step = d; + }; + route(){}; +} ro[1000001]; +bool inside(int x, int y) { return (1 <= x && x <= r && 1 <= y && y <= c); } +int match(point start, int dx, int dy) { + if (inside(start.x - dx, start.y - dy)) + return 0; + int k; + for (k = 0;; k++) { + if (inside(start.x + k * dx, start.y + k * dy)) { + if (ma[start.x + k * dx][start.y + k * dy] == 0) { + return 0; + } + } else + break; + } + if (k > 2) + return k; + return 0; +} +void dfs(int code, int s, int num) { + if (sum == s) { + ans = min(ans, num); + return; + } + for (int i = code; i < rnum; ++i) { + if (num + (sum - s) / ro[i].step >= ans) + return; + if (match(ro[i].start, ro[i].dx, ro[i].dy) == 0) + continue; + for (int x = ro[i].start.x, y = ro[i].start.y; + 1 <= x && x <= r && y >= 1 && y <= c; x += ro[i].dx, y += ro[i].dy) + --ma[x][y]; + dfs(i, s + ro[i].step, num + 1); + for (int x = ro[i].start.x, y = ro[i].start.y; + 1 <= x && x <= r && y >= 1 && y <= c; x += ro[i].dx, y += ro[i].dy) + ++ma[x][y]; + } + return; +} +int main() { + int t; + cin >> t; + while (t--) { + rnum = 0; + ans = 14; + cin >> r >> c; + cin >> n; + sum = 0; + for (int i = 0; i < n; ++i) { + cin >> p[i].x >> p[i].y; + int l; + cin >> l; + sum += l; + ma[p[i].x][p[i].y] = l; + } + sort(p, p + n); + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + int st = match(p[i], p[j].x - p[i].x, p[j].y - p[i].y); + if (st != 0) { + ro[rnum++] = route(p[i], p[j].x - p[i].x, p[j].y - p[i].y, st); + } + } + } + sort(ro, ro + rnum); + dfs(0, 0, 0); + cout << ans << endl; + } +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/apartment.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/apartment.cpp" new file mode 100644 index 0000000..52eab51 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/apartment.cpp" @@ -0,0 +1,123 @@ +#include + +/* dir:u,d,l,r + * face:ABDC,BFHD,FEGH,EACG,EFBA,GHDC + * next[face][dir] + */ + +#define TEST +#undef TEST + +const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; +const int next[6][4] = {{4, 5, 3, 1}, {4, 5, 0, 2}, {4, 5, 1, 3}, + {4, 5, 2, 0}, {2, 0, 3, 1}, {2, 0, 3, 1}}; +int map[6][20][20]; +int num, max, cases, n, area; + +int bfs(int face, int i, int j) { + int sum = 1; + int I, J, F; + map[face][i][j] = 1; + for (int d = 0; d < 4; d++) { + I = i + dir[d][0]; + J = j + dir[d][1]; + F = face; + if ((face - 4) * (face - 5)) { + if (J < 0) + F = next[face][2], J = n - 1; + if (J >= n) + F = next[face][3], J = 0; + } + if ((face - 1) * (face - 3)) { + if ((I < 0 || I >= n) && face == 2) + J = n - 1 - J; + if (I < 0) + F = next[face][0], (face == 2 || face == 4) ? I = 0 : I = n - 1; + if (I >= n) + F = next[face][1], (face == 0 || face == 5) ? I = n - 1 : I = 0; + if (F != face && F == 2) + J = n - 1 - J; + } + if (face * (face - 2)) { + switch (face) { + case 4: + if (J < 0) + F = next[face][2], J = I, I = 0; + if (J >= n) + F = next[face][3], J = n - 1 - I, I = 0; + break; + case 5: + if (J < 0) + F = next[face][2], J = I, I = n - 1; + if (J >= n) + F = next[face][3], J = n - 1 - I, I = n - 1; + break; + case 1: + if (I < 0) + F = next[face][0], I = n - 1 - J, J = n - 1; + if (I >= n) + F = next[face][1], I = n - 1 - J, J = n - 1; + break; + case 3: + if (I < 0) + F = next[face][0], I = J, J = 0; + if (I >= n) + F = next[face][1], I = J, J = 0; + break; + } + } + if (!map[F][I][J]) { +#ifdef TEST + printf("%d:%d %d %d ", d, face, i, j); + printf("-> %d %d %d\n", F, I, J); +#endif + sum += bfs(F, I, J); + } + } + return sum; +} +void test() { + for (int face = 0; face < 6; face++) { + printf("face:%d\n", face); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + printf("%d%c", map[face][i][j], (j == n - 1) ? '\n' : ' '); + } + printf("\n\n"); + return; +} + +int main() { +#ifdef TEST + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif + scanf("%d", &cases); + while (cases--) { + scanf("%d", &n); + num = 0; + max = 0; + for (int face = 0; face < 6; face++) + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + scanf("%d", &map[face][i][j]); + for (int face = 0; face < 6; face++) + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + if (!map[face][i][j]) { +#ifdef TEST + printf("%d %d %d\n\n", face, i, j); + test(); +#endif + num++; + area = bfs(face, i, j); + if (area > max) + max = area; +#ifdef TEST + printf("num=%d,nowArea=%d,max=%d\n", num, area, max); +#endif + } + printf("%d %d\n", num, max); + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/complex_divide.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/complex_divide.cpp" new file mode 100644 index 0000000..4afcab2 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/complex_divide.cpp" @@ -0,0 +1,63 @@ +#include +#include +using namespace std; +#define N 51 +int dp1[N][N]; // N鍒掑垎鎴怟涓鏁存暟涔嬪拰鐨勫垝鍒嗘暟鐩 +int dp2[N][N]; // N鍒掑垎鎴愯嫢骞蹭釜涓嶅悓姝f暣鏁颁箣鍜岀殑鍒掑垎鏁扮洰銆 +int dp3[N][N]; // N鍒掑垎鎴愯嫢骞蹭釜鍙浉鍚岀殑姝f暣鏁颁箣鍜岀殑鍒掑垎鏁扮洰銆 +int f[N][N]; // N鍒掑垎鎴怟涓姝f暣鏁颁箣鍜岀殑鍒掑垎鏁扮洰銆 +int g[N][N]; // N鍒掑垎鎴怟涓伓姝f暣鏁颁箣鍜岀殑鍒掑垎鏁扮洰銆 + +void initDivideInt() { + memset(dp1, 0, sizeof(dp1)); // dp[n][k]=dp[n-k][k]+dp[n-1][k-1] + memset(dp2, 0, sizeof(dp2)); // dp[n][m]=dp[n][m-1]+dp[n-m][m-1] + memset(dp3, 0, sizeof(dp3)); // dp[n][m]=dp[n][m-1]+dp[n-m][m] + + for (int i = 1; i < N; i++) { + for (int j = 1; j < N; j++) { + if (i < j) { + dp1[i][j] = 0; + dp2[i][j] = dp2[i][i]; + dp3[i][j] = dp3[i][i]; + } else if (i == j) { + dp1[i][j] = 1; + dp2[i][j] = dp2[i][j - 1] + 1; + dp3[i][j] = dp3[i][j - 1] + 1; + } else { + dp1[i][j] = dp1[i - j][j] + dp1[i - 1][j - 1]; + dp2[i][j] = dp2[i][j - 1] + dp2[i - j][j - 1]; + dp3[i][j] = dp3[i][j - 1] + dp3[i - j][j]; + } + } + } +} + +// f[i][j]=f[i-1][j-1]+g[i-j][j] +void initDivideOdd() { + f[0][0] = 1; + g[0][0] = 1; + for (int i = 1; i < N; i++) { + for (int j = 1; j <= i; j++) { + g[i][j] = f[i - j][j]; + f[i][j] = f[i - 1][j - 1] + g[i - j][j]; + } + } +} + +int main() { + // freopen("in.txt", "r", stdin); + int n, k; + initDivideInt(); + initDivideOdd(); + while (cin >> n >> k) { + cout << dp1[n][k] << endl; + cout << dp2[n][n] << endl; + + int sum = 0; + for (int i = 0; i <= n; i++) { + sum += f[n][i]; + } + cout << sum << endl; + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/highest_common_factor.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/highest_common_factor.cpp" new file mode 100644 index 0000000..e3473e8 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/highest_common_factor.cpp" @@ -0,0 +1,12 @@ +#include +int main() { + int a,b; + while (scanf("%d%d", &a,&b) != EOF) { + while ( b != 0) { + int temp = a % b; + a=b; b = temp; + } + printf("%d\n", a); + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/hua.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/hua.cpp" new file mode 100644 index 0000000..2d8cbc3 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/hua.cpp" @@ -0,0 +1,70 @@ +#include +using namespace std; +int l, r, c, k, i, j, m, T; // T缁勬暟鎹 +char map[201][201]; +int num, + que[40001] + [3]; //闃熷垪锛宷ue[i][2]璁板綍璧扮殑绗嚑姝,que[i][0],璁板綍璧拌繃鐨勭偣妯潗鏍噚锛宷ue[i][1]绾靛潗鏍 +int soux, souy, desx, desy, head, tail; //璁板綍鍑哄彂鐐广佺洰鏍囩偣鐨勫眰鏁帮紝鍧愭爣锛 +int dx[4] = {-1, 0, 1, 0}, + dy[4] = {0, 1, 0, -1}; //鍧愭爣鎸変笂銆佸彸銆佷笅銆佸乏鍥涗釜鏂瑰悜鍙樺寲 +bool ff = false; +int main() { + cin >> T; + while (T > 0) // T缁勬暟鎹 + { + ff = false; //瀵规瘡 缁勬暟鎹紝鍒濆鍖栦负鏃犳硶鎵惧埌濂堕叒 + cin >> r >> c; + for (i = 1; i <= r; i++) + for (j = 1; j <= c; j++) { + cin >> map[i][j]; + if (map[i][j] == 'S') { + soux = i; + souy = j; + map[i][j] = '#'; + } + if (map[i][j] == 'E') { + desx = i; + desy = j; + } + } + head = 0; + tail = 1; + //灏嗚捣濮嬬偣鍏ラ槦 + que[1][0] = soux; + que[1][1] = souy; + que[1][2] = 0; //姝ユ暟鍒濆鍖栦负0 + + while (head < tail) { + head++; + int l1, x1, y1; + for (i = 0; i < 4; i++) { + x1 = que[head][0] + dx[i]; + y1 = que[head][1] + dy[i]; + if (1 <= x1 && x1 <= r && 1 <= y1 && y1 <= c) //鍧愭爣涓嶈秴鍑鸿竟鐣 + if (map[x1][y1] == 'E') //鎵惧埌浜嗙粓鐐癸紝杈撳嚭 + { + tail++; + que[tail][0] = x1; + que[tail][1] = y1; + que[tail][2] = que[head][2] + 1; //姝ユ暟鍔1 + cout << que[tail][2] << endl; + head = tail + 1; + ff = true; + break; + } else if (map[x1][y1] == '.') //鎵惧埌浜嗗彲浠ヨ蛋鐨勭偣锛屽叆闃 + { + tail++; + que[tail][0] = x1; + que[tail][1] = y1; + que[tail][2] = que[head][2] + 1; //姝ユ暟鍔1 + map[x1][y1] = '#'; //璧拌繃鐨勭偣鍙樹负涓嶅彲閫氳鐨勭偣 + } + } + } + if (!ff) + cout << "oop!" << endl; + T--; + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/minecraft.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/minecraft.cpp" new file mode 100644 index 0000000..4cc043c --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/minecraft.cpp" @@ -0,0 +1,36 @@ +#include +using namespace std; + +int n[1001]; +void fun() { + for (int k=1; k<=1000; k++){ + int minarea=100000; + for (int len=1; len<=k; len++) { + if (k%len==0) { + int maxwi=k/len; + for(int wi=1; wi<=maxwi; wi++) { + if (maxwi%wi==0) { + int hi=maxwi/wi; + int area=(len*wi+len*hi+wi*hi)*2; + if (minarea>area) { + minarea=area; + } + } + } + } + } + n[k]=minarea; + } +} + +int main() { + int c; + cin >> c; + fun(); + int i; + while(c--) { + cin >> i; + cout << n[i] << endl; + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/resuce.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/resuce.cpp" new file mode 100644 index 0000000..52d0862 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/resuce.cpp" @@ -0,0 +1,128 @@ +#include +#include +#include +#include +using namespace std; +queue X; +queue Y; +queue D; +queue Sum; +int t, n, m, k, sx, sy, fx, fy, f; +char S[205][205]; +int vis[205][205][50]; +int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; +bool check(int x, int y, int l) { + if (x < 1 || y < 1 || x > n || y > m) { + return 0; + } + if (S[x][y] >= '0' && S[x][y] <= '4') { + int h = pow(2, S[x][y] - '0'); + if (!vis[x][y][l + h]) + return 1; + } + if (S[x][y] == '#' || vis[x][y][l]) { + return 0; + } + return 1; +} +void bfs() { + while (!X.empty()) { + int x = X.front(), y = Y.front(), d = D.front(), sum = Sum.front(), num = 0; + if (d >= 16) { + num++; + } + if (d % 16 >= 8) { + num++; + } + if (d % 8 >= 4) { + num++; + } + if (d % 4 >= 2) { + num++; + } + if (d % 2 >= 1) { + num++; + } + for (int i = 0; i < 4; i++) { + int xx = x + dir[i][0], yy = y + dir[i][1]; + if (check(xx, yy, d)) { + if (xx == fx && yy == fy && num >= k) { + f = 1; + printf("%d\n", sum + 1); + return; + } + if (S[xx][yy] >= '0' && S[xx][yy] <= '4') { + int o = pow(2, S[xx][yy] - '0'); + if (d % (o * 2) < o) { + X.push(xx); + Y.push(yy); + D.push(d + pow(2, S[xx][yy] - '0')); + Sum.push(sum + 1); + vis[xx][yy][d + 1] = 1; + continue; + } + } + if (S[xx][yy] == '$') { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if ((i != xx || j != yy) && S[i][j] == '$' && vis[i][j][d] == 0) { + vis[i][j][d] = 1; + X.push(i); + Y.push(j); + D.push(d); + Sum.push(sum + 1); + } + } + } + } + X.push(xx); + Y.push(yy); + D.push(d); + Sum.push(sum + 1); + vis[xx][yy][d] = 1; + } + } + X.pop(); + Y.pop(); + D.pop(); + Sum.pop(); + } +} +int main() { + scanf("%d", &t); + for (; t; t--) { + scanf("%d %d %d\n", &n, &m, &k); + while (!X.empty()) { + X.pop(); + Y.pop(); + Sum.pop(); + D.pop(); + } + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + S[i][j] = getchar(); + if (S[i][j] == 'S') { + vis[i][j][0] = 1; + sx = i; + sy = j; + X.push(i); //绾靛潗鏍 + Y.push(j); //妯潗鏍 + D.push(0); //鏀堕泦鐨勫疂鐭虫暟鎹寲 + Sum.push(0); //琛岃蛋鐨勬鏁 + } + if (S[i][j] == 'E') { + fx = i; + fy = j; + } + } + getchar(); + } + bfs(); + if (!f) { + printf("oop!\n"); + } + memset(vis, 0, sizeof(vis)); + memset(S, 0, sizeof(S)); + f = 0; + } +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/simple_divide.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/simple_divide.cpp" new file mode 100644 index 0000000..a21a2ae --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week10/simple_divide.cpp" @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +const int NMAX = 55; +int dp[NMAX][NMAX] = {}; + +int main() +{ + int n,i,j,k,ans=0; + while (cin >> n) + { + for (i=0; i +#include + +int n, k; +int num[4], target; // L:0,V:1 +long long int dp[4][MAX_N * 2][MAX_N * 2][MAX_N][MAX_N], ans; + +void initialize() { + dp[0][2][1][1][1] = 1; + dp[1][1][2][1][1] = 1; + dp[2][1][1][2][1] = 1; + dp[3][1][1][1][2] = 1; + for (int ll = 1; ll < MAX_N * 2; ll++) + for (int lv = 1; lv < MAX_N; lv++) + for (int vl = 1; vl < MAX_N; vl++) + for (int vv = 1; vv < MAX_N; vv++) + if (ll + lv + vl + vv > 5) { + dp[LL][ll][lv][vl][vv] = + dp[LV][ll][lv][vl - 1][vv] + dp[LL][ll - 1][lv][vl][vv]; + dp[LV][ll][lv][vl][vv] = + dp[LV][ll][lv][vl][vv - 1] + dp[LL][ll][lv - 1][vl][vv]; + dp[VL][ll][lv][vl][vv] = + dp[VV][ll][lv][vl - 1][vv] + dp[VL][ll - 1][lv][vl][vv]; + dp[VV][ll][lv][vl][vv] = + dp[VV][ll][lv][vl][vv - 1] + dp[VL][ll][lv - 1][vl][vv]; + } + return; +} + +inline int code(char ch1, char ch2) { + return ((ch1 == 'V') << 1) + (ch2 == 'V'); +} + +int main() { + // freopen("input.txt","r",stdin); + char ch1, ch2; + int j, nt[4]; + initialize(); + while (scanf("%d %d\n", &n, &k) == 2) { + ans = 0; + memset(num, 0, sizeof(num)); + scanf("%c%c\n", &ch1, &ch2); + target = code(ch2, ch1); + for (int i = 1; i <= n; i++) { + scanf("%c%c\n", &ch1, &ch2); + num[code(ch1, ch2)]++; + } + j = 0; + for (int i = LL; i <= VV; i++) + if (num[i] > num[j]) + j = i; + j = (j & 2) + ((j & 2) >> 1); + for (int i = 0; i < 4; i++) + nt[i] = num[i]; + for (int i = 0; i < 4; i++) + num[i] = nt[i ^ j]; + target ^= j; + // printf("%d %d %d %d:%d",num[0],num[1],num[2],num[3],target); + for (int ll = 0; ll <= num[LL]; ll++) + for (int lv = 0; lv <= num[LV] && ll + lv <= k; lv++) + for (int vl = 0; vl <= num[VL] && ll + lv + vl <= k; vl++) + if (k - ll - lv - vl <= num[VV]) + ans += dp[target][ll + 1][lv + 1][vl + 1][k - ll - lv - vl + 1]; + if (ans) + printf("YES\n%lld\n", ans); + else + printf("NO\n"); + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/README.md" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/README.md" index 02ab669..957c098 100644 --- "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/README.md" +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/README.md" @@ -76,78 +76,62 @@ ### 瑙g瓟绋嬪簭 -```c++ -#include +```cpp +#include +#include using namespace std; - int puzzle[6][8], press[6][8]; -// 鍒濆鍖栨暟缁勫拰澶勭悊杈撳叆 -void init() -{ - for (int i = 1; i < 6; i++) - { - for (int j = 1; j < 7; j++) - { +void init() { + for (int i = 1; i < 6; i++) { + for (int j = 1; j < 6; j++) { cin >> puzzle[i][j]; } } - for (int j = 0; j < 8; j++) - { - press[0][j] = 0; - } - for (int i = 0; i < 6; i++) - { + for (int i = 0; i < 6; i++) { press[i][0] = 0; press[i][7] = 0; } + for (int i = 0; i < 8; i++) { + press[0][i] = 0; + } } - -// 楠岃瘉绛旀鏄惁姝g‘锛屼富瑕侀獙璇佹渶鍚庝竴琛 -bool guess() -{ - for (int j = 1; j < 7; j++) - { - if ((press[5][j - 1] + press[5][j] + press[5][j + 1] + press[4][j]) % 2 != puzzle[5][j]) +bool guess() { + for (int c = 1; c < 7; c++) { + if ((press[5][c - 1] + press[5][c] + press[5][c + 1] + press[4][c]) % 2 != puzzle[5][c]) { return false; + } + return true; } - return true; } -// 鏍规嵁绗竴琛岀殑鏋氫妇鏇存柊鍓╀綑鍑犺 -void update() -{ +void update(){ for (int i = 1; i < 5; i++) - { - for (int j = 1; j < 7; j++) { - press[i + 1][j] = (puzzle[i][j] + press[i][j] + press[i][j - 1] + press[i][j + 1] + press[i - 1][j]) % 2; + for (int j = 1; j < 7; j++) + { + press[i + 1][j] = (puzzle[i][j] + press[i][j] + press[i][j - 1] + press[i][j + 1] + press[i - 1][j]) % 2; + } } } -} -// 鏋氫妇绗竴琛岀殑鎵鏈夋儏鍐碉紝鐩村埌寰楀嚭绛旀 -void enumerate() -{ - // 鍒濆鎯呭喌 - for (int j = 1; j < 7; j++) - { +void enmuerate() { + for (int j = 1; j < 7; j++) { press[1][j] = 0; } update(); while (!guess()) { press[1][1]++; - int i = 1; - while (press[1][i] > 1) // 杩涗綅 - { - press[1][i] = 0; - press[1][++i]++; + int c = 1; + while (press[1][c] > 1) { + press[1][c] = 0; + press[1][++c] ++; } update(); } + } - int main() { int ncase; @@ -156,8 +140,7 @@ int main() { cout << "PUZZLE #" << i + 1 << endl; init(); - enumerate(); - // 杈撳嚭缁撴灉 + enmuerate(); for (int i = 1; i < 6; i++) { for (int j = 1; j < 7; j++) @@ -191,7 +174,7 @@ int main() ### 鏍蜂緥杈撳叆 -``` +```cpp 6 7 14 2 1 @@ -212,7 +195,7 @@ int main() ### 鏍蜂緥杈撳嚭 -``` +```cpp 7 ``` @@ -234,7 +217,7 @@ int main() ### 瑙i绋嬪簭 -```c++ +```cpp #include #include using namespace std; diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/\347\274\226\347\250\213\347\273\203\344\271\240/\347\224\273\345\256\266\351\227\256\351\242\230.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/\347\274\226\347\250\213\347\273\203\344\271\240/\347\224\273\345\256\266\351\227\256\351\242\230.cpp" index ba52e8d..4fc6e36 100644 --- "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/\347\274\226\347\250\213\347\273\203\344\271\240/\347\224\273\345\256\266\351\227\256\351\242\230.cpp" +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week2/\347\274\226\347\250\213\347\273\203\344\271\240/\347\224\273\345\256\266\351\227\256\351\242\230.cpp" @@ -104,7 +104,7 @@ int main() { int ncase; cin >> ncase; - + for (int i = 0; i < ncase; i++) { cin >> width; diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/Maze_game.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/Maze_game.cpp" new file mode 100644 index 0000000..9f656d0 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/Maze_game.cpp" @@ -0,0 +1,93 @@ +#include +#include +#define MAXIN 75 +char board[MAXIN + 2][MAXIN + 2];//define the board +int minstep, width, height; +int to[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } }; +//define the step motion +bool mark[MAXIN + 2][MAXIN + 2];//set the mark to check whether this board has been explored +using namespace std; + +void Search(int now_x, int now_y, int end_x, int end_y, int step, int f) { + //now_x, now_y: the current position + //end_x, end_y: the end position + //step: counter for how many steps have been used + //f: the direction from last step to (now_x, now_y) + if (step > minstep) + return; + if (now_x == end_x && now_y == end_y) { + // arrive the end point + if (minstep > step) { + minstep = step; + } + return; + } + for (int i = 0; i < 4; i++) { + int x = now_x + to[i][0]; + int y = now_y + to[i][1]; + bool position_inside_board = (x > -1) && (x < width + 2) && (y > -1) && (y < height + 2); + bool step_noCard_noMark = (board[y][x] == ' ') && (mark[y][x] == false); + bool step_to_end = (y == end_y) && (x == end_x) && (board[y][x] == 'X'); + if (position_inside_board && (step_noCard_noMark || step_to_end)) { + mark[y][x] = true; + //if the new position is suitable, then mark it as True + if (f == i) + Search(x, y, end_x, end_y, step, i); + else + Search(x, y, end_x, end_y, step + 1, i); + // if all the directions from this step are turn out to be unsuitable + mark[y][x] = false; + } + } +} +int main() { + int Boardnum = 0; + while (cin >> width >> height) { + if (!height && !width) { + break; + } + Boardnum++; + for (int i = 0; i < height + 2; i++) { + board[i][0] = ' '; + board[i][width + 1] = ' '; + } + for (int i = 0; i < width + 2; i++) { + board[0][i] = ' '; + board[height + 1][i] = ' '; + } + for (int i = 1; i <= height; i++) { + cin.get(); + for (int j = 1; j <= width; j++) { + board[i][j] = cin.get(); + } + } + int x, y, end_x, end_y; + int pair = 0; + while (cin >> x >> y >> end_x >> end_y) + { + pair++; + minstep = 100000; + memset(mark, false, sizeof(mark)); + if (x && y && end_x && end_y) + { + Search(x, y, end_x, end_y, 0, -1);//at first, the direction flag do not set + cout << "Pair " << pair << ": "; + if (minstep == 100000) + { + cout << "impossible." << endl; + } + else + { + cout << minstep << " segments." << endl; + } + } + else + { + break; + } + } + cout << endl; + + } + +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/README.md" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/README.md" index be020fb..0d76c51 100644 --- "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/README.md" +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/README.md" @@ -38,7 +38,7 @@ - 鎵惧嚭閫掓帹鍏紡 - 鎵惧埌閫掑綊缁堟鏉′欢 -娉ㄦ剰浜嬮」: 鐢变簬鍑芥暟鐨勫眬閮ㄥ彉閲忔槸瀛樺湪鏍堜笂鐨勶紝濡傛灉鏈変綋绉ぇ鐨勫眬閮ㄥ彉閲忥紙姣斿鏁扮粍锛夎岄掑綊灞傛鍙兘寰堟繁鐨勬儏鍐典笅锛屼篃璁镐細瀵艰嚧鏍堟孩鍑恒傚彲浠ヨ冭檻浣跨敤鍏ㄥ眬鏁扮粍鎴栧姩鎬佸垎閰嶆暟缁勩 +娉ㄦ剰浜嬮」: 鐢变簬鍑芥暟鐨勫眬閮ㄥ彉閲忔槸瀛樺湪鏍堜笂鐨勶紝濡傛灉鏈変綋绉ぇ鐨勫眬閮ㄥ彉閲忥紙姣斿鏁扮粍锛夎岄掑綊灞傛鍙兘寰堟繁鐨勬儏鍐典笅锛屼篃璁镐細瀵艰嚧鏍堟孩鍑恒傚彲浠ヨ冭檻浣跨敤**鍏ㄥ眬鏁扮粍**鎴**鍔ㄦ佸垎閰**鏁扮粍銆 @@ -114,92 +114,80 @@ Pair 3: impossible. ### 瑙i绋嬪簭 -```c++ -#include +```cpp +#include +#include +#define MAXIN 75 +char board[MAXIN + 2][MAXIN + 2];//define the board +int minstep, width, height; +int to[4][2] = { {0,1},{1,0},{0,-1},{-1,0} }; +//define the step motion +bool mark[MAXIN + 2][MAXIN + 2];//set the mark to check whether this board has been explored using namespace std; -char Board[77][77] = { 0 }; -bool Mark[77][77] = { 0 }; -int minstep; -int width, height; -int to[4][2] = { {0,-1},{0,1},{-1,0},{1,0} }; - -void searchPath(int x, int y, int end_x, int end_y, int step, int dir) -{ +void Search(int now_x, int now_y, int end_x, int end_y, int step, int f) { + //now_x, now_y: the current position + //end_x, end_y: the end position + //step: counter for how many steps have been used + //f: the direction from last step to (now_x, now_y) if (step > minstep) - { return; - } - if (x == end_x && y == end_y) - { - if (step < minstep) - { + if (now_x == end_x && now_y == end_y) { + // arrive the end point + if (minstep > step) { minstep = step; - return; } + return; } - - for (int i = 0; i < 4; i++) - { - int new_x = x + to[i][0]; - int new_y = y + to[i][1]; - if ((new_x > -1 && new_x < width + 2 && new_y > -1 && new_y < height + 2) - && ((Board[new_y][new_x] == ' ' && !Mark[new_y][new_x]) - || (Board[new_y][new_x] == 'x' && new_x == end_x && new_y == end_y))) - { - Mark[new_y][new_x] = true; - if (i == dir) - { - searchPath(new_x, new_y, end_x, end_y, step, i); - } + for (int i = 0; i < 4; i++) { + int x = now_x + to[i][0]; + int y = now_y + to[i][1]; + bool position_inside_board = (x > -1) && (x < width + 2) && (y > -1) && (y < height + 2); + bool step_noCard_noMark = (board[y][x] == ' ') && (mark[y][x] == false); + bool step_to_end = (y == end_y) && (x == end_x)&&(board[y][x]=='X'); + if (position_inside_board && (step_noCard_noMark || step_to_end)) { + mark[y][x] = true; + //if the new position is suitable, then mark it as True + if (f == i) + Search(x, y, end_x, end_y, step, i); else - { - searchPath(new_x, new_y, end_x, end_y, step + 1, i); - } - // 杩欐潯璺緞娌℃悳鍒帮紝鎾ゅ洖鏍囪 - Mark[new_y][new_x] = false; + Search(x, y, end_x, end_y, step + 1, i); + // if all the directions from this step are turn out to be unsuitable + mark[y][x] = false; } } } - -int main() -{ - while (cin >> width >> height) - { - if (!height && !height) - { +int main() { + int Boardnum = 0; + while (cin >> width >> height) { + if (!height && !width) { break; } - // 杈撳叆Board - for (int i = 1; i <= height; i++) - { - cin.get(); - for (int j = 1; j <= width; j++) - { - Board[i][j] = cin.get(); - } + Boardnum++; + for (int i = 0; i < height + 2; i++) { + board[i][0] = ' '; + board[i][width + 1] = ' '; } - // 澶勭悊Board杈圭紭 - for (int i = 0; i < height + 2; i++) - { - Board[i][0] = ' '; - Board[i][width + 1] = ' '; + for (int i = 0; i < width + 2; i++) { + board[0][i] = ' '; + board[height + 1][i] = ' '; } - for (int j = 0; j < width + 2; j++) - { - Board[0][j] = ' '; - Board[height + 1][j] = ' '; + for (int i = 1; i <= height; i++) { + cin.get(); + for (int j = 1; j <= width; j++) { + board[i][j] = cin.get(); + } } - int x, y, end_x, end_y; int pair = 0; while (cin >> x >> y >> end_x >> end_y) { pair++; minstep = 100000; + memset(mark, false, sizeof(mark)); if (x && y && end_x && end_y) { - searchPath(x, y, end_x, end_y, 0, -1); + Search(x, y, end_x, end_y, 0, -1);//at first, the direction flag do not set cout << "Pair " << pair << ": "; if (minstep == 100000) { @@ -216,8 +204,9 @@ int main() } } cout << endl; + } - return 0; + } ``` diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/cheeseBoard_divide.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/cheeseBoard_divide.cpp" new file mode 100644 index 0000000..e606f57 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week3/cheeseBoard_divide.cpp" @@ -0,0 +1,61 @@ +#include +#include +#include +#include +using namespace std; + +int s[9][9]; // the score in every grid +int sum[9][9]; // the sum of the rectangel with (1,1) to (i,j) +int res[15][9][9][9][9]; // table for fun f + +int calSum(int x1, int y1, int x2, int y2) { + //the sum of scores from (x1, y1) to (x2, y2) + return sum[x2][y2] - sum[x2][y1-1] - sum[x1-1][y2] + sum[x1-1][y1-1]; +} + +int fun(int n, int x1, int y1, int x2, int y2) { + int t, a, b, c, e, MIN = 1000000; + if (res[n][x1][y1][x2][y2] != -1) + { + return res[n][x1][y1][x2][y2]; + } + if (n==1) //if only one piece needs to be divided + { + t = calSum(x1,y1, x2,y2); + res[n][x1][y1][x2][y2] = t * t; + return t * t; + } + for(a=x1;at) MIN = t; + } + for(b=y1; b t) MIN = t; + } + res[n][x1][y1][x2][y2] = MIN; + return MIN; +} + +int main() { + memset(sum, 0, sizeof(sum)); + memset(res, -1, sizeof(res)); + int n; + cin>>n; + for(int i=1; i<9; i++) { + for (int j =1, rowsum = 0; j < 9; j++) + { + cin>>s[i][j]; + rowsum += s[i][j]; + sum[i][j] += sum[i-1][j] + rowsum; + } + } + double result = n*fun(n,1,1,8,8) -sum[8][8] * sum[8][8]; + cout< +#include +#include +#include +using namespace std; +#pragma warning(disable: 4996) + +bool stop = false; +int i =1; +bool top_output = true; + +void printItem(int hierachy, string item) { + if(hierachy == 0 && top_output) { + cout<<"DATA SET "<< i << endl; + i++; + top_output = false; + } + + for (int i = 0; i < hierachy; i++) { + + cout << "| "; + } + cout << item << endl; +} + +void presentFile(int hierachy, string curDir) { + set Files; + string item; + while (cin>>item) + { + if(item == "]" || item == "*") { + set::iterator i; + for(i=Files.begin(); i != Files.end(); i++) { + printItem(hierachy, *i); + } + if(item == "*") { + top_output=true; + cout< -#include -#include -using namespace std; -#pragma warning(disable:4996) - -bool stop = false; - -void printItem(int hierachy, string item) -{ - for (int i = 0; i < hierachy; i++) - { - cout << "| "; - } - cout << item << endl; -} - -void presentFile(int hierachy, string curDir) -{ - set Files; - printItem(hierachy, curDir); - string item; - while (cin >> item) - { - if (item == "]" || item == "*") - { - set::iterator i; - for (i = Files.begin(); i != Files.end(); i++) - { - printItem(hierachy, *i); - } - return; - } - if (item[0] == 'f') - { - Files.insert(item); - } - else if (item[0] == 'd') - { - presentFile(hierachy + 1, item); - } - } -} - -int main() -{ - freopen("out.txt", "w", stdout); - int i = 1; - while (!stop) - { - cout << "DATA SET " << i << endl; - presentFile(0, "ROOT"); - cout << endl; - i++; - char a = cin.get(); - char b = cin.peek(); - } -} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/JimmyJump.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/JimmyJump.cpp" new file mode 100644 index 0000000..563bafc --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/JimmyJump.cpp" @@ -0,0 +1,85 @@ +#include +#include +#include +#include +using namespace std; + +#define MAX_N 100 +#define INFINITE 100000000 + +int t, n, x, y, maxHeight; +struct Platform +{ + int Lx, Rx, h; + bool operator<(const Platform &p2) const{ + return h>p2.h; + } +}; +Platform platForms[MAX_N+10]; +int leftMInTime[MAX_N+10], rightMinTime[MAX_N+10], L[MAX_N+10]; +int MinTime(int l, bool bLeft) +{ + int y = platForms[l].h; + int x; + if(bLeft) + x = platForms[l].Lx; + else + x = platForms[l].Rx; + int i; + for( i=l+1;i<=n;i++) + { + if (platForms[i].Lx<= x && platForms[i].Rx >=x) + break; + if (i<=n) + { + if( y-platForms[i].h > maxHeight) + return INFINITE; + } + else + { + if (y>maxHeight) + return INFINITE; + else + return y; + } + int nLeftTime = y - platForms[i].h + x - platForms[i].Lx; + int nRightTime = y - platForms[i].h + platForms[i].Rx -x; + if(leftMInTime[i] == -1) + { + leftMInTime[i] = MinTime(i, true); + } + if( L[i] == -1) + { + L[i] = MinTime(i, false); + } + nLeftTime += leftMInTime[i]; + nRightTime += L[i]; + if(nLeftTime < nRightTime) + return nLeftTime; + return nRightTime; + } +} + +int main() { + cin>>t; + for( int i = 0;i < t; i ++ ) { + memset(leftMInTime,-1,sizeof(leftMInTime)); + memset(L,-1,sizeof(rightMinTime)); + cin>>n>>x>>y>>maxHeight; + platForms[0].Lx = x; + platForms[0].Rx = x; + platForms[0].h = y; + for( int j = 1; j <= n; j ++ ) + cin>>platForms[j].Lx>>platForms[j].Rx>>platForms[j].h; + sort(platForms,platForms+n+1); + printf("%d\n", MinTime(0,true)); + } + return 0; +} +/* +1 +3 8 17 20 +0 10 8 +0 10 13 +4 14 3 +/***/ \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/JimmyJump_induction.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/JimmyJump_induction.cpp" new file mode 100644 index 0000000..ae0c599 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/JimmyJump_induction.cpp" @@ -0,0 +1,81 @@ +#include +#include +#include +#include +using namespace std; +#define MAX_N 100 +#define INFINITE 100000000 + +int t, n, x, y, maxHeight; +struct Platform +{ + int Lx, Rx, h; + bool operator<(const Platform &p2) const{ + return h>p2.h; + } +}; +Platform platForms[MAX_N+10]; +int leftMInTime[MAX_N+10]; //鍚勬澘瀛愪粠宸﹁蛋鐨勬渶鐭椂闂 +int rightMinTime[MAX_N+10]; //鍚勬澘瀛愪粠鍙宠蛋鐨勬渶鐭椂闂 + +int main() +{ + cin>>t; + while (t--) + { + cin>>n>>x>>y>>maxHeight; + platForms[0].Lx=x; + platForms[0].Rx=x; + platForms[0].h=y; + for(int j=1; j<=n; j++) + { + cin>>platForms[j].Lx>>platForms[j].Rx>>platForms[j].h; + } + sort(platForms, platForms+n+1); + for(int i=n; i>=0; --i) + { + int j; + for(j=i+1; j<=n; ++j) + { //鎵惧埌i鐨勫乏绔殑涓嬮潰鐨勯偅鍧楁澘瀛 + if (platForms[i].Lx<=platForms[j].Rx && platForms[i].Lx >= platForms[j].Lx) + break; + } + if(j>n) + { //鎵句笉鍒 + if(platForms[i].h > maxHeight) + leftMInTime[i] = INFINITE; + else + leftMInTime[i] = platForms[i].h; + } + else + { + int y = platForms[i].h - platForms[j].h; + if( y > maxHeight) + leftMInTime[i] = INFINITE; + else + leftMInTime[i] = y+min(leftMInTime[j]+platForms[i].Lx-platForms[j].Lx, + rightMinTime[j]+platForms[j].Rx-platForms[i].Lx ); + } + for(j=i+1;j<=n; ++j){ + //鎵惧埌i鐨勫彸绔殑涓嬮潰鐨勫憿鍧楁澘瀛 + if( platForms[i].Rx <= platForms[j].Rx && platForms[i].Rx >= platForms[j].Lx) + break; + } + if( j > n ) { + if( platForms[i].h > maxHeight ) + rightMinTime[i] = INFINITE; + else + rightMinTime[i] = platForms[i].h; + } + else { + int y = platForms[i].h - platForms[j].h; + if( y > maxHeight) rightMinTime[i] = INFINITE; + else + rightMinTime[i] = y + min(leftMInTime[j]+platForms[i].Rx-platForms[j].Lx, + rightMinTime[j]+platForms[j].Rx-platForms[i].Rx); + } + } + printf("%d\n", min(leftMInTime[0],rightMinTime[0])); + } + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/MaxSubString.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/MaxSubString.cpp" new file mode 100644 index 0000000..431647e --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/MaxSubString.cpp" @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; +char sz1[1000]; +char sz2[1000]; +int maxLen[1000][1000]; +int main() +{ + while (cin>>sz1>>sz2) + { + int length1 = strlen(sz1); + int length2 = strlen(sz2); + cout< +using namespace std; +int main() +{ + int n, s, A[10000], B[10000], aNum, bNum, num; + cin >> n; + while(n--) + { + num = 0; + cin >> s >> aNum; + for(int i = 0; i < aNum; i++) + cin >> A[i]; + cin >> bNum; + for(int i = 0; i < bNum; i++) + cin >> B[i]; + for(int i = 0; i < aNum; i++) + { + for(int j = 0; j < bNum; j++) + { + if(A[i] + B[j] == s) + num++; + } + } + cout << num << endl; + } + //system("pause"); + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/homework/skiing_matrix.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/homework/skiing_matrix.cpp" new file mode 100644 index 0000000..2d8d7f4 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week4/homework/skiing_matrix.cpp" @@ -0,0 +1,53 @@ +#include +#include +#include +#include +using namespace std; + +#define Max 100 +int map[Max][Max], length[Max][Max]; +int n=0, m=0; +int max_length=0; +int search(int a, int b); + +int main() +{ + while(cin>>n>>m) + { + max_length = 0; + for(int i=0;i>map[i][j]; + memset(length, 0, sizeof(length)); + for(int i=0; i=0 && map[a][b]> map[a-1][b]) + { + if(max=0 && map[a][b-1] < map[a][b]) + if(max < search(a,b-1)) + max = search(a, b-1); + if(b+1 map[a+1][b]) + if(max < search(a+1,b)) + max = search(a+1,b); + length[a][b] = max + 1; + if(max_length +#include +#define MAX 101 +using namespace std; +int D[MAX][MAX]; +int maxSum[MAX][MAX]; +int n; +int MaxSum(int i, int j) { + if(maxSum[i][j] != -1) + return maxSum[i][j]; + if(i == n) { + maxSum[i][j] = D[i][j]; } + else { + int x = MaxSum(i+1, j); + int y = MaxSum(i+1, j+1); + maxSum[i][j] = max(x, y) + D[i][j]; + } + return maxSum[i][j]; +} + +int main() { + int i, j; + cin>>n; + for(i=1; i<=n; i++) { + for(j=1; j<=i ; j++) { + cin>>D[i][j]; + maxSum[i][j] = -1; + } + } + cout< +#include +using namespace std; +#define MAX 101 +int D[MAX][MAX]; +int n; +int maxSum[MAX][MAX]; +int main() { + int i,j; + cin>>n; + for(i =1; i<+n; i++){ + for(j = 1; j<=i; j++){ + cin>> D[i][j];} + } + + for(int i=1;i<=n;++i) + maxSum[n][i] = D[n][i]; + for( int i=n-1; i>=1; --i) + for( int j=1; j<=i; ++j) { + maxSum[i][j] = max(maxSum[i+1][j], maxSum[i+1][j+1]) + D[i][j]; + } + cout<< maxSum[1][1]< +#include + +using namespace std; +char sz1[1000]; +char sz2[1000]; +int maxLen[1000][1000]; +int main() +{ + while (cin>>sz1>>sz2) + { + int length1 = strlen(sz1); + int length2 = strlen(sz2); + cout< +#include +#include +#include +using namespace std; +#define MAX_N 100 +#define INFINITE 100000000 + +int t, n, x, y, maxHeight; +struct Platform +{ + int Lx, Rx, h; + bool operator<(const Platform &p2) const{ + return h>p2.h; + } +}; +Platform platForms[MAX_N+10]; +int leftMInTime[MAX_N+10]; //鍚勬澘瀛愪粠宸﹁蛋鐨勬渶鐭椂闂 +int rightMinTime[MAX_N+10]; //鍚勬澘瀛愪粠鍙宠蛋鐨勬渶鐭椂闂 + +int main() +{ + cin>>t; + while (t--) + { + cin>>n>>x>>y>>maxHeight; + platForms[0].Lx=x; + platForms[0].Rx=x; + platForms[0].h=y; + for(int j=1; j<=n; j++) + { + cin>>platForms[j].Lx>>platForms[j].Rx>>platForms[j].h; + } + sort(platForms, platForms+n+1); + for(int i=n; i>=0; --i) + { + int j; + for(j=i+1; j<=n; ++j) + { //鎵惧埌i鐨勫乏绔殑涓嬮潰鐨勯偅鍧楁澘瀛 + if (platForms[j].Lx<=platForms[j].Rx && platForms[j].Lx >= platForms[j].Lx) + break; + } + if(j>n) + { //鎵句笉鍒 + if(platForms[i].h > maxHeight) + leftMInTime[i] = INFINITE; + else + leftMInTime[i] = y+min(leftMInTime[j]+platForms[i].Lx-platForms[j].Lx, + rightMinTime[j]+platForms[j].Rx-platForms[i].Lx ); + } + for(j=i+1;j<=n; ++j){ + //鎵惧埌i鐨勫彸绔殑涓嬮潰鐨勫憿鍧楁澘瀛 + if( platForms[i].Rx <= platForms[j].Rx && platForms[i].Rx >= platForms[j].Lx) + break; + } + if( j > n ) { + if( platForms[i].h > maxHeight ) + rightMinTime[i] = INFINITE; + else + rightMinTime[i] = platForms[i].h; + } + else { + int y = platForms[i].h - platForms[j].h; + if( y > maxHeight) rightMinTime[i] = INFINITE; + else + rightMinTime[i] = y + min(leftMInTime[j]+platForms[i].Rx-platForms[j].Lx, + rightMinTime[j]+platForms[j].Rx-platForms[i].Rx); + } + } + printf("%d\n", min(leftMInTime[0],rightMinTime[0])); + } + return 0; +} +``` + +### 缂栫▼缁冧範: 婊戦洩 + +鎻忚堪 + +Michael鍠滄婊戦洩鐧捐繖骞朵笉濂囨紝 鍥犱负婊戦洩鐨勭‘寰堝埡婵銆傚彲鏄负浜嗚幏寰楅熷害锛屾粦鐨勫尯鍩熷繀椤诲悜涓嬪炬枩锛岃屼笖褰撲綘婊戝埌鍧″簳锛屼綘涓嶅緱涓嶅啀娆¤蛋涓婂潯鎴栬呯瓑寰呭崌闄嶆満鏉ヨ浇浣犮侻ichael鎯崇煡閬撹浇涓涓尯鍩熶腑鏈闀跨殑婊戝潯銆傚尯鍩熺敱涓涓簩缁存暟缁勭粰鍑恒傛暟缁勭殑姣忎釜鏁板瓧浠h〃鐐圭殑楂樺害銆備笅闈㈡槸涓涓緥瀛 + +1 2 3 4 5 +16 17 18 19 6 +15 24 25 20 7 +14 23 22 21 8 +13 12 11 10 9 +涓涓汉鍙互浠庢煇涓偣婊戝悜涓婁笅宸﹀彸鐩搁偦鍥涗釜鐐逛箣涓锛屽綋涓斾粎褰撻珮搴﹀噺灏忋傚湪涓婇潰鐨勪緥瀛愪腑锛屼竴鏉″彲婊戣鐨勬粦鍧′负24-17-16-1銆傚綋鐒25-24-23-鈥-3-2-1鏇撮暱銆備簨瀹炰笂锛岃繖鏄渶闀跨殑涓鏉° + +杈撳叆 + +杈撳叆鐨勭涓琛岃〃绀哄尯鍩熺殑琛屾暟R鍜屽垪鏁癈(1 <= R,C <= 100)銆備笅闈㈡槸R琛岋紝姣忚鏈塁涓暣鏁帮紝浠h〃楂樺害h锛0<=h<=10000銆 + +杈撳嚭 + +杈撳嚭鏈闀垮尯鍩熺殑闀垮害銆 + +鏍蜂緥杈撳叆 + +5 5 +1 2 3 4 5 +16 17 18 19 6 +15 24 25 20 7 +14 23 22 21 8 +13 12 11 10 9 +鏍蜂緥杈撳嚭 + +25 + +```cpp +#include +#include +#include +#include +using namespace std; + +#define Max 100 +int map[Max][Max], length[Max][Max]; +int n=0, m=0; +int max_length=0; +int search(int a, int b); + +int main() +{ + while(cin>>n>>m) + { + max_length = 0; + for(int i=0;i>map[i][j]; + memset(length, 0, sizeof(length)); + for(int i=0; i=0 && map[a][b]> map[a-1][b]) + { + if(max=0 && map[a][b-1] < map[a][b]) + if(max < search(a,b-1)) + max = search(a, b-1); + if(b+1 map[a+1][b]) + if(max < search(a+1,b)) + max = search(a+1,b); + length[a][b] = max + 1; + if(max_length +#include +using namespace std; + +struct Block +{ + int color; + int len; +}; +struct Block segment[200]; +int score[200][200][200]; //store the computation result, avoid the repeated computation +int click_box(int start, int end, int extra_len) +{ + int i, result, temp; + if (score[start][end][extra_len] > 0) + return score[start][end][extra_len]; + result = segment[end].len + extra_len; + result = result * result; + if (start == end) + { + score[start][end][extra_len] = result; + return score[start][end][extra_len]; + } + result += click_box(start, end - 1, 0); + i = end - 1; + for (i = end - 1; i >= start; i--) + { + if (segment[i].color != segment[end].color) + continue; + temp = click_box(start, i, segment[end].len + extra_len) + click_box(i + 1, end - 1, 0); + if (temp <= result) + continue; + result = temp; + } + score[start][end][extra_len] = result; + return score[start][end][extra_len]; +} +int main() +{ + int t, n, i, j, end, color; + cin >> t; + for (i = 0; i < t; i++) + { + cin >> n; + end = 0; + cin >> segment[end].color; + segment[end].len = 1; + for (j = 1; j < n; j++) + { + cin >> color; + if (color == segment[end].color) + segment[end].len++; + else + { + end++; + segment[end].color = color; + segment[end].len = 1; + } + } + memset(score, 0, sizeof(score)); + cout << "Case " << i + 1 << ": " << click_box(0, end, 0) << endl; + } + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/Charm Bracelet.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/Charm Bracelet.cpp" new file mode 100644 index 0000000..dd24bcf --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/Charm Bracelet.cpp" @@ -0,0 +1,29 @@ + +#include +#include +#include +#include +using namespace std; +const int maxn = 12880; +int v[maxn]; +int w[maxn]; +int dp[maxn]; +int main() +{ + int n, m; + while (cin >> n >> m) + { + for (int i = 0; i < n; i++) + cin >> w[i] >> v[i]; + memset(dp, 0, sizeof(dp)); + for (int i = 0; i < n; i++) + { + for (int j = m; j >= w[i]; j--) + { + dp[j] = max(dp[j], dp[j - w[i]] + v[i]); + } + } + cout << dp[m] << endl; + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/DynamicProgramming.md" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/DynamicProgramming.md" new file mode 100644 index 0000000..cffc39a --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/DynamicProgramming.md" @@ -0,0 +1,151 @@ +## 鏈闀夸笂鍗囧瓙搴忓垪 + +[![ugvch8.md.png](https://s2.ax1x.com/2019/10/07/ugvch8.md.png)](https://imgchr.com/i/ugvch8) + +[![ugvyAP.md.png](https://s2.ax1x.com/2019/10/07/ugvyAP.md.png)](https://imgchr.com/i/ugvyAP) + +### 瑙i鎬濊矾锛 + +1. 鎵惧瓙闂锛 + + 鈥滄眰浠$a_k$锛坘=1, 2, 3鈥锛変负缁堢偣鐨勬渶闀夸笂鍗囧瓙搴忓垪鐨勯暱搴︹ + 涓涓笂鍗囧瓙搴忓垪涓渶鍙宠竟鐨勯偅涓暟锛岀О涓鸿瀛愬簭鍒楃殑鈥滅粓鐐光濄 + 铏界劧杩欎釜瀛愰棶棰樺拰鍘熼棶棰樺舰寮忎笂骞朵笉瀹屽叏涓鏍凤紝浣嗘槸鍙杩橬涓瓙闂閮借В鍐充簡锛岄偅涔堣繖N涓瓙闂鐨勮В涓紝鏈澶х殑閭d釜灏辨槸鏁翠釜闂鐨勮В銆 + +2. 纭畾鐘舵 + + 瀛愰棶棰樺彧鍜屼竴涓彉閲-- 鏁板瓧鐨勪綅缃浉鍏炽傚洜姝ゅ簭鍒椾腑鏁扮殑浣嶇疆k 灏辨槸鈥滅姸鎬佲濓紝鑰岀姸鎬 k 瀵瑰簲鐨勨滃尖濓紝灏辨槸浠 $a_k$ 鍋氫负鈥滅粓鐐光濈殑鏈闀夸笂鍗囧瓙搴忓垪鐨勯暱搴︺傜姸鎬佷竴鍏辨湁N涓 + +3. 鐘舵佽浆绉绘柟绋 + + maxLen (k)琛ㄧず浠$a_k$鍋氫负鈥滅粓鐐光濈殑鏈闀夸笂鍗囧瓙搴忓垪鐨勯暱搴﹂偅涔堬細 + 鍒濆鐘舵侊細$maxLen (1) = 1$ + $maxLen (k) = max { maxLen (i)锛1<=i < k and ai < ak涓 k鈮1 } + 1$ + 鑻ユ壘涓嶅埌杩欐牱鐨刬,鍒$maxLen(k) = 1$ + maxLen(k)鐨勫硷紝灏辨槸鍦$a_k$宸﹁竟锛屸滅粓鐐光濇暟鍊煎皬浜$a_k$ 锛屼笖闀垮害鏈澶х殑閭d釜涓婂崌瀛愬簭鍒楃殑闀垮害鍐嶅姞1銆傚洜涓篴k宸﹁竟浠讳綍鈥滅粓鐐光濆皬浜巃k鐨勫瓙搴忓垪锛屽姞涓奱k鍚庡氨鑳藉舰鎴愪竴涓洿闀跨殑涓婂崌瀛愬簭鍒椼 + +```cpp +#include +#include +#include +using namespace std; +const int MAXN=1010; +int a[MAXN]; +int maxLen[MAXN]; +int main(){ + int N; + cin>>N; + for(int i=1; i<=N; i++) + { + cin>>a[i]; + maxLen[i]=1; + } + for(int i=2; i<=N; ++i) + {//姣忔姹備互绗琲涓暟涓虹粓鐐圭殑鏈闀夸笂鍗囧瓙搴忓垪鐨勯暱搴 + for (int j = 1; j a[j]) + { + maxLen[i]=max(maxLen[i], maxLen[j]+1); + + } + } + } + cout<<*max_element(maxLen+1, maxLen+N+1); + return 0; +} +``` + +## 鏂圭洅娓告垙 + +[![ugv29S.md.png](https://s2.ax1x.com/2019/10/07/ugv29S.md.png)](https://imgchr.com/i/ugv29S) + +[![ugv6tf.md.png](https://s2.ax1x.com/2019/10/07/ugv6tf.md.png)](https://imgchr.com/i/ugv6tf) + +[![ugvr7t.md.png](https://s2.ax1x.com/2019/10/07/ugvr7t.md.png)](https://imgchr.com/i/ugvr7t) + +闂鍙互鎻忚堪涓猴細click_box(i, j, ex_len)锛岃〃绀猴細澶у潡j鐨勫凡缁忔湁浜嗛暱搴︿负ex_len鐨勫ぇ鍧楋紙璇ュぇ鍧楀彲鑳芥槸鍦ㄥ悎骞剁殑杩囩▼涓舰鎴愮殑锛夛紝涓攋棰滆壊鍜宔x_len鐩稿悓锛屽湪姝ゆ儏鍐典笅灏唅鍒癹浠ュ強ex_len閮芥秷闄ゆ墍鑳藉緱鍒扮殑鏈楂樺垎銆 + +闂杞崲涓 click_box(0, n-1, 0) + +鍦ㄦ眰瑙lick_box(i, j, ex_len锛夋椂涓鍏辨湁涓ょ鍙兘锛屽彇鏈浼樿呫傚亣璁緅鍜宔x_len鍜屽苟鍚庣殑澶у潡涓篞 + +1. 灏哘鐩存帴娑堥櫎锛岃繖绉嶅仛娉曡兘寰楀埌鐨勬渶楂樺垎涓猴細 + +鈥 click_box$(i, j-1, 0)+ (len[j] + $ex_len$)^2$ + +2. 鏈熷緟Q浠ュ悗鑳藉拰宸﹁竟鐨勬煇涓悓鑹插ぇ鍧楀悎骞躲傞渶瑕佹灇涓惧彲鑳藉拰Q鍚堝苟鐨勫ぇ鍧椼傚亣璁捐澶у潡K鍜孮鍚堝苟锛屾鏃剁殑鍒嗘暟涓: + + click_box(i, k, len[j]+ex_len) + click_box( k+1, j-1, 0) + + 鍒嗘暟涓轰袱閮ㄥ垎鏋勬垚锛屽悎骞跺悗鐨勫垎鏁帮紝浠ュ強涓轰簡鍚堝苟娑堥櫎K鍜孮涔嬮棿灏忓潡鐨勫垎鏁 + +```cpp +#include +#include +using namespace std; + +struct Block +{ + int color; + int len; +}; +struct Block segment[200]; +int score[200][200][200]; //store the computation result, avoid the repeated computation +int click_box(int start, int end, int extra_len) +{ + int i, result, temp; + if (score[start][end][extra_len] > 0) + return score[start][end][extra_len]; + result = segment[end].len + extra_len; + result = result * result; + if (start == end) + { + score[start][end][extra_len] = result; + return score[start][end][extra_len]; + } + result += click_box(start, end - 1, 0); + i = end - 1; + for (i = end - 1; i >= start; i--) + { + if (segment[i].color != segment[end].color) + continue; + temp = click_box(start, i, segment[end].len + extra_len) + click_box(i + 1, end - 1, 0); + if (temp <= result) + continue; + result = temp; + } + score[start][end][extra_len] = result; + return score[start][end][extra_len]; +} +int main() +{ + int t, n, i, j, end, color; + cin >> t; + for (i = 0; i < t; i++) + { + cin >> n; + end = 0; + cin >> segment[end].color; + segment[end].len = 1; + for (j = 1; j < n; j++) + { + cin >> color; + if (color == segment[end].color) + segment[end].len++; + else + { + end++; + segment[end].color = color; + segment[end].len = 1; + } + } + memset(score, 0, sizeof(score)); + cout << "Case " << i + 1 << ": " << click_box(0, end, 0) << endl; + } + return 0; +} +``` + + + diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/DynamicProgramming_2.md" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/DynamicProgramming_2.md" new file mode 100644 index 0000000..e69de29 diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/UNIMODAL PALINDROMIC DECOMPOSITIONS.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/UNIMODAL PALINDROMIC DECOMPOSITIONS.cpp" new file mode 100644 index 0000000..588a5ea --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/UNIMODAL PALINDROMIC DECOMPOSITIONS.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include +using namespace std; + +unsigned int dp[250][250]; + +class Solution +{ +public: + unsigned int fun(int n) + { + //鍒濆鍖栨暟鎹, 杈圭晫鏉′欢 + for (int num = 0; num < 250; num++) + { + for (int k = 0; k < 250; k++) + { + if (num == 0) + { + dp[num][k] = 1; + } + else if (k > num) + { + dp[num][k] = 0; + } + else + { + dp[num][k] = 1; + } + } + } + //鍔ㄦ佽鍒 + for (int num = 2; num <= n; num++) + { + for (int k = num / 2; k >= 1; k--) + { + dp[num][k] = dp[num - 2 * k][k] + dp[num][k + 1]; + } + } + return dp[n][1]; + } +}; + +int main() +{ + Solution sol; + int n; + cin >> n; + while (n != 0) + { + cout << n << ' ' << sol.fun(n) << endl; + cin >> n; + } + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/maxLen.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/maxLen.cpp" new file mode 100644 index 0000000..6a74238 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/maxLen.cpp" @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; +const int MAXN=1010; +int a[MAXN]; +int maxLen[MAXN]; +int main(){ + int N; + cin>>N; + for(int i=1; i<=N; i++) + { + cin>>a[i]; + maxLen[i]=1; + } + for(int i=2; i<=N; ++i) + {//姣忔姹備互绗琲涓暟涓虹粓鐐圭殑鏈闀夸笂鍗囧瓙搴忓垪鐨勯暱搴 + for (int j = 1; j a[j]) + { + maxLen[i]=max(maxLen[i], maxLen[j]+1); + + } + } + } + cout<<*max_element(maxLen+1, maxLen+N+1); + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/maxLen2.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/maxLen2.cpp" new file mode 100644 index 0000000..5e222a4 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week5/maxLen2.cpp" @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; +const int MAXN=1010; +int a[MAXN]; +int maxLen[MAXN]; +int main(){ + int N; + cin>>N; + for(int i=1; i<=N; i++) + { + cin>>a[i]; + maxLen[i]=1; + } + for(int i=2; i<=N; ++i) + {//姣忔姹備互绗琲涓暟涓虹粓鐐圭殑鏈闀夸笂鍗囧瓙搴忓垪鐨勯暱搴 + for(int j= i+1; j<=N; ++j) + { + if(a[j]>a[i]) + maxLen[j]=max(maxLen[j], maxLen[i]+1); + } + } + cout<<*max_element(maxLen+1, maxLen+N+1); + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/DfS_CastleProblem.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/DfS_CastleProblem.cpp" new file mode 100644 index 0000000..14ef5d4 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/DfS_CastleProblem.cpp" @@ -0,0 +1,67 @@ +#include +#include +#include +using namespace std; +int R, C; //the number of row/col +int rooms[60][60]; +int color[60][60]; //marker whether a room has been searched +int maxRoomArea = 0, roomNum = 0; +int roomArea; +void Dfs(int i, int k) +{ + if (color[i][k]) + return; + ++roomArea; + color[i][k] = roomNum; + if (rooms[i][k] & 1 == 0) + Dfs(i, k - 1); // go west + if (rooms[i][k] & 2 == 0) + Dfs(i - 1, k); //go north + if (rooms[i][k] & 4 == 0) + Dfs(i, k + 1); + if (rooms[i][k] & 8 == 0) + Dfs(i + 1, k); //go south +} +int main() +{ + cin >> R >> C; + for (int i = 1; i <= R; ++i) + for (int k = 1; k <= C; ++k) + cin >> rooms[i][k]; + memset(color, 0, sizeof(color)); + for (int i = 1; i <= R; ++i) + for (int k = 1; k <= C; ++k) + { + if (!color[i][k]) + { + ++roomNum; + roomArea = 0; + Dfs(i, k); + maxRoomArea = max(roomArea, maxRoomArea); + } + } + cout << roomNum << endl; + cout << maxRoomArea << endl; + return 0; +} +/* + Input: + First row: row numbers R + Second row: col number C + R rows: The walls sourrding a room + 1 stands for west wall, 2 for north wall + 4 for east wall, 8 for south wall + the sum stands for one cell + */ + +/* +4 +7 +11 6 11 6 3 10 6 +7 9 6 13 5 15 5 +1 10 12 7 13 7 5 +13 11 10 8 10 12 13 +Output: +5 +9 + */ \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/DfS_CastleProblem_Stack.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/DfS_CastleProblem_Stack.cpp" new file mode 100644 index 0000000..a504c31 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/DfS_CastleProblem_Stack.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include +using namespace std; +int R, C; //the number of row/col +int rooms[60][60]; +int color[60][60]; //marker whether a room has been searched +int maxRoomArea = 0, roomNum = 0; +int roomArea; +void Dfs(int r, int c) +{ + struct Room + { + int r, c; + Room(int rr, int cc) : r(rr), c(cc) {} + }; + stack stk; + stk.push(Room(r, c)); + while (!stk.empty()) + { + Room rm = stk.top(); + int i = rm.r; + int k = rm.r; + if (color[i][k]) + stk.pop(); + else + { + ++roomArea; + color[i][k] = roomNum; + if ((rooms[i][k] & 1) == 0) + stk.push(Room(i, k - 1)); + if ((rooms[i][k] & 2) == 0) + stk.push(Room(i - 1, k)); + if ((rooms[i][k] & 4) == 0) + stk.push(Room(i, k + 1)); + if ((rooms[i][k] & 8) == 0) + stk.push(Room(i + 1, k)); + } + } +} +int main() +{ + cin >> R >> C; + for (int i = 1; i <= R; ++i) + for (int k = 1; k <= C; ++k) + cin >> rooms[i][k]; + memset(color, 0, sizeof(color)); + for (int i = 1; i <= R; ++i) + for (int k = 1; k <= C; ++k) + { + if (!color[i][k]) + { + ++roomNum; + roomArea = 0; + Dfs(i, k); + maxRoomArea = max(roomArea, maxRoomArea); + } + } + cout << roomNum << endl; + cout << maxRoomArea << endl; + return 0; +} +/* + Input: + First row: row numbers R + Second row: col number C + R rows: The walls sourrding a room + 1 stands for west wall, 2 for north wall + 4 for east wall, 8 for south wall + the sum stands for one cell + */ + +/* +4 +7 +11 6 11 6 3 10 6 +7 9 6 13 5 15 5 +1 10 12 7 13 7 5 +13 11 10 8 10 12 13 +Output: +5 +9 + */ \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/Dfs_PathSearch.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/Dfs_PathSearch.cpp" new file mode 100644 index 0000000..fd354f5 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/Dfs_PathSearch.cpp" @@ -0,0 +1,92 @@ +/* +N citys, R one-way road between each two citys +Every Road connects two citys, and has property of length and cost +Bob has money K, and wants to travel from 1 to City N +Search: the minimum path length +2<=N<=100 +0<=K<=10000 +1<=R<=10000 + +Input: +K +N +R +s1 e1 L1 T1 +s2 e2 L2 T2 +... +sR eR LE TR +*/ +#include +#include +#include +using namespace std; +int K, N, R, S, D, L, T; +struct Road +{ + int d, L, t; +}; +vector> cityMap(110); //Construct an Adjacency List +//cityMap[i] represents the city set that connected to point i with a road +int minLen = 1 << 30; // bit shift to get a big number +int totalLen; // the current path length +int totalCost; +int visited[110]; // Marker that a city is looped +int minL[110][10100]; //minL[i][j]: from 1 to point i, the minimum path length with cost of j +void Dfs(int s) //start from s to N +{ + if (s == N) + { + minLen = min(minLen, totalLen); + return; + } + for (int i = 0; i < cityMap[s].size(); i++) + { + int d = cityMap[s][i].d; + // exists one road connecting s and d + if (!visited[d]) + { + int cost = totalCost + cityMap[s][i].t; + if (cost > K) + continue; //the cost is beyond the afford + if (totalLen + cityMap[s][i].L >= minLen || totalLen + cityMap[s][i].L >= minL[d][cost]) + continue; + // the length is already not optimum + totalLen += cityMap[s][i].L; + totalCost += cityMap[s][i].t; + minL[d][cost] = totalLen; + visited[d] = 1; + Dfs(d); + visited[d] = 0; + totalCost -= cityMap[s][i].t; + totalLen -= cityMap[s][i].L; + } + } +} +int main() +{ + cin >> K >> N >> R; + for (int i = 0; i < R; i++) + { + int s; + Road r; + cin >> s >> r.d >> r.L >> r.t; + if (s != r.d) + cityMap[s].push_back(r); + } + for (int i = 0; i < 110; i++) + for (int j = 0; j < 10100; j++) + minL[i][j] 1 << 30; + memset(visited, 0, sizeof(visited)); + totalLen = 0; + totalLen = 0; + visited[1] = 1; + minLen = 1 << 30; + Dfs(1); + if (minLen < (1 << 30)) + cout << minLen << endl; + else + { + cout << "-1" << endl; + } + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/Dfs_Sticks.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/Dfs_Sticks.cpp" new file mode 100644 index 0000000..0064826 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/Dfs_Sticks.cpp" @@ -0,0 +1,104 @@ +/* +Description + +George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero. +Input + +The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero. +Output + +The output should contains the smallest possible length of original sticks, one per line. +Sample Input + +9 +5 2 1 5 2 1 5 2 1 +4 +1 2 3 4 +0 +Sample Output + +6 +5*/ +#include +#include +#include +#include +#include +using namespace std; +vector len(65); +int used[65]; //Marker whether is used +int L; //proposal of the length of the stick +int n; //proposal of the number of the stick + +/*when rest stick parts left, and current stick still need needLen to achieve the goal length*/ +// in such case, whether the search has a result +bool dfs(int rest, int needLen, int lastIndex) +{ + if (rest == 0 && needLen == 0) + return true; + if (needLen == 0) + // one stick is completed + needLen = L; + //Pruning 4: multi sticks part to construct one complete strick, and do it in an order (long to short) + // No need to search the case that the current stick part is langer than the former, + //since if that case could work, it just means a different order + int startIndex = needLen == L ? 0 : lastIndex + 1; + for (int i = startIndex; i < n; i++) + { + //Pruning 1: we do not try the same length at the same case + if (i > 0 && !used[i - 1] && len[i - 1] == len[i]) + continue; + if (!used[i] && len[i] <= needLen) + { + used[i] = 1; + if (dfs(rest - 1, needLen - len[i], i)) + return true; + else + { + // this trial failed, and switch to next part + used[i] = 0; + // Pruning 2:when i as the first part and cannot work, then this part + //could never be used, thus the length proposal is dropped. + if (needLen == L || + //Pruning 3: if len[i] is the last part, + needLen == len[i]) + return false; + } + } + } + return false; +} +int main() +{ + while (cin>>n && n>0) + { + len.clear(); + int minLen = 0; + int totalLen = 0; + int tempLen; + for(int i = 0;i> tempLen; + len.push_back(tempLen); + totalLen += tempLen; + } + // sort the stick parts + sort(len.begin(), len.end(), greater()); + for( L =len[0]; L<= totalLen/2; L++) + { + if(totalLen%L) + continue; + memset(used, 0, sizeof(used)); + if(dfs(n,L,-1)) + { + minLen = L; + break; + } + } + if(L>totalLen/2) + { + minLen = totalLen; + } + cout< +#include +#include +using namespace std; +int R, C; //the number of row/col +int rooms[60][60]; +int color[60][60]; //marker whether a room has been searched +int maxRoomArea = 0, roomNum = 0; +int roomArea; +void Dfs(int i, int k) +{ + if (color[i][k]) + return; + ++roomArea; + color[i][k] = roomNum; + if (rooms[i][k] & 1 == 0) + Dfs(i, k - 1); // go west + if (rooms[i][k] & 2 == 0) + Dfs(i - 1, k); //go north + if (rooms[i][k] & 4 == 0) + Dfs(i, k + 1); + if (rooms[i][k] & 8 == 0) + Dfs(i + 1, k); //go south +} +int main() +{ + cin >> R >> C; + for (int i = 1; i <= R; ++i) + for (int k = 1; k <= C; ++k) + cin >> rooms[i][k]; + memset(color, 0, sizeof(color)); + for (int i = 1; i <= R; ++i) + for (int k = 1; k <= C; ++k) + { + if (!color[i][k]) + { + ++roomNum; + roomArea = 0; + Dfs(i, k); + maxRoomArea = max(roomArea, maxRoomArea); + } + } + cout << roomNum << endl; + cout << maxRoomArea << endl; + return 0; +} +/* + Input: + First row: row numbers R + Second row: col number C + R rows: The walls sourrding a room + 1 stands for west wall, 2 for north wall + 4 for east wall, 8 for south wall + the sum stands for one cell + */ + +/* +4 +7 +11 6 11 6 3 10 6 +7 9 6 13 5 15 5 +1 10 12 7 13 7 5 +13 11 10 8 10 12 13 +Output: +5 +9 + */ +``` + +### 渚嬮浜岋細瀵昏矾闂 + + N涓煄甯傦紝缂栧彿1鍒癗銆傚煄甯傞棿鏈塕鏉″崟鍚戦亾璺 姣忔潯閬撹矾杩炴帴涓や釜鍩庡競锛屾湁闀垮害鍜岃繃璺垂涓や釜灞炴с Bob鍙湁K鍧楅挶锛屼粬鎯充粠鍩庡競1璧板埌鍩庡競N銆傞棶鏈鐭叡闇瑕佽蛋澶氶暱鐨勮矾銆傚鏋滃埌涓嶄簡N锛岃緭 鍑-1 銆 + +#### 瑙i鎬濊矾 + +浠庡煄甯1寮濮嬫繁搴︿紭鍏堥亶鍘嗘暣涓浘锛屾壘鍒版墍鏈夎兘鍒拌揪N鐨勮蛋娉曪紝骞堕夋嫨涓涓渶浼樼殑銆 + +浼樺寲锛 + +1. 濡傛灉褰撳墠宸茬粡鎵惧埌鐨勬渶浼樿矾寰勯暱搴︿负L ,閭d箞鍦ㄧ户缁悳绱㈢殑杩囩▼涓紝鎬婚暱搴﹀凡缁忓ぇ 浜嶭鐨勮蛋娉曪紝灏卞彲浠ョ洿鎺ユ斁寮冿紝涓嶇敤璧板埌搴曚簡 +2. 鐢$midL[ k][m]$ 琛ㄧず锛氳蛋鍒板煄甯俴鏃舵昏繃璺垂涓簃鐨勬潯浠朵笅锛屾渶浼樿矾寰勭殑闀垮害銆傝嫢鍦 鍚庣画鐨勬悳绱腑锛屽啀娆¤蛋鍒発鏃讹紝濡傛灉鎬昏矾璐规伆濂戒负m锛屼笖姝ゆ椂鐨勮矾寰勯暱搴﹀凡缁忚秴杩$ midL[k][m]$,鍒欎笉蹇呭啀璧颁笅鍘讳簡銆 + +```cpp +/* +N citys, R one-way road between each two citys +Every Road connects two citys, and has property of length and cost +Bob has money K, and wants to travel from 1 to City N +Search: the minimum path length +2<=N<=100 +0<=K<=10000 +1<=R<=10000 + +Input: +K +N +R +s1 e1 L1 T1 +s2 e2 L2 T2 +... +sR eR LE TR +*/ +#include +#include +#include +using namespace std; +int K, N, R, S, D, L, T; +struct Road +{ + int d, L, t; +}; +vector> cityMap(110); //Construct an Adjacency List +//cityMap[i] represents the city set that connected to point i with a road +int minLen = 1 << 30; // bit shift to get a big number +int totalLen; // the current path length +int totalCost; +int visited[110]; // Marker that a city is looped +int minL[110][10100]; //minL[i][j]: from 1 to point i, the minimum path length with cost of j +void Dfs(int s) //start from s to N +{ + if (s == N) + { + minLen = min(minLen, totalLen); + return; + } + for (int i = 0; i < cityMap[s].size(); i++) + { + int d = cityMap[s][i].d; + // exists one road connecting s and d + if (!visited[d]) + { + int cost = totalCost + cityMap[s][i].t; + if (cost > K) + continue; //the cost is beyond the afford + if (totalLen + cityMap[s][i].L >= minLen || totalLen + cityMap[s][i].L >= minL[d][cost]) + continue; + // the length is already not optimum + totalLen += cityMap[s][i].L; + totalCost += cityMap[s][i].t; + minL[d][cost] = totalLen; + visited[d] = 1; + Dfs(d); + visited[d] = 0; + totalCost -= cityMap[s][i].t; + totalLen -= cityMap[s][i].L; + } + } +} +int main() +{ + cin >> K >> N >> R; + for (int i = 0; i < R; i++) + { + int s; + Road r; + cin >> s >> r.d >> r.L >> r.t; + if (s != r.d) + cityMap[s].push_back(r); + } + for (int i = 0; i < 110; i++) + for (int j = 0; j < 10100; j++) + minL[i][j] 1 << 30; + memset(visited, 0, sizeof(visited)); + totalLen = 0; + totalLen = 0; + visited[1] = 1; + minLen = 1 << 30; + Dfs(1); + if (minLen < (1 << 30)) + cout << minLen << endl; + else + { + cout << "-1" << endl; + } + return 0; +} +``` + +### 娣辨悳涓庡壀鏋 + +鍏蜂綋鐨勪緥棰樹笉鏀惧湪杩欓噷浜嗭紝璇峰弬鑰冿細 + + http://www.voidcn.com/article/p-oxclnehu-qh.html + +浠呬粎鏀句笂浠庤繖閬撻搴旇瀛﹀埌鐨勭粡楠岋細 + +1. 瑕侀夋嫨绠鍚堥傜殑鎼滅储椤哄簭锛屽鏋滀竴涓汉鐗╁垎鎴愬姝ワ紝瑕佷紭鍏堝皾璇曞彲鑳芥у皯鐨勩 锛堜紭鍏堝皾璇曢暱鐨勬湪妫掞級 + +2. 瑕佸彂鐜拌〃闈笂鐨勪笉鍚岋紝瀹炶川涓婄瓑鏁堢殑閲嶅鐘舵侊紝閬垮厤閲嶅銆 + +3. 鏍规嵁瀹為檯闂锛屽彂鐜板壀鏋濇柟妗堛 \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/assignment/A_Knights_Journey.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/assignment/A_Knights_Journey.cpp" new file mode 100644 index 0000000..28bd461 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week6/assignment/A_Knights_Journey.cpp" @@ -0,0 +1,62 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +using namespace std; +int path[88][2], book[88][88], p, q; +bool flag; +int dx[8] = { -1, 1, -2, 2, -2, 2, -1, 1 }; +int dy[8] = { -2, -2, -1, -1, 1, 1, 2, 2 }; + +void DFS(int r, int c, int step) +{ + path[step][0] = r; + path[step][1] = c; + if (step == p * q) + { + flag = true; + return; + } + for (int i = 0; i < 8; i++) + { + int nx = r + dx[i]; + int ny = c + dy[i]; + if (nx >= 1 && ny >= 1 && nx <= p && ny <= q && !book[nx][ny] && !flag) + { + book[nx][ny] = 1; + DFS(nx, ny, step + 1); + book[nx][ny] = 0; + } + } +} +int main() +{ + //freopen("in.txt", "r", stdin); + int case_number, cas_ = 0; + cin >> case_number; + while (case_number--) + { + flag = 0; + cin >> p >> q; + memset(book, 0, sizeof(book)); + book[1][1] = 1; //start from (1,1); + DFS(1, 1, 1); + cout << "Scenario #" << ++cas_ <<":" <0) + cout< +#include +using namespace std; +int n, k, total, m; +/*n: the col number +* k: the cheese pieces number +* m: the number of the pieces that already in +*/ +int book[10]; //record whether one col has been used +char a[10][10]; //record the cheeseboard + +void DFS(int current) +{ + if (k == m) + { + // all the pieces have been put in + total++; + return; + } + if (current > n) + return; //beyond the board + for (int i = 0; i < n; i++) + if (book[i] == 0 && a[current][i] == '#') + { + book[i] = 1; + m++; + DFS(current + 1); + book[i] = 0; + m--; + } + DFS(current + 1); +} +int main() +{ + int i, j; + while (cin >> n >> k && (n != -1 && k != -1)) + { + total = 0; + m = 0; + for (i = 0; i < n; i++) + { + //getchar(); + for (j = 0; j < n; j++) + cin >> a[i][j]; + } + memset(book, 0, sizeof(book)); + DFS(0); + cout << total << endl; + } + return 0; +} +/* +*Sample Input: +2 1 +#. +.# +4 4 +...# +..#. +.#.. +#... +-1 -1 +* Sample Output: +2 +1 + +*/ \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/assignment/shredding_company.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/assignment/shredding_company.cpp" new file mode 100644 index 0000000..ef10959 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/assignment/shredding_company.cpp" @@ -0,0 +1,79 @@ +#include +#include +using namespace std; +#define LL long long +#define MAX 310 + +int T, N; +int MIN; +int flag; +int number[10], son[10], ans[10], ans_num; + +void Dfs(int bit, int sum, int num) +{ //bit琛ㄧず褰撳墠杩樺墿浣欑殑浣嶆暟锛宻um褰撳墠鐨勭疮绉拰,num鏄綋鍓嶇殑娣卞害 + if (sum > T) + return; //鏃犳硶瑁佸壀锛岀洿鎺ヨ繑鍥 + if (bit == 0) + { //bit涓0锛岃鏄庤鍓畬鎴愶紝寮濮嬪垽瀹氭槸鍚︽槸鏈浼樿В + if (T - sum == MIN) + { + flag = -1; + return; //鏈夊涓渶浼樿В锛屾墦鍗板紓甯 + } + if (T - sum > MIN) + return; //涓嶆槸鏈浼樿В锛屽壀鏋濊繑鍥 + for (int i = 0; i < num; i++) + ans[i] = son[i]; //鐩稿鏈浼樿В鐨勫瓨鍌紝灏嗚鍓殑姣忎竴娈电焊鐨勬暟鎹瓨鍌ㄥ湪鐩稿浼樿В涓 + MIN = T - sum; //鏇存柊鏈浼樿В + ans_num = num; //鏇存柊鏈娓歌鐨勬繁搴︼紝 鍗宠鍓殑绾哥殑浠芥暟 + flag = 1; // flag = 1 琛ㄧず瀛樺湪涓涓В + return; //缁撴潫杩欎釜鎼滅储 + } + int temp = 0; + for (; bit > 0; bit--) + { //鎼滅储鏍稿績锛岀粰鍑哄墿浣欑焊寮犵殑浣嶆暟锛屾ā鎷熷壀瑁 + temp = temp * 10 + number[bit]; //鐢╰emp鏉ユā鎷熸墍鏈夊彲鑳界殑瑁佸壀鏂瑰紡 + son[num] = temp; //son鏁扮粍璁板綍浜嗗綋鍓嶈鍓柟娉曚腑鐨勬瘡涓娈电焊鐨勫 + Dfs(bit - 1, sum + temp, num + 1); //瀵瑰墿浣欑殑鐭ヨ繘琛屼笅涓娆℃悳绱紝绾稿紶浠芥暟鍔1 + } +} +int main() +{ + while (cin >> T >> N) + { + if (T == 0 && N == 0) + break; + if (T == N) + { + cout << T << " " << N << endl; + continue; + } + int num = 0; + MIN = 1 << 30; + flag = 0; + while (N) + { + number[++num] = N % 10; + N /= 10; + } + Dfs(num, 0, 0); + if (flag == 0) + { + cout << "error" << endl; + } + else + { + if (flag == -1) + cout << "rejected" << endl; + else + { + cout << T - MIN << " "; + for (int i = 0; i < ans_num - 1; i++) + cout << ans[i] << " "; + cout << ans[ans_num - 1] << endl; + } + } + } + return 0; +} +//references: https://blog.csdn.net/iwts_24/article/details/79728793 \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/birthdayCake.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/birthdayCake.cpp" new file mode 100644 index 0000000..12fa4e7 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/birthdayCake.cpp" @@ -0,0 +1,89 @@ +#include +#include +#include +#include +using namespace std; +int N, M; +int minArea = 1 << 30; +int area = 0; //姝e湪鎼缓涓殑铔嬬硶鐨勮〃闈㈢Н +int minV[30]; //minV[n]琛ㄧずn灞傝泲绯曟渶灏戠殑浣撶Н +int minA[30]; //minA[n]琛ㄧずn灞傝泲绯曠殑鏈灏戜晶闈㈢Н +int MaxVforNRH(int n, int r, int h) +{ //姹傚湪n灞傝泲绯曪紝搴曞眰鏈澶у崐寰剅锛屾渶楂橀珮搴鐨勬儏鍐典笅锛岃兘鍑戝嚭鏉ョ殑鏈澶т綋绉 + int v = 0; + for (int i = 0; i < n; i++) + { + v += (r - i) * (r - i) * (h - i); + } + + return v; +} +void Dfs(int v, int n, int max_r, int max_h) +{ + //瑕佺敤n灞傚幓鍑戜綋绉痸锛屾渶搴曞眰鍗婂緞涓嶈兘瓒呰繃r 楂樺害涓嶈兘瓒呰繃h + //姹傚嚭鏈灏忚〃闈㈢Н鏀惧叆minArea + if (n == 0) + { + if (v) + return; + else + { + minArea = min(minArea, area); + return; + } + } + if (v <= 0) + return; + if (minV[n] > v) //鍓灊3锛 杩樻病鎼缓鐨勪綋绉紝涓瀹氫細瓒呰繃杩樼己鐨勪綋绉 + return; + if (area + minA[n] >= minArea) //鍓灊1 + return; + if (max_h < n || max_r < n) //鍓灊2 + // 鎼缓杩囩▼涓瑙佸埌鍐嶅線涓婃惌寤猴紝楂樺害鎴栬呭崐寰勫凡缁忔棤娉曞畨鎺 + return; + if (MaxVforNRH(n, max_r, max_h) < v)//鍓灊4锛 杩樻病鎼缓鐨勪綋绉紝鏈澶т篃鍒颁笉浜嗚繕缂虹殑浣撶Н + return; + for (int rr = max_r; rr >= n; --rr) + { + if (n == M) + area = rr * rr; //搴曢潰绉 + for (int hh = max_h; hh >= n; --hh) + //浠庡ぇ鍒板皬姣旇緝蹇備粠澶х殑寮濮嬶紝鍐呴儴鐨勫惊鐜彲閫夋嫨鑼冨洿灏辫緝灏戯紝绗﹀悎鍏堝仛姝ラ灏戠殑鎬濊矾 + { + area += 2 * rr * hh; + Dfs(v - rr * rr * hh, n - 1, rr - 1, hh - 1); + area -= 2 * rr * hh; + } + } +} +int main() +{ + cin >> M >> N; + // M 灞傝泲绯 浣撶Н涓篘 + minV[0] = 0; + minA[0] = 0; + for (int i = 0; i <= M; i++) + { + minV[i] = minV[i - 1] + i * i * i; + minA[i] = minA[i - 1] + 2 * i * i; + //绗琲灞傚崐寰勬渶灏戜负i 楂樺害鑷冲皯涓篿 + } + if (minV[M] < N) + cout << 0 << endl; + else + { + int maxH = (N - minV[M - 1]) / (M * M) + 1; //搴曢儴鏈澶ч珮搴 + //鏈搴曞眰浣撶Н涓嶈秴杩 (N-minV[M-1])锛屼笖鍗婂緞鏈灏戜负M + int maxR = sqrt(double(N - minV[M - 1]) / M) + 1; + //搴曞眰楂樺害鏈灏戜负M + area = 0; + minArea = 1 << 30; + Dfs(N, M, maxR, maxH); + if (minArea == 1 << 30) + cout << 0 << endl; + else + cout << minArea << endl; + } + + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/sudoku.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/sudoku.cpp" new file mode 100644 index 0000000..bc4e6d8 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week7/sudoku.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include +#include +using namespace std; +short rowFlags[9][10]; // rowFlags[i][num]=1 琛ㄧず鍦ㄧi琛 宸茬粡瀛樻斁浜嗘暟瀛梟um +short colFlags[9][10]; // colFlags[i][num]=1 琛ㄧず鍦ㄧj鍒 宸茬粡瀛樻斁浜嗘暟瀛梟um +short blockFlags[9][10]; //blockFlag[i][num]=1 琛ㄧず鍦ㄧi涓潡宸茬粡瀛樻斁浜嗘暟瀛梟um +int board[9][9]; //鏁翠釜妫嬬洏 +struct Pos +{ + int r, c; + Pos(int rr, int cc) : r(rr), c(cc) {} +}; +vector blockPos; //鎵鏈夌┖鐧芥牸鐨勪綅缃 +inline int GetBlockNum(int r, int c) +{ //鐢辫锛屽垪鍙锋眰灏忓潡鍙 + int rr = r / 3; + int cc = c / 3; + return rr * 3 + cc; +} +void SetAllFlags(int i, int j, int num, int f) +{ + //鎶妌um鏀惧湪锛坕锛宩锛変綅缃紝璁剧疆鐩稿簲鏍囪锛屾垨浠庯紙i锛宩锛夊彇璧帮紝娑堥櫎鐩稿簲鏍囪 + rowFlags[i][num] = f; + colFlags[j][num] = f; + blockFlags[GetBlockNum(i, j)][num] = f; +} +bool IsOk(int i, int j, int num) +{ + return !rowFlags[i][num] && !colFlags[j][num] && + !blockFlags[(GetBlockNum(i, j))][num]; +} +bool Dfs(int n) +{ + //澶勭悊鍓峮涓┖鏍 + if (n < 0) + return true; + int r = blockPos[n].r; + int c = blockPos[n].c; + for (int num = 1; num < 9; num++) + { + if (IsOk(r, c, num)) + { + board[r][c] = num; + SetAllFlags(r, c, num, 1); + if (Dfs(n - 1)) + return true; + SetAllFlags(r, c, num, 0); + } + } + return false; +} +int main() +{ + int t; + cin >> t; + while (t--) + { + memset(rowFlags, 0, sizeof(rowFlags)); + memset(colFlags, 0, sizeof(colFlags)); + memset(blockFlags, 0, sizeof(blockFlags)); + blockPos.clear(); + for (int i = 0; i < 9; i++) + for (int j = 0; j < 9; j++) + { + cin >> board[i][j]; + if (board[i][j]) + SetAllFlags(i, j, board[i][j], 1); + else + blockPos.push_back(Pos(i, j)); + } + if (Dfs(blockPos.size() - 1)) + { + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + cout << char(board[i][j] + '0'); + cout << endl; + } + } + } +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/8-puzzle-problem.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/8-puzzle-problem.cpp" new file mode 100644 index 0000000..4a066d8 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/8-puzzle-problem.cpp" @@ -0,0 +1,240 @@ +#include +#include +#include +using namespace std; +int goalStatus; //鐩爣鐘舵 +bitset<362880> Flags; //鑺傜偣鏄惁琚墿灞曠殑鏍囪浣 +const int MAXS = 400000; +char result[MAXS]; //缁撴灉瀛樺偍 +struct Node +{ + int status; //鐘舵侊紝鍗虫帓鍒楃殑缂栧彿 + int father; //鐖惰妭鐐圭殑鎸囬拡 + char move; //鐖惰妭鐐瑰埌鏈妭鐐圭殑绉诲姩鏂瑰紡 u/d/r/l + Node(int s, int f, char m) : status(s), father(f), move(m) {} + Node() {} +}; +Node myQueue[MAXS]; //闃熷垪鐘舵併傜姸鎬佹绘暟362880 +int qHead, qTail; //瀵瑰ご鎸囬拡鍜岄槦灏炬寚閽 +char sz4Moves[] = "urdl"; //鍥涚鍔ㄤ綔 +unsigned int factorial[21]; //瀛樻斁0-20鐨勯樁涔 +unsigned int GetPermutationNumForInt(int *perInt, int len) +//perInt閲屾斁鐫鏁存暟0鍒發en-1鐨勪竴涓帓鍒楋紝姹備粬鏄鍑犱釜鎺掑垪 +//len鐨勯暱搴︿笉鑳借秴杩21 +{ + unsigned int num = 0; + bool used[21]; + memset(used, 0, sizeof(bool) * len); + + for (int i = 0; i < len; i++) + { + unsigned int n = 0; + for (int j = 0; j < perInt[i]; ++j) + { + if (!used[j]) + ++n; + } + num += n * factorial[len - i - 1]; + used[perInt[i]] = true; + } + return num; +} + +template +unsigned int GetPermutationNum(T s1, T s2, int len) +//缁欏畾鎺掑垪锛屾眰搴忓彿銆 +//[s1, s1+len)閲岄潰鏀剧潃绗0鍙峰簭鍒楋紝[s2, s2+len)閲岄潰瀛樻斁鐫瑕佹眰搴忓彿鐨勬帓鍒 +//涓よ呭繀椤讳竴鏍烽暱锛宭en涓嶈兘瓒呰繃21 +//鎺掑垪鐨勬瘡涓厓绱犻兘涓嶄竴鏍凤紝杩斿洖鎺掑垪鐨勫簭鍙 +{ + int perInt[21]; //瑕佽浆鎹㈡垚[0, len-1]鐨勬暣鏁扮殑鎺掑垪 + for (int i = 0; i < len; i++) + for (int j = 0; j < len; j++) + { + if (*(s2 + 1) == *(s1 + 1)) + { + perInt[i] = j; + break; + } + } + unsigned int num = GetPermutationNumForInt(perInt, len); + return num; +} + +template +void GenPermutationByNum(T s1, T s2, int len, unsigned int No) +//鏍规嵁鎺掑垪缂栧彿锛岀敓鎴愭帓鍒楋紝len涓嶈秴杩21 +//[s1, s1+len)閲岄潰瀛樻斁鐫绗0鍙穚ermutation,鎺掑垪鐨勬瘡涓厓绱犻兘涓嶄竴鏍 +{ + int perInt[21]; //闇瑕佽浆鎹负[0锛宭en-1]鐨勬暣鏁扮殑鎺掑垪 + bool used[21]; + memset(used, 0, sizeof(bool) 锛妉en); + for (int i = 0; i < len; i++) + { + unsigned int tmp; + int n = 0; + int j; + for (j = 0; j < len; j++) + { + if (!used[j]) + { + if (factorial[len - 1 - i] >= No + 1) + break; + else + No -= factorial[len - i - 1]; + } + } + perInt[i] = j; + used[j] = true; + } + for (int i = 0; i < len; i++) + *(s2 + i) = *(s1 + perInt[i]); +} +int StrStatusToIntStatus(const char *strStatus) +{ //瀛楃涓插舰寮忕殑鐘舵侊紝杞崲涓烘暣鏁板舰寮忕殑鐘舵(鎺掑垪搴忓彿锛 + return GetPermutationNum("012345678", strStatus, 9); +} +void IntStatusToStrStatus(int n, char *strStatus) +{ //鏁存暟褰㈠紡鐨勭姸鎬(鎺掑垪搴忓彿锛夛紝杞崲涓哄瓧绗︿覆褰㈠紡鐨勭姸鎬 + GenPermutationByNum((char *)"012345678", strStatus, 9, n); +} +int NewStatus(int nStatus, char cMove) +{ + //姹備粠nStatus缁忚繃 cMove 绉诲姩鍚庡緱鍒扮殑鏂扮姸鎬併傝嫢绉诲姩涓嶅彲琛屽垯杩斿洖-1 + char szTmp[20]; + int nZeroPos; + IntStatusToStrStatus(nStatus, szTmp); + for (int i = 0; i < 9; ++i) + if (szTmp[i] == '0') + { + nZeroPos = i; + break; + } //杩斿洖绌烘牸鐨勪綅缃 + switch (cMove) + { + case 'u': + if (nZeroPos - 3 < 0) + return -1; //绌烘牸鍦ㄧ涓琛 + else + { + szTmp[nZeroPos] = szTmp[nZeroPos - 3]; + szTmp[nZeroPos - 3] = '0'; + } + break; + case 'd': + if (nZeroPos + 3 > 8) + return -1; //绌烘牸鍦ㄧ涓夎 + else + { + szTmp[nZeroPos] = szTmp[nZeroPos + 3]; + szTmp[nZeroPos + 3] = '0'; + } + break; + case 'l': + if (nZeroPos % 3 == 0) + return -1; //绌烘牸鍦ㄧ涓鍒 + else + { + szTmp[nZeroPos] = szTmp[nZeroPos - 1]; + szTmp[nZeroPos - 1] = '0'; + } + break; + case 'r': + if (nZeroPos % 3 == 2) + return -1; //绌烘牸鍦ㄧ涓夊垪 + else + { + szTmp[nZeroPos] = szTmp[nZeroPos + 1]; + szTmp[nZeroPos + 1] = '0'; + } + break; + } + return StrStatusToIntStatus(szTmp); +} +bool Bfs(int nStatus) +{ //瀵绘壘浠庡垵濮嬬姸鎬乶Status鍒扮洰鏍囩殑璺緞 + int nNewStatus; + Flags.reset(); //娓呴櫎鎵鏈夋墿灞曟爣璁 + qHead = 0; + qTail = 1; + myQueue[qHead] = Node(nStatus, -1, 0); + while (qHead != qTail) + { //闃熷垪涓嶄负绌 + nStatus = myQueue[qHead].status; + if (nStatus == goalStatus) //鎵惧埌鐩爣鐘舵 + return true; + for (int i = 0; i < 4; i++) + { //灏濊瘯4绉嶇Щ鍔 + nNewStatus = NewStatus(nStatus, sz4Moves[i]); + if (nNewStatus == -1) + continue; //涓嶅彲绉伙紝璇曚笅涓绉 + if (Flags[nNewStatus]) + continue; //鎵╁睍鏍囪宸茬粡瀛樺湪锛屽垯涓嶅叆闃 + Flags.set(nNewStatus, true); //璁句笂宸叉墿灞曟爣璁 + myQueue[qTail++] = + Node(nNewStatus, qHead, sz4Moves[i]); //鏂拌妭鐐瑰叆闃熷垪 + } + qHead++; + } + return false; +} +int main() +{ + factorial[0] = factorial[1] = 1; + for (int i = 2; i < 21; ++i) + factorial[i] = i * factorial[i - 1]; + goalStatus = StrStatusToIntStatus("123456780"); + char szLine[50]; + char szLine2[20]; + while (cin.getline(szLine, 48)) + { + int i, j; + //灏嗚緭鍏ョ殑鍘熷瀛楃涓插彉涓烘暟瀛楀瓧绗︿覆 + for (i = 0, j = 0; szLine[i]; i++) + { + if (szLine[i] != ' ') + { + if (szLine[i] == 'x') + szLine2[j++] = '0'; + else + szLine2[j++] = szLine[i]; + } + } + szLine2[j] = 0; //瀛楃涓插舰寮忕殑鍒濆鐘舵 + int sumGoal = 0; //浠庢寰鍚庣敤濂囧伓鎬у垽鏂槸鍚︽湁瑙 + for (int i = 0; i < 8; ++i) + sumGoal += i - 1; + int sumOri = 0; + for (int i = 0; i < 9; ++i) + { + if (szLine2[i] == '0') + continue; + for (int j = 0; j < i; ++j) + { + if (szLine2[j] < szLine2[i] && szLine2[j] != '0') + sumOri++; + } + } + if (sumOri % 2 != sumGoal % 2) + { + cout << "unsolvable" << endl; + continue; + } + //涓婇潰鐢ㄥ鍋舵у垽鏂槸鍚︽湁瑙 + if (Bfs(StrStatusToIntStatus(szLine2))) + { + int nMoves = 0; + int nPos = qHead; + do + { //閫氳繃father鎵惧埌鎴愬姛鐨勭姸鎬佸簭鍒楋紝杈撳嚭鐩稿簲姝ラ + result[nMoves++] = myQueue[nPos].move; + nPos = myQueue[nPos].father; + } while (nPos); //nPos = 0 璇存槑宸茬粡鍥為鍒板垵濮嬬姸鎬佷簡 + for (int i = nMoves - 1; i >= 0; i--) + cout << result[i]; + } + else + cout << "unsolvable" << endl; + } +} +//鏃堕棿澶嶆潅搴︼紝灏辨槸鐘舵佹绘暟 \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/CatchCow.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/CatchCow.cpp" new file mode 100644 index 0000000..24a6057 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/CatchCow.cpp" @@ -0,0 +1,52 @@ +#include +#include +#include +using namespace std; +int N, K; //鍐滄皯浣嶇疆 鍜 鐗涚殑浣嶇疆 +const int MAXN = 10000; +int visited[MAXN + 10]; //鍒ら噸鏍囪 +struct Step +{ + int x; //褰撳墠浣嶇疆 + int steps; //鍒拌揪x鎵闇瑕佺殑姝ユ暟 + Step(int xx, int ss) : x(xx), steps(ss) {} +}; +queue open_table; //闃熷垪锛屽嵆open琛 +int main() +{ + cin >> N >> K; + memset(visited, 0, sizeof(visited)); + open_table.push(Step(N, 0)); + visited[N] = 1; + while (!open_table.empty()) + { + Step s = open_table.front(); + if (s.x == K) + { + //鎵惧埌鐩爣 + cout << s.steps << endl; + return 0; + } + else + { + if (s.x - 1 >= 0 && !visited[s.x - 1]) + { + //鍚戝乏璧颁竴姝 + open_table.push(Step(s.x - 1, s.steps + 1)); + visited[s.x - 1] = 1; + } + if (s.x + 1 <= MAXN && !visited[s.x + 1]) + { + open_table.push(Step(s.x + 1, s.steps + 1)); + visited[s.x + 1] = 1; + } + if (s.x * 2 <= MAXN && !visited[s.x * 2]) + { + open_table.push(Step(s.x * 2, s.steps + 1)); + visited[s.x * 2]; + } + open_table.pop(); //z渚濇嵁褰撳墠鐐规悳绱㈠畬鍚庯紝寮瑰嚭 + } + } + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/Flip Games \351\242\230\350\247\243\346\212\245\345\221\212.md" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/Flip Games \351\242\230\350\247\243\346\212\245\345\221\212.md" new file mode 100644 index 0000000..b4ade17 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/Flip Games \351\242\230\350\247\243\346\212\245\345\221\212.md" @@ -0,0 +1,160 @@ +# Flip Games 棰樿В鎶ュ憡 + +## 闂鎻忚堪锛 + +鍏蜂綋闂鎻忚堪灏变笉鍦ㄦ灞曞紑锛屾湁鍏磋叮鐨勬湅鍙嬪彲浠ュ弬鐪嬩笅闈㈢殑閾炬帴锛歨ttp://cxsjsxmooc.openjudge.cn/test2/G/ 銆傜畝鍗曟潵璇村氨鏄粰瀹氫竴涓鐩樼殑鐘舵侀噺锛堥粦鐧界浉闂达級锛屾瘡娆$偣鍑讳竴涓瀛愶紝浼氭妸鍏跺強鍛ㄥ洿鐨勬瀛愭敼鍙橀鑹诧紝闂渶灏戦渶瑕佺偣鍑诲嚑娆℃瀛愭墠鑳借揪鍒扮函榛戞垨鑰呯函鐧界殑鐘舵併 + +![KLZKoj.png](https://s2.ax1x.com/2019/11/02/KLZKoj.png) + +## 闂鍒嗘瀽 + +鐢变簬棰樼洰涓棶鐨勬槸鏈浼樿В锛屾垜浠緢瀹规槗鎯冲埌浣跨敤骞垮害浼樺厛鎼滅储鐨勬柟娉曟潵鑾峰緱绛旀锛屽叧浜嶣FS绠楁硶鐨勫垵姝ヤ粙缁嶈鍙傝冩垜鐨勪笂涓绡囧崥瀹€ https://blog.csdn.net/qq_27848347/article/details/102874472 + +### 鐘舵佽〃杈 + +BFS绠楁硶鑳屽悗鐨勬濇兂骞朵笉澶嶆潅锛屽氨鏄敤闃熷垪鏉ユā鎷熷垎灞傛悳绱㈢殑杩囩▼锛屼絾鍦ㄦ悳绱㈡椂锛屾垜浠渶瑕佸褰撳墠鐨勭姸鎬佸姞涓鏍囪锛屼互閬垮厤閲嶅鎼滅储銆傞偅涔堝浣曞湪淇濊瘉鏈夎緝蹇殑鎼滅礌閫熷害鐨勫墠鎻愪笅锛屼互杈冨皬鐨勭┖闂村瓨鍌ㄧ姸鎬(鑺傜偣)鏁扮洰宸ㄥぇ鐨勭姸鎬侊紵鏄剧劧绠鍗曠矖鏆寸殑浣跨敤涓涓瓧绗︿覆瀹炲湪鏄氮璐圭┖闂淬備綔鑰呭湪杩欓噷浣跨敤浜嗕竴涓16浣嶇殑2杩涘埗鏁版潵琛ㄨ揪褰撳墠妫嬬洏鐨勭姸鎬侀噺锛屼竴鏄瘮杈冪洿瑙傦紝浜屽垯鏂逛究鍚庨潰鐨勭炕杞搷浣溿 + +浠ヤ笂鍥句负渚嬶紝鍏剁姸鎬侀噺鍙洿鎺ヨ〃杈句负锛 + +```cpp +0 1 0 1 +1 1 1 1 +0 0 1 0 +0 1 1 0 +``` + +鍦ㄨ绠楁満瀛樺偍涓猴細 + +```cpp +010111100100110 +``` + +鏄剧劧涓涓猧nt鍙橀噺灏辫冻浠ユ弧瓒虫垜浠渶姹傦紝澶уぇ鑺傜渷浜嗙浉瀵逛簬瀛楃涓茬殑绌洪棿寮鏀 + +### 缈昏浆鎿嶄綔 + +鎸夌収棰樼洰瑕佹眰锛屾瘡鐐瑰嚮涓涓瀛愶紝浼氬皢鍏跺強鍏跺懆鍥寸殑妫嬪瓙鐨勯鑹插彂鐢熸敼鍙橈紝濡傛灉鎸夌収甯歌鎬濊矾锛岄渶瑕佸褰撳墠妫嬬洏鐨勬瀛愯繘琛岄亶鍘嗭紝鐒跺悗閫愪釜杩涜缈昏浆鎿嶄綔锛屾椂闂村鏉傚害杈冮珮銆傝冭檻鍒颁笂鏂囦腑锛屾垜浠皢姣忎釜妫嬪瓙鐨勭姸鎬佺敤涓涓猙it鐨0-1琛ㄨ揪锛屼娇鐢ㄤ綅杩愮畻鏄竴涓緢鐩存帴鐨勮冭檻銆 + +鑰冭檻鍒颁笌鎴栬繍绠楃殑鎬ц川锛 + +- 0 涓庢垨 浠讳綍鏁帮紝浠讳綍鏁颁笉鍙戠敓鍙樺寲锛氬搴斾簬妫嬬洏妫嬪瓙鍦ㄦ晥鏋滆寖鍥村锛屼笉鍙楀奖鍝 +- 1 涓庢垨 浠讳綍鏁帮紝浠讳綍鏁板彇鍙嶏細瀵瑰簲浜庢瀛愬湪鏁堟灉鑼冨洿鍐咃紝鐘舵佸彇鍙 + +鎴戜滑寰堣嚜鐒剁殑鎯冲埌灏嗗綋鍓嶆鐩樼姸鎬佷笌涓涓搷浣滃搴旂殑鏁板艰繘琛屼笌鎴栨搷浣滐紝杈惧埌缈昏浆鐨勬晥鏋溿 + +浠ョ偣鍑绘鐩樺彸涓嬭涓轰緥锛屽叾浜岃繘鍒惰〃杈惧彲浠ュ啓涓猴細 + +```cpp +0 0 0 0 +0 0 0 0 +0 0 0 1 +0 0 1 1 +``` + +灏嗗叾涓庣姸鎬佽〃杈捐繘琛屼笌鎴栫殑浣嶈繍绠楋紝鍙互寰堟柟渚跨殑鏀瑰彉妫嬬洏鐨勫綋鍓嶇姸鎬併 + +### 婧愪唬鐮 + +鍏朵粬灏辨病鏈変粈涔堟敞鎰忎簨椤癸紝鐩存帴鐪嬩唬鐮佸氨濂姐 + +```cpp +#include +#include +#include +using namespace std; + +const int board_size = 4; +const int direction_num = 4; +struct Piece +{ + int status; //鐘舵佸 + int steps; //姝ユ暟 + Piece(int st, int step) : status(st), steps(step) {} +}; +int direction[direction_num][2] = { + {-1, 0}, {0, 1}, {1, 0}, {0, -1}}; +int pos[board_size * board_size]; //16绉嶇偣鍑诲弽杞彲鑳芥 +bool marker[1 << (board_size * board_size)]; //2^16娆℃柟鎽嗘斁鐘舵 + +bool inBoard(int x, int y) +{ + if (x >= 0 && x < board_size && y >= 0 && y < board_size) + return true; + else + return false; +} +void FlipInitialize() +{ + for (int i = 0; i < board_size; i++) + { + for (int j = 0; j < board_size; j++) + { + // i*size+j 鍗充负缂栧彿 + int value = 1 << (i * board_size + j); + for (int k = 0; k < direction_num; k++) + { + int next_x = i + direction[k][0]; + int next_y = j + direction[k][1]; + if (inBoard(next_x, next_y)) + value += 1 << (next_x * board_size + next_y); + } + pos[i * board_size + j] = value; + } + } +} +int BFS(int value) +{ + queue q; + Piece s = Piece(value, 0); + q.push(s); + marker[s.status] = true; + while (!q.empty()) + { + Piece cur = q.front(); + q.pop(); + //鐩橀潰鍏ㄩ粦鎴栬呭叏鐧(2^16 -1)鏃剁粨鏉燂紝骞惰繑鍥炴鏁 + if (cur.status == 0 || cur.status == (1 << (board_size * board_size)) - 1) //娉ㄦ剰鍔犲噺杩愮畻绗︽瘮浣嶈繍绠楃绛夌骇楂橈紒锛 + { + return cur.steps; + } + //鎼滅储16涓綅缃 + for (int i = 0; i < board_size * board_size; ++i) + { + //閫氳繃寮傛垨杩愮畻寰楀埌缈昏浆鍚庣殑鐘舵 + Piece next = Piece(cur.status ^ pos[i], cur.steps + 1); + if (!marker[next.status]) + { + q.push(next); + marker[next.status] = true; + } + } + } + return -1; //鏃犳硶鍒拌揪鐩爣鐘舵 杩斿洖-1 +} +int main() +{ + freopen("E:\\text.txt", "r", stdin); + FlipInitialize(); + char str[5]; + int value = 0; + for (int i = 0; i < board_size; i++) + { + cin >> str; + for (int j = 0; j < board_size; j++) + { + if (str[j] == 'w') + value += 1 << (i * board_size + j); + // 鍔犱笂 1<<(缂栧彿) 鍗冲彲灏嗘浣嶇疆璁剧疆涓1 + } + } + int ans = BFS(value); + if (ans >= 0) + cout << ans << endl; + else + { + cout << "Impossible" << endl; + } + return 0; +} +``` + diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/README.md" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/README.md" new file mode 100644 index 0000000..e90dd1e --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/README.md" @@ -0,0 +1,145 @@ +## 寮曞叆渚嬮锛氭姄浣忛偅澶寸墰 +鍐滃か鐭ラ亾涓澶寸墰鐨勪綅缃紝鎯宠鎶撲綇瀹冦傚啘澶拰鐗涢兘浣嶄簬鏁拌酱涓婏紝鍐滃か璧峰浣嶄簬鐐筃(0<=N<=100000)锛岀墰浣嶄簬鐐筀(0<=K<=100000)銆傚啘澶湁涓ょ绉诲姩鏂瑰紡锛 +1銆佷粠X绉诲姩鍒癤-1鎴朮+1锛屾瘡娆$Щ鍔ㄨ姳璐逛竴鍒嗛挓 +2銆佷粠X绉诲姩鍒2*X锛屾瘡娆$Щ鍔ㄨ姳璐逛竴鍒嗛挓 + 鍋囪鐗涙病鏈夋剰璇嗗埌鍐滃か鐨勮鍔紝绔欏湪鍘熷湴涓嶅姩銆傚啘澶渶灏戣鑺卞灏戞椂闂存墠鑳芥姄浣忕墰锛 + ### 鍦烘櫙鍒嗘瀽 + 鍋囪鍐滃か璧峰浣嶄簬3锛岀墰浣嶄簬5锛屽浣曟悳绱竴鏉″埌5鐨勮矾寰勶紵 +[![Ksx7nS.png](https://s2.ax1x.com/2019/10/27/Ksx7nS.png)](https://imgchr.com/i/Ksx7nS) + +#### 绛栫暐涓锛氭繁搴︿紭鍏堟悳绱細 + +浠庤捣鐐瑰嚭鍙戯紝闅忔満鎸戦変竴涓柟鍚戯紝鑳藉線鍓嶈蛋灏卞線鍓嶈蛋锛堟墿灞曪級锛岃蛋涓嶅姩浜嗗垯鍥炴函銆 + +杩愭皵濂界殑璇濓細 3->4->5 + +杩愭皵鏈鍧忕殑璇濓細 3->2->1->0->4->5 + +濡傛灉鎯宠姹傛渶浼樿В锛岄渶瑕侀亶鍘嗘墍鏈夎蛋娉曪紝浣嗗彲浠ヤ娇鐢ㄤ竴瀹氭柟娉曡繘琛屽壀鏋濄 + +杩愮畻杩囩▼涓渶瑕佸瓨鍌ㄨ矾寰勪笂鐨勮妭鐐癸紝鏁伴噺杈冨皯锛岀敤鏍堝瓨鍌ㄣ + +#### 绛栫暐浜岋細骞垮害浼樺厛鎼滅储 + +缁欒妭鐐瑰垎灞傘傝捣鐐瑰氨鏄0灞傦紝浠庤捣鐐规渶灏戦渶瑕丯姝ュ氨鑳藉埌杈剧殑鐐瑰氨灞炰簬绗琻灞傘 + +渚濆眰娆¢『搴忥紝浠庡皬鍒板ぇ鐨勬墿灞曡妭鐐广傛妸灞傛浣庣殑鐐瑰叏閮ㄦ墿灞曞嚭鏉ュ悗锛屾墠浼氭墿灞曞眰娆¢珮寰楃偣銆 + +鎼滅储杩囩▼锛 + +3 + +12 4 6 + +1 5 + +娉ㄦ剰锛氭墿灞曟椂锛屼笉鑳芥墿灞曞凡缁忚蛋杩囩殑鑺傜偣銆傚彲纭繚鎵惧埌鏈浼樿В锛屼絾鏄洜鎵╁睍鍑 鏉ョ殑鑺傜偣杈冨锛屼笖澶氭暟鑺傜偣閮介渶瑕 淇濆瓨锛屽洜姝ら渶瑕佺殑瀛樺偍绌洪棿杈冨ぇ銆 鐢ㄩ槦鍒楀瓨鑺傜偣銆 + +## 骞挎悳绠楁硶锛 + + 骞垮害浼樺厛鎼滅储绠楁硶濡備笅锛氾紙鐢≦UEUE 锛 + +[![KsxH0g.png](https://s2.ax1x.com/2019/10/27/KsxH0g.png)](https://imgchr.com/i/KsxH0g) + +1. 鎶婂垵濮嬭妭鐐筍0鏀惧叆Open琛ㄤ腑锛 +2. 濡傛灉Open琛ㄤ负绌猴紝鍒欓棶棰樻棤瑙o紝澶辫触 閫鍑猴紱 +3. 鎶奜pen琛ㄧ殑绗竴涓妭鐐瑰彇鍑烘斁鍏 Closed琛紝骞惰璇ヨ妭鐐逛负 n 锛 +4. 鑰冨療鑺傜偣 n鏄惁涓虹洰鏍囪妭鐐广傝嫢鏄紝 鍒欏緱鍒伴棶棰樼殑瑙o紝鎴愬姛閫鍑猴紱 +5. 鑻ヨ妭鐐 n涓嶅彲鎵╁睍锛屽垯杞(2)姝ワ紱 +6. 鎵╁睍鑺傜偣 n锛屽皢鍏朵笉鍦–losed琛ㄥ拰 Open琛ㄤ腑鐨勫瓙鑺傜偣 (鍒ら噸锛夋斁鍏pen琛ㄧ殑灏鹃儴 锛屽苟涓烘瘡涓涓瓙鑺傜偣璁剧疆鎸囧悜鐖惰妭鐐圭殑鎸囬拡 ( 鎴栬褰曡妭鐐圭殑灞傛锛夛紝鐒跺悗杞(2)姝 + +浠g爜瀹炵幇濡備笅锛 + +```cpp +#include +#include +#include +using namespace std; +int N, K; +const int MAXN = 100000; +int visited[MAXN + 10]; //鍒ら噸鏍囪锛屼负true琛ㄧず璇ョ偣宸茬粡鎵╁睍杩囦簡 +struct Step +{ + int x; //浣嶇疆 + int steps; //鍒拌揪x闇瑕佺殑姝ユ暟 + Step(int xx, int ss) : x(xx), steps(ss) {} +}; +queue q; //闃熷垪锛屽嵆open琛 +int main() +{ + cin >> N >> K; + memset(visited, 0, sizeof(visited)); + q.push(Step(N, 0)); //鎶婅捣濮嬩綅缃斁鍏pen琛 + while (!q.empty()) + { + Step s = q.front(); //鎷垮嚭Open琛ㄤ腑绗竴涓厓绱 + if (s.x == K) + { //鎵惧埌鐩爣 + cout << s.steps << endl; + return 0; + } + else + { + if (s.x - 1 >= 0 && !visited[s.x - 1]) + { + q.push(Step(s.x - 1, s.steps + 1)); + visited[s.x - 1] = 1; + } + if (s.x + 1 <= MAXN && !visited[s.x + 1]) + { + q.push(Step(s.x + 1, s.steps + 1)); + visited[s.x + 1] = 1; + } + if (s.x * 2 <= MAXN && !visited[s.x * 2]) + { + q.push(Step(s.x * 2, s.steps + !)); + visited[s.x * 2] = 1; + } + q.pop(); + } + } + return 0; +} + +``` + +## 骞挎悳涓庢繁鎼滅殑姣旇緝 + + 骞挎悳涓鑸敤浜庣姸鎬佽〃绀烘瘮杈冪畝鍗曘佹眰鏈浼樼瓥鐣ョ殑闂 + +- 浼樼偣锛 + - 鏄竴绉嶅畬澶囩瓥鐣ワ紝鍗冲彧瑕侀棶棰樻湁瑙o紝瀹冨氨涓瀹氬彲浠ユ壘鍒拌В 銆傚苟涓旓紝 + - 骞垮害浼樺厛鎼滅储鎵惧埌鐨勮В锛岃繕涓瀹氭槸璺緞鏈鐭殑瑙c +- 缂虹偣锛 + - 鐩茬洰鎬ц緝澶э紝灏ゅ叾鏄綋鐩爣鑺傜偣璺濆垵濮嬭妭鐐硅緝杩滄椂锛屽皢浜 鐢熻澶氭棤鐢ㄧ殑鑺傜偣锛屽洜姝ゅ叾鎼滅储鏁堢巼杈冧綆銆 + - 闇瑕佷繚瀛樻墍鏈夋墿灞曞嚭 鐨勭姸鎬侊紝鍗犵敤鐨勭┖闂村ぇ + + 娣辨悳鍑犱箮鍙互鐢ㄤ簬浠讳綍闂 + +- 鍙渶瑕佷繚瀛樹粠璧峰鐘舵佸埌褰撳墠鐘舵佽矾寰勪笂鐨勮妭鐐 + +## 鍙屽悜骞垮害浼樺厛鎼滅储(DBFS) + +DBFS绠楁硶鏄BFS绠楁硶鐨勪竴绉嶆墿灞曘 + +- BFS绠楁硶浠庤捣濮嬭妭鐐逛互骞垮害浼樺厛鐨勯『搴忎笉鏂墿灞曪紝鐭ラ亾閬囧埌鐩殑鑺傜偣銆 +- DBFS绠楁硶浠庝袱涓妭鐐逛互骞垮害浼樺厛鐨勯『搴忓悓鏃舵墿灞曪紝涓涓槸浠庡叾瀹炶妭鐐瑰紑濮嬫墿灞曪紝涓涓槸浠庣洰鐨勮妭鐐瑰紑濮嬫墿灞曪紝鐭ラ亾涓涓墿灞曢槦鍒椾腑鍑虹幇浜嗗彟涓涓槦鍒椾腑宸茬粡鎵╁睍鐨勮妭鐐癸紝灏辩浉褰撲簬涓や釜鎵╁睍鐨勬柟鍚戞湁浜嗕氦鐐癸紝閭d箞鍙互璁や负鎴戜滑鎵惧埌浜嗕竴鏉¤矾寰勩 + +DBFS绠楁硶鐩稿浜嶣FS绠楁硶鏉ヨ锛岀敱浜庨噰鐢ㄤ簡鍙屽悜鎵╁睍鐨勬柟寮忥紝鎼滅储鏍戠殑瀹藉害寰楀埌浜嗘槑鏄剧殑鍑忓皯锛屾墍浠ュ湪绠楁硶鐨勬椂闂村鏉傚害鍜岀┖闂 澶嶆潅搴︿笂閮芥湁杈冨ぇ鐨勪紭鍔裤 + +- 鍋囪1涓粨鐐硅兘鎵╁睍鍑簄涓粨鐐癸紝鍗曞悜鎼滅储瑕乵灞傝兘鎵惧埌绛旀锛岄偅涔堟墿灞曞嚭鏉ョ殑鑺傜偣鏁扮洰灏辨槸: (1-$n^m$)/(1-n) +- 鍙屽悜骞挎悳锛屽悓鏍锋槸涓鍏辨墿灞昺灞傦紝鍋囧畾涓よ竟鍚勬墿灞曞嚭m/2灞傦紝鍒欐荤粨鐐规暟鐩 2 * (1-$n^{m/2}$)/(1-n) +- 姣忔鎵╁睍缁撶偣鎬绘槸閫夋嫨缁撶偣姣旇緝灏戠殑閭h竟杩涜鎵╁睍锛屽苟涓嶆槸鏈烘鐨勪袱杈逛氦鏇. + +```cpp +int expand(i) //鍏朵腑i涓洪槦鍒楃殑缂栧彿锛0鎴1 +{ + 鍙栭槦鍒梣i鐨勫ご缁撶偣H锛 + 瀵笻鐨勬瘡涓涓浉閭昏妭鐐筧dj锛 + 1 濡傛灉adj宸茬粡鍦ㄩ槦鍒梣i涔嬩腑鍑虹幇杩囷紝鍒欐姏寮僡dj锛 + 2 濡傛灉adj鍦ㄩ槦鍒梣i涓湭鍑虹幇杩囷紝鍒: + 1锛 灏哸dj鏀惧叆闃熷垪qi锛 + 2) 濡傛灉adj 鏇惧湪闃熷垪q1-i涓嚭鐜拌繃, 鍒欙細杈撳嚭鎵惧埌鐨勮矾寰 +} +``` + diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/assignment/flip_games.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/assignment/flip_games.cpp" new file mode 100644 index 0000000..378960c --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/assignment/flip_games.cpp" @@ -0,0 +1,98 @@ +#include +#include +#include +using namespace std; + +const int board_size = 4; +const int direction_num = 4; +struct Piece +{ + int status; //鐘舵佸 + int steps; //姝ユ暟 + Piece(int st, int step) : status(st), steps(step) {} +}; +int direction[direction_num][2] = { + {-1, 0}, {0, 1}, {1, 0}, {0, -1}}; +int pos[board_size * board_size]; //16绉嶇偣鍑诲弽杞彲鑳芥 +bool marker[1 << (board_size * board_size)]; //2^16娆℃柟鎽嗘斁鐘舵 + +bool inBoard(int x, int y) +{ + if (x >= 0 && x < board_size && y >= 0 && y < board_size) + return true; + else + return false; +} +void FlipInitialize() +{ + for (int i = 0; i < board_size; i++) + { + for (int j = 0; j < board_size; j++) + { + // i*size+j 鍗充负缂栧彿 + int value = 1 << (i * board_size + j); + for (int k = 0; k < direction_num; k++) + { + int next_x = i + direction[k][0]; + int next_y = j + direction[k][1]; + if (inBoard(next_x, next_y)) + value += 1 << (next_x * board_size + next_y); + } + pos[i * board_size + j] = value; + } + } +} +int BFS(int value) +{ + queue q; + Piece s = Piece(value, 0); + q.push(s); + marker[s.status] = true; + while (!q.empty()) + { + Piece cur = q.front(); + q.pop(); + //鐩橀潰鍏ㄩ粦鎴栬呭叏鐧(2^16 -1)鏃剁粨鏉燂紝骞惰繑鍥炴鏁 + if (cur.status == 0 || cur.status == (1 << (board_size * board_size)) - 1) //娉ㄦ剰鍔犲噺杩愮畻绗︽瘮浣嶈繍绠楃绛夌骇楂橈紒锛 + { + return cur.steps; + } + //鎼滅储16涓綅缃 + for (int i = 0; i < board_size * board_size; ++i) + { + //閫氳繃寮傛垨杩愮畻寰楀埌缈昏浆鍚庣殑鐘舵 + Piece next = Piece(cur.status ^ pos[i], cur.steps + 1); + if (!marker[next.status]) + { + q.push(next); + marker[next.status] = true; + } + } + } + return -1; //鏃犳硶鍒拌揪鐩爣鐘舵 杩斿洖-1 +} +int main() +{ + freopen("E:\\text.txt", "r", stdin); + FlipInitialize(); + char str[5]; + int value = 0; + for (int i = 0; i < board_size; i++) + { + cin >> str; + for (int j = 0; j < board_size; j++) + { + if (str[j] == 'w') + value += 1 << (i * board_size + j); + // 鍔犱笂 1<<(缂栧彿) 鍗冲彲灏嗘浣嶇疆璁剧疆涓1 + } + } + int ans = BFS(value); + if (ans >= 0) + cout << ans << endl; + else + { + cout << "Impossible" << endl; + } + return 0; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/assignment/matze_problem.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/assignment/matze_problem.cpp" new file mode 100644 index 0000000..118ad10 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week8/assignment/matze_problem.cpp" @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +using namespace std; + +const int board_size = 5; +const int direction_num = 4; + +struct Piece +{ + int x; + int y; + //Piece(int xx, int yy): x(xx), y(yy){} +}; + +Piece father[board_size][board_size]; +// father[x][y].x 瀛樺偍鐨勬槸鏈鐭矾寰勭殑鍧愭爣涓(x,y)鐐圭殑涓嬩竴涓潗鏍噚 + int direction[direction_num][2] = { + {-1, 0}, {0, 1}, {1, 0}, {0,-1} +}; + +int marker[board_size][board_size]; +int board[board_size][board_size]; + +bool InBoard(int x, int y) +{ + if (x >= 0 && x < board_size && y >= 0 && y < board_size) + return true; + else + return false; +} + +void BFS() +{ + queueq; + Piece start; + start.x = 4; + start.y = 4; + marker[4][4] = 1; + q.push(start); + while (!q.empty()) + { + Piece current = q.front(); + q.pop(); + if (current.x == 0 && current.y == 0) + return; + for (int i = 0; i < direction_num; i++) + { + int next_x = current.x + direction[i][0]; + int next_y = current.y + direction[i][1]; + if (InBoard(next_x, next_y) && !board[next_x][next_y] && !marker[next_x][next_y]) + { + Piece next; + next.x = next_x; + next.y = next_y; + father[next_x][next_y].x = current.x; + father[next_x][next_y].y = current.y; + marker[next_x][next_y] = 1; + q.push(next); + //cout <<"the current:" <> board[i][j]; + BFS(); + print_answer(); + return 0; +} diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/Gone_Fishing.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/Gone_Fishing.cpp" new file mode 100644 index 0000000..a3c76e1 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/Gone_Fishing.cpp" @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +int ans[30][30], f[30], d[30], t[30]; +int main() +{ + int h, n; + cin >> n; + while (true) + { + if (n == 0) + break; + + cin >> h; + h *= 12; + memset(ans, 0, sizeof(ans)); + memset(f, 0, sizeof(f)); + memset(t, 0, sizeof(t)); + memset(d, 0, sizeof(d)); + + for (int i = 1; i <= n; ++i) + { + scanf("%d", &f[i]); + } + for (int i = 1; i <= n; ++i) + { + scanf("%d", &d[i]); + } + for (int i = 1; i < n; ++i) + { + scanf("%d", &t[i]); + } + + int ht, ft[30]; + for (int ed = 1; ed <= n; ++ed) //鏋氫妇鏈杩滀細璧板埌鍝釜婀 + { + memset(ft, 0, sizeof(ft)); + for (int i = 1; i <= ed; ++i) + { + ft[i] = f[i]; + } //ft浣滀负f鐨勪复鏃惰褰 + ht = h; //璁板綍鍓╀綑鏃堕棿 + for (int i = 1; i < ed; ++i) + { + ht -= t[i]; //鍑忓幓璺笂鐨勬椂闂磋姳璐 + } + //鎺ヤ笅鏉ュ紑濮嬫ā鎷熼挀楸艰繃绋 + int k, emp = 1; //emp鏍囪杩炵画鐨勫凡缁忕┖浜嗙殑婀 + while (ht > 0 && emp <= ed) //鏃堕棿鐢ㄥ畬鎴栨箹绌轰负姝 + { + k = 1; + for (int j = emp; j <= ed; ++j) + { + if (ft[j] > ft[k]) + { + k = j; + } + } //鎵惧嚭鏈浼樼殑婀 + + ans[ed][0] += ft[k]; //姝ゆ鏀惰幏+ft[k] + ++ans[ed][k]; //璁板綍鍦╧婀栬姳璐逛簡1鍗曚綅鏃堕棿 + --ht; //鏃堕棿娑堣1鍗曚綅 + ft[k] -= d[k]; + ft[k] = ft[k] > 0 ? ft[k] : 0; + for (int j = emp; j <= ed; ++j) + { + if (ft[j] == 0) + ++emp; + else + break; + } //妫鏌ユ槸鍚d鍓嶇殑婀栭兘宸茬┖ + } + if (ht > 0) + ans[ed][1] += ht; //鑻ユ椂闂存湁鍓╀綑 + } + + int a = 1; + for (int i = 2; i <= n; ++i) + { + if (ans[i][0] > ans[a][0]) + a = i; + } //鎵惧嚭鏀剁泭鏈澶х殑鏂规 + + for (int i = 1; i <= n; ++i) + { + cout << ans[a][i] * 5; + if (i != n) + cout << ", "; + } + cout << endl; + cout << "Number of fish expected: " << ans[a][0] << endl; + + cin >> n; + if (n != 0) + cout << endl; + } + return 0; +} +// Sick this week, no more time to do code. +// Thanks to this website 47.91.197.53/acm/post/6268a \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/Yogurt factory.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/Yogurt factory.cpp" new file mode 100644 index 0000000..260f214 --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/Yogurt factory.cpp" @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +int price[10000], needs[10000]; +int s, N; +long long res; + +int main() +{ + cin >> N >> s; + for (int i = 0; i < N; i++) + scanf("%d%d", &price[i], &needs[i]); + int lowPrice = price[0]; + res += lowPrice * needs[0]; + + for (int i = 1; i < N; i++) + { + lowPrice = min(lowPrice + s, price[i]); + res += lowPrice * needs[i]; + } + cout << res << endl; +} \ No newline at end of file diff --git "a/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/aggressive_cows.cpp" "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/aggressive_cows.cpp" new file mode 100644 index 0000000..d2feffb --- /dev/null +++ "b/4.\347\256\227\346\263\225\345\237\272\347\241\200/week9/assignment/aggressive_cows.cpp" @@ -0,0 +1,43 @@ +#include +#include +#include +#include +using namespace std; +int n, c; +long long cows[100007]; +bool check(long long mid) +{ + int cnt = 1; + int cow = cows[0]; + for (int i = 1; i < n; i++) + { + if (cows[i] - cow >= mid) + { + cnt++; + cow = cows[i]; + } + if (cnt >= c) + return true; + } + return false; +} +int main() +{ + cin >> n >> c; + for (int i = 0; i < n; i++) + scanf("%lld", &cows[i]); + sort(cows, cows + n); + long long left = cows[0], right = cows[n - 1]; + while (right - left > 1) + { + long long mid = (left + right) / 2; + if (check(mid)) + left = mid; + else + { + right = mid; + } + } + cout << left << endl; + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index dc5f88d..4cdd388 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,5 @@ # 绋嬪簭璁捐涓庣畻娉 -杩欓噷鏀跺綍浜嗘垜鍦ㄥ涔犲寳浜ぇ瀛﹀湪Coursera涓婂紑璁剧殑鈥滅▼搴忚璁′笌绠楁硶鈥濅笓椤硅绋嬬殑PPT锛岀瑪璁板拰浣滀笟銆 +鏈」鐩槸鏍规嵁[chiuchiuuu/programming-and-algorithm](https://github.com/chiuchiuuu/programming-and-algorithm)杩涜閮ㄥ垎鐨勪慨鏀瑰拰璋冩暣銆 -杩欓棬涓撻」璇剧▼涓鍏辨湁7闂ㄨ锛屼粠鍩虹鐨勮绠楀璁哄紑濮嬶紝鐒跺悗浠嬬粛C/C++锛屾暟鎹粨鏋勪笌绠楁硶锛屾渶鍚庡畬鎴愪竴涓仛鎼滅储寮曟搸鐨勫ぇ椤圭洰銆 -鍦ㄥ涔犺繃绋嬩腑锛屾垜鍙戠幇杩欓棬璇剧殑璁哄潧瀹炲湪鏄お鍐锋竻浜嗐傛垜鐪嬪埌涓嶅皯灏忎紮浼村湪璁哄潧鎻愪簡闂锛屽嚑涓湀杩囧幓浜嗕緷鏃ф病浜鸿В绛旓紝杩樻湁涓浜 PPT 璧勬簮涔熸病鏈夈傝繖闂ㄨ鐨勮佸笀鍜屽姪鏁欑湅璧锋潵涔熷儚鏄斁浠讳笉绠′簡銆傚洜姝わ紝鎴戞墦绠楀畬鎴愬叏閮ㄨ绋嬶紝鏁寸悊鎵鏈夌浉鍏充俊鎭埌github涓婏紝鍊熸鎼缓涓涓氦娴佸钩鍙般備笉杩囧緢閬楁喚锛屾垜鏈缁堝彧瀹屾垚浜嗕笁闂ㄨ锛岀劧鍚庡純鍧戜簡銆 - -鐩墠鎴戝叏閮ㄥ畬鎴愮殑鏈夛細 - -1. 璁$畻瀵艰涓嶤璇█鍩虹 -2. C绋嬪簭璁捐杩涢樁 -3. C++绋嬪簭璁捐 - -4.5闆舵暎鐨勫仛浜嗗嚑閬撻锛屼笉鍊间竴鎻愩 - -鍏充簬璐$尞鐨勮瘽灏遍殢鎰忎簡锛屽鏋滀綘鏈夊彂鐜版垜鐨勯敊璇垨鑰呮湁鏇村ソ鐨勮В绛旓紝閮藉彲浠ョ洿鎺ユ彁鍑 pull request銆 - ----- - -浠ヤ笅鏄釜浜哄叧浜庤繖闂ㄨ鐨勪竴浜涚纰庡康锛 - -鍏跺疄鑷CS锛屾垜瑙夊緱鐪嬭嫳鏂囩殑鍏紑璇惧拰鑻辨枃鍘熺増涔︽洿濂姐傚浗澶栦紭绉澶у鐨勫叕寮璇鹃氬父璁茶鏈夎叮锛岃祫婧愪赴瀵岋紝璁哄潧娲昏穬锛屼綔涓氬浠roject涓轰富锛岃繖浜涘鑷鑰呮潵璇存洿鏈夎叮锛屾洿瀹规槗鍧氭寔锛屽敮涓鐨勯棬妲涘彲鑳芥槸鑻辫姘村钩鍚hh銆傚涔犲寳澶ц繖涓笓椤硅绋嬫槸鍥犱负鎴戞槸鍦ㄧ煡涔庝笂鐪嬪埌鏈変汉鎺ㄨ崘锛屾兂杩欐瘯绔熸槸鍖楀ぇ寮鐨勶紝灏卞幓瑙傛懇涓涓嬩汉瀹剁殑鏁欏鏂瑰紡鍜屼範棰樸 - -绗竴闂ㄨ鏉庢垐鑰佸笀璁茬殑璁$畻瀵艰杩樻槸鎸烘湁鎰忔濈殑锛屼綔涓氫篃閮芥槸oj鐨勫舰寮忋傚啓oj棰樿櫧鐒朵笉濡傚仛project鏈夎叮锛屼絾鏄痮j棰樿兘鏇村畾鍚戠殑娴嬭瘯浣犲鍒扮殑鐭ヨ瘑銆傚湪涓娆℃鎻愪氦澶辫触鍒版垚鍔熻繖涓繃绋嬶紝鎰熻鑳藉姏寰楀埌浜嗘彁鍗囥備负姝わ紝鎴戜竴寮濮嬪氨鎵撶畻瀹屾垚鍏ㄩ儴璇剧▼鐒跺悗缇庢粙婊嬬殑鎷夸釜璇佷功銆備絾鏄埌浜嗗悗闈㈠嚑闂ㄨ锛岃璇惧氨娌¢偅涔堝嚭褰╀簡锛屽氨鏄偅绉嶅康PPT鐨勮璇炬柟寮忋傛垜绾补鏄嚭浜庡仛棰樼殑鍏磋叮鍜屽~鍧戠殑鍔ㄥ姏鍧氭寔涓嬫潵銆 - -浠庣畻娉曞拰鏁版嵁缁撴瀯寮濮嬶紝鐢变簬鎴戜滑瀛︽牎寮浜嗙被浼肩殑璇剧▼锛屽啀鍔犱笂鎴戣嚜宸卞紑濮嬭嚜瀛︿竴浜涙満鍣ㄥ涔犵殑鐭ヨ瘑锛屾垜灏变腑鏂簡涓撻」璇剧▼鐨勫涔犮備竴寮濮嬫垜杩樻墦绠楁壘鏃堕棿濉潙锛屼笉杩囦竴鐩存嫋寤剁潃銆備絾鏄粖澶╃偣寮杩欓棬璇撅紝鍗¢】鐨勮棰戯紝鏋嚗鐨勮璇撅紝鏃犳硶鎻愪氦鐨刾oj锛屼笅杞戒笉浜嗙殑ppt锛屽喎娓呯殑璁哄潧锛屽疄鍦ㄦ槸娌″姩鍔涘潥鎸佷笅鍘讳簡锛岀劧鍚庡氨鍐冲畾褰诲簳鏀惧純浜嗐 - -鍦ㄦ垜鎷栧欢鐨勮繖鏈熼棿鏀跺埌浜嗕笉灏戝皬浼欎即鐨剆tar锛岃繕鏈変汉鎻恑ssue锛宲ull request锛岃洰鎰熷姩鐨勶紝涔熸湁鐐规劎鐤氳嚜宸辨病濉畬鍧戙備笉杩囦篃灏辫繖鏍蜂簡銆 - -2019.03.30