最近正好在刷算法题,报了一个蓝桥杯体验一下,但是钱都交了,高低混个奖好吧,今天做的都是一些填空推理题,相当于用程序写下正解,代码是在Dev C++上面写的
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int res = 0;
for(int i = 1;i<= 2020;i++){
string cur = to_string(i);
for(char c : cur){
if(c == '2'){
res++;
}
}
}
cout<<res<<endl;
return 0;
}
结果是624,拿下!
这道题就是深度优先遍历即可,就是要自己搞输入的数据处理起来很麻烦,就直接在resInit
函数上做了。。。
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int res = 0;
vector<vector<bool>> visit;
vector<vector<string>> chooseList;
void resInit(int n){
for(int i = 0;i< n;i++){
vector<bool> visitVec(n,false);
visit.push_back(visitVec);
}
vector<string> visitStr1(1,"UDDLUULRUL");
chooseList.push_back(visitStr1);
vector<string> visitStr2(1,"UURLLLRRRU");
chooseList.push_back(visitStr2);
vector<string> visitStr3(1,"RRUURLDLRD");
chooseList.push_back(visitStr3);
vector<string> visitStr4(1,"RUDDDDUUUU");
chooseList.push_back(visitStr4);
vector<string> visitStr5(1,"URUDLLRRUU");
chooseList.push_back(visitStr5);
vector<string> visitStr6(1,"DURLRLDLRL");
chooseList.push_back(visitStr6);
vector<string> visitStr7(1,"ULLURLLRDU");
chooseList.push_back(visitStr7);
vector<string> visitStr8(1,"RDLULLRDDD");
chooseList.push_back(visitStr8);
vector<string> visitStr9(1,"UUDDUDUDLL");
chooseList.push_back(visitStr9);
vector<string> visitStr10(1,"ULRDLUURRR");
chooseList.push_back(visitStr10);
}
void dfs(vector<vector<string>> &chooseList,vector<vector<bool>> &visit,int i,int j,int &n){
if(i < 0 || i> n-1 || j < 0 || j> n-1){
res++;
return;
}
if(visit[i][j]){
return;
}
visit[i][j] = true;
if(chooseList[i][0][j] == 'U'){
dfs(chooseList,visit,i-1,j,n);
}else if(chooseList[i][0][j] == 'D'){
dfs(chooseList,visit,i+1,j,n);
}else if(chooseList[i][0][j] == 'R'){
dfs(chooseList,visit,i,j+1,n);
}else if(chooseList[i][0][j] == 'L'){
dfs(chooseList,visit,i,j-1,n);
}
visit[i][j] = false;
}
int main(){
int n = 10;
resInit(n);
for(int i = 0;i< n;i++){
for(int j = 0;j< n;j++){
dfs(chooseList,visit,i,j,n);
}
}
cout<<res<<endl;
return 0;
}
resInit
的后半部分在输入题干的地图数据,所以看起来很多,其实就是每个位置都做一次dfs
就行
5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211
这道题不能直接相乘找0,暴力解的话很麻烦,大数乘法,分分钟就溢出,什么long long
也不顶用,应该把数据一一处理,记录每一个乘数的2和5因子,最终把所有数的2和5因子的个数汇总起来去最小值,就是10因子的个数
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int res = 0;
int data[100] = {5650, 4542, 3554, 473, 946, 4114, 3871, 9073, 90, 4329 ,
2758 ,7949, 6113, 5659, 5245 ,7432 ,3051, 4434, 6704, 3594 ,
9937 ,1173, 6866, 3397 ,4759 ,7557, 3070, 2287 ,1453 ,9899,
1486, 5722, 3135, 1170 ,4014, 5510 ,5120 ,729 ,2880 ,9019,
2049, 698, 4582, 4346, 4427 ,646 ,9742, 7340 ,1230, 7683,
5693, 7015, 6887, 7381, 4172, 4341, 2909, 2027, 7355, 5649,
6701 ,6645 ,1671, 5978 ,2704 ,9926, 295 ,3125, 3878 ,6785 ,
2066 ,4247, 4800 ,1578, 6652 ,4616, 1113, 6205, 3264, 2915,
3966 ,5291, 2904, 1285, 2193, 1428 ,2265, 8730, 9436, 7074 ,
689, 5510, 8243, 6114, 337 ,4096 ,8199, 7313, 3685 ,211 };
int cnt2 = 0;
int cnt5 = 0;
for(int i = 0;i< 100;i++){
int dataSum = data[i];
while(dataSum %2 == 0) {
cnt2++;
dataSum /= 2;
}
dataSum = data[i];
while(dataSum%5 == 0){
cnt5++;
dataSum /= 5;
}
}
res += min(cnt2,cnt5);
cout<<res<<endl;
return 0;
}
好困,结束!!!