P8722 [蓝桥杯 2020 省 AB3] 日期识别(map,substr,stoi应用)
做法1:暴力枚举
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
string s;
cin>>s;
if(s[0]=='J')
{
if(s[1]=='a') cout<<1<<" ";
else if(s[1]=='u'&&s[2]=='n') cout<<6<<" ";//注意不要用if,若用不知下面的else和谁相对
else cout<<7<<" ";
}
if(s[0]=='F') cout<<2<<" ";
if(s[0]=='M')
{
if(s[2]=='r') cout<<3<<" ";
else cout<<5<<" ";
}
if(s[0]=='A')
{
if(s[1]=='p') cout<<4<<" ";
else cout<<8<<" ";
}
if(s[0]=='S') cout<<9<<" ";
if(s[0]=='O') cout<<10<<" ";
if(s[0]=='N') cout<<11<<" ";
if(s[0]=='D') cout<<12<<" ";
string d=s.substr(3,2); //运用substr和stoi将天数字符转化为数字,自动取消前导0
cout<<stoi(d);
return 0;
}
做法2:map
#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<string,int> d={
{"Jan",1},{"Feb",2},{"Mar",3},{"Apr",4},
{"May",5},{"Jun",6},{"Jul",7},{"Aug",8},
{"Sep",9},{"Oct",10},{"Nov",11},{"Dec",12}
};
int main()
{
string s,month,day;
cin>>s;
month=s.substr(0,3);
day=s.substr(3,2);
cout<<d[month]<<" "<<stoi(day);
return 0;
}
P8680 [蓝桥杯 2019 省 B] 特别数的和(to_string应用)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int n,sum=0;
cin>>n;
for(int i=1;i<=n;i++)
{
string s=to_string(i);
for(int j=0;j<s.length();j++)
{
if(s[j]=='1'||s[j]=='0'||s[j]=='2'||s[j]=='9')
{
sum+=i;
break; //记得break,不然会多加
}
}
}
cout<<sum;
return 0;
}