课堂总结
P2010 [NOIP 2016 普及组] 回文日期
考察知识点:
枚举,判断回文数
题意:
在给定的两个日期之间,找出所有真实存在的回文日期的数量。
错误代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int a,b;
cin>>a>>b;
int cnt=0;
for(int i=a;i<=b;i++)
{
int d=i%100;
int m=(i%10000-d)/100,y=i/10000;
// cout<<d<<' '<<m<<' '<<y<<'\n';
if(y%4==0 and (y%100!=0 or y%400==0))
{
if(m>=13)continue;
if(d>=32)continue;
if((m==4 or m==6 or m==9 or m==11)and d>=31)continue;
if(m==2 and d>=30)continue;
}
else
{
if(m>=13)continue;
if(d>=32)continue;
if((m==4 or m==6 or m==9 or m==11)and m>=31)continue;
if(m==2 and d>=29)continue;
}
int m1=(d%10)*1000+(d/10)*100;
if(m<=9)m1+=m*10;
else m1=m1+(m%10)*10+1;
if(y==m1)
{
cnt++;
// cout<<m1<<' '<<i<<'\n';
}
// cout<<x<<' '<<y<<'\n';
// 0228=>8220
}
cout<<cnt;
return 0;
}错误原因:
没有判断月份和日期为 的情况。
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int a,b;
cin>>a>>b;
int cnt=0;
for(int i=a;i<=b;i++)
{
int d=i%100;
int m=(i%10000-d)/100,y=i/10000;
// cout<<d<<' '<<m<<' '<<y<<'\n';
if(y%4==0 and (y%100!=0 or y%400==0))
{
if(m>=13 or m==0)continue;
if(d>=32 or d==0)continue;
if((m==4 or m==6 or m==9 or m==11)and d>=31)continue;
if(m==2 and d>=30)continue;
}
else
{
if(m>=13 or m==0)continue;
if(d>=32 or d==0)continue;
if((m==4 or m==6 or m==9 or m==11)and d>=31)continue;
if(m==2 and d>=29)continue;
}
int m1=(d%10)*1000+(d/10)*100;
if(m<=9)m1+=m*10;
else m1=m1+(m%10)*10+1;
if(y==m1)
{
cnt++;
// cout<<m1<<' '<<i<<'\n';
}
// cout<<x<<' '<<y<<'\n';
// 0228=>8220
}
cout<<cnt;
return 0;
}P5661 [CSP-J 2019] 公交换乘
考察知识点:
模拟
题意:
你有一些地铁和公交的乘车记录,坐地铁会获得一张有效期为 分钟的优惠票,坐公交时如果有票价足够且未过期的优惠票就必须使用(优先用最早的),否则需要付钱,求总花费。
正确思路:
坐地铁是用数组存所有优惠票。坐公交时先清除过期票,然后遍历查找第一张票价足够且未过期的票,找到后把票价置0表示已使用,没找到就花钱买票。
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
int t,p;
}a[100010];
int hh=1,tt=1;
signed main()
{
int n;
cin>>n;
int ans=0;
while(n--)
{
int op,t,p;
cin>>op>>p>>t;
//判断坐的是公交还是地铁
if(op==0)
{
a[tt].t=t;
a[tt].p=p;
tt++;
ans+=p;
}
else
{
while(hh<tt and t-a[hh].t>45)hh++;
for(int i=hh;i<tt;i++)
{
//判断能否用优惠卷
if(a[i].p>=p and t-a[i].t<=45)
{
p=0;
a[i].p=0;
break;
}
}
ans+=p;
}
}
cout<<ans;
return 0;
}P7911 [CSP-J 2021] 网络连接
考察知识点:
枚举
正确思路:
| 机器类型 | 地址是否合法 | 连接情况 | 输出 |
|---|---|---|---|
| Server | ❌ 不合法 | \ | ERR |
| Server | ✅ 合法 | 该地址未被占用 | OK |
| Server | ✅ 合法 | 该地址已被其他服务机占用 | FAIL |
| Client | ❌ 不合法 | \ | ERR |
| Client | ✅ 合法 | 能找到对应服务机 | 输出服务机编号 |
| Client | ✅ 合法 | 找不到对应服务机 | FAIL |
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
map<string,int> mp;
int f(string s)
{
s+='@';
int num=0,cnt=0,cnt1=0;
string ss;
for(int i=0;i<s.size();i++)
{
if(s[i]=='.')
{
cnt++;
if(ss.size()==0 || num<0 || num>255 || to_string(num)!=ss)
{
return 1;
}
num=0;
ss="";
}
else if(s[i]==':')
{
cnt1++;
if(cnt<3 || cnt1>1 || ss.size()==0 || to_string(num)!=ss || num<0 || num>255)//判断是否为空串/超范围/前导零
{
return 1;
}
num=0;
ss="";
}
else if(s[i]=='@')
{
if(num<0 || num>65535 || to_string(num)!=ss || ss.size()==0)
{
return 1;
}
num=0;
ss="";
}
else//数字字符
{
ss+=s[i];
num=num*10+(s[i]-'0');
}
}
if(cnt!=3 || cnt1!=1)//有且仅有3个'.'或1个':'
{
return 1;
}
return 0;
}
signed main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
string op,s;
cin>>op>>s;
if(op=="Server")
{
if(f(s)==1)
{
cout<<"ERR\n";
}
else if(mp.count(s)==0)
{
cout<<"OK\n";
mp[s]=i;
}
else
{
cout<<"FAIL\n";
}
}
else
{
if(f(s)==1)
{
cout<<"ERR\n";
}
else if(mp.count(s)==0)
{
cout<<"FAIL\n";
}
else
{
cout<<mp[s]<<'\n';
}
}
}
return 0;
}