極力推薦《算法筆記》這本書!!!
極力推薦《算法筆記》這本書!!!
極力推薦《算法筆記》這本書!!!
(重要的事情說三遍)
數據結構和算法講的很好,反正我能想到的問題它都給我了答案,代碼中使用c++容器和算法恰到好處,使得代碼通俗易懂并且容易實現,不至于陷于c++的語言細節中,只學過c語言的同學請放心食用,每個代碼平均就3-4行用到c++。數據結構除了樹與圖要自己實現以外其他c++都給予實現,同時樹與圖中的廣度遍歷經常要使用到隊列(qutue),直接調用qutue就可以,如果使用數組模擬隊列實在是勸退。反正《算法競賽入門經典》把我勸退了,(沒有看完不多評價)
我的代碼用c++的特性比較多,不過這些特性真的很好用啊
集訓隊練習04
7-1 旅游記 //這題考了圖的最短路徑,我使用了Floyd算法
#include<bits/stdc++.h>
using namespace std;
int main()
{int a, b, x, y, z, n, m;while (cin >> a >> b){int c[10 + 1][10 + 1];for (int i = 1; i <= a; ++i){for (int j = 1; j <= a; ++j)c[i][j] = 20 + 1;}for (int item = 0; item < b; ++item){cin >> x >> y >> z;c[x][y] = z;c[y][x] = z;}for (int k = 1; k <= a; ++k)//Floyd算法只有短短7行,詳情算法筆記for (int i = 1; i <= a; ++i)for (int j = 1; j <= a; ++j){if (c[i][k] + c[k][j] < c[i][j])c[i][j] = c[i][k] + c[k][j];}cin >> n >> m;if (c[n][m] == 20 + 1){cout << "unreachable"<<endl;}else{cout << c[n][m]<<endl;}}}
7-2 大眾評委大作戰
#include<bits/stdc++.h>
using namespace std;
int main()
{int a, c, flag = 0;vector<int>B;while (cin >> a){for(int i=0;i<a;++i){cin>>c;B.push_back(c);}sort(B.begin(), B.end());B.erase(unique(B.begin(), B.end()), B.end());cout << B.size() << endl;for (auto j : B){if (flag == 1)cout << ' ';cout << j;flag = 1;}cout<<endl;B.clear();flag = 0;}}
集訓隊練習05
7-1 找數字
#include<bits/stdc++.h>
using namespace std;
using namespace std;
int main()
{int a, b, c;int count = 0;while (cin >> a >> b >> c){for (int i = a; i <= b; ++i){string n = to_string(i);for (auto j : n){if ((j - '0') == c)++count;}}cout << count << endl;count = 0;}}
7-2 統計相同數字的個數
#include<bits/stdc++.h>
using namespace std;
int main()
{map<int, int>A;int b, c;cin >> c;for(int j=0;j<c;++j){cin >> b;A[b]++;}for (auto i : A){cout << i.first << ' ' << i.second << endl;}}
7-1 大數計算
大數計算是經常出現在競賽題目中的一種情況,c++的實現是用到的字符串string
#include<bits/stdc++.h>
using namespace std;
int main()
{int a, flag = 0, len, count = 0, item, d = 0;int m = 0, count2 = 0;vector<string>B;string c, sum;while (cin >> a){while (cin >> c){B.push_back(c);if (c == string("0"))++m;if (m == a)break;}for (auto j : B){if (flag == 0){sum = j;flag = 1;continue;}if (j != string("0")){if (sum.size() < j.size())swap(sum, j);len = sum.size() - j.size();j = string(len, '0') + j;for (int k = sum.size() - 1; k >= 0; k--){item = sum[k] - '0' + j[k] - '0' + d;if (item > 9){sum[k] = item - 10 + '0';d = 1;}else{sum[k] = item + '0';d = 0;}}if (d == 1){sum = string("1") + sum;d = 0;}}else{if (count != 0)cout << "\n\n";cout << sum;flag = 0;count = 1;}}count = 0;flag = 0;B.clear();m = 0;++count2;cout << endl;}
}
7-2 集訓隊測試成績管理
#include<bits/stdc++.h>
using namespace std;
int main()
{int a, b, item;int n, m;char l;vector<int>C;while (cin >> a){cin >> b;for (int i = 0; i < a; ++i){cin >> item;C.push_back(item);}for (int j = 0; j < b; ++j){cin >> l >> n >> m;if (l == 'U'){C[n - 1] = m;}else{cout << *max_element(C.begin() + (n - 1), C.begin() + m) << endl;}}C.clear();}}
7-3 集訓隊測試成績管理(1000ms)//7-2,7-3代碼一樣
#include<bits/stdc++.h>
using namespace std;
int main()
{int a, b, item;int n, m;char l;vector<int>C;while (cin >> a){cin >> b;for (int i = 0; i < a; ++i){cin >> item;C.push_back(item);}for (int j = 0; j < b; ++j){cin >> l >> n >> m;if (l == 'U'){C[n - 1] = m;}else{cout << *max_element(C.begin() + (n - 1), C.begin() + m) << endl;}}C.clear();}}