极力推荐《算法笔记》这本书!!!
极力推荐《算法笔记》这本书!!!
极力推荐《算法笔记》这本书!!!
(重要的事情说三遍)
数据结构和算法讲的很好,反正我能想到的问题它都给我了答案,代码中使用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();}}