1、C++ STL标准模板库学习记录
2、部分代码
2021年4月第一题:卡片
#include<iostream> #include<cstring> using namespace std; int main(){ string str; int n=2021; int i=0; while(n>0){ str = to_string(i); for(int j=0;j<str.length();j++){ if(str[j]=='1') { n--; } } cout<<i<<endl; cout<<n<<endl; i++; } return 0; }
第二题:直线(答案来源:https://blog.csdn.net/wjinjie/article/details/116013954)
#include <iostream> #include <vector> #include <set> using namespace std; struct point { int x; //横坐标 int y; //纵坐标 }; int main() { vector<point> p;//存放所有点 for (int i = 0; i <= 19; ++i) for (int j = 0; j <= 20; ++j) p.push_back({i, j}); int len = p.size(); set<pair<double, double>> lines; //存放斜直线y=kx+b for (int i = 0; i < len; ++i) { for (int j = 0; j < len; ++j) { if (p[i].x != p[j].x && p[i].y != p[j].y) // 统计所有斜直线的情况 { double k = (p[j].y - p[i].y) * 1.0 / (p[j].x - p[i].x); double b = (p[j].y * (p[j].x - p[i].x) - (p[j].y - p[i].y) * p[j].x) * 1.0 / (p[j].x - p[i].x); //double b = p[j].y - k * p[j].x; 不用这种方法,避免k造成精度爆炸 lines.insert(pair<double, double>(k, b)); } } } cout << lines.size() + 20 + 21 << endl; // 总的直线=斜直线+横直线+竖直线 return 0; }
第六题:时间显示(答案来源:https://blog.csdn.net/wjinjie/article/details/116013954)
#include<iostream> #include<sstream> #include<algorithm> using namespace std; int main() { long long num; cin >> num; long long time = num % (24 * 60 * 60 * 1000); //时钟 int HH = time / (60 * 60 * 1000); //分钟 int MM = time % (60 * 60 * 1000); MM = MM / 60000; //秒钟 int SS = (time / 1000) % 60; printf("%02d:%02d:%02d", HH, MM, SS); return 0; }
2020第十一届蓝桥杯C/C++ B组省赛 试题B 既约分数
#include<iostream> using namespace std; int gcd(int a,int b) { if(a%b==0) return b; else return gcd(b,a%b); } int main(){ int n = 0; for(int i=1;i<=2020;i++){ for(int j=1;j<=2020;j++){ if(gcd(i,j)==1){ n++; } } } cout<<n<<endl; return 0; }
试题F 成绩统计
#include<iostream> #include<cstdio> using namespace std; int main() { int num; cin>>num; int fen; int jige=0; int youxiu=0; for(int i=0;i<num;i++){ cin>>fen; if(fen<=100||fen>=0) { if(fen>=60){ jige++; if(fen>=85){ youxiu++; } } }else{ i--; } } double you = (double)youxiu/num; double ji = (double)jige/num; you = you*100; ji = ji *100; printf("%.0f",ji);cout<<"%"<<endl; printf("%.0f",you);cout<<"%"<<endl; return 0; }
试题 H: 等差数列(答案来源:https://blog.csdn.net/qq_44524918/article/details/113530469)
#include <iostream> #include <algorithm> #include <set> using namespace std; const int max_n = 100005; int a[max_n], N, res = 0; int main(){ cin >> N; set<int> d; for(int i=0; i<N; i++) cin >> a[i]; sort(a, a+N); //将已知的数列从小到大排序 for(int i=1; i<N; i++)//找出最小公差,set 可以自动排序,且元素唯一 d.insert(a[i] - a[i-1]); int t = *(d.begin());//d 的首元素即最小公差 for(int i=a[0]; i<a[N-1]; i=i+t) res++; //算出最小项 cout << res+1; return 0; }