为了备战3月5号的pat甲级/kk
不得不开始做做题库了呜呜
上次太菜了只考了42 : (
文章目录
第一天
1001 A+B Format (20 分)
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
int a,b;
cin>>a>>b;
a+=b;
if(a==0)
{//特判
cout<<"0";return 0;
}
if(a<0)
{
cout<<"-";
a = -a;
}
int cnt = 0;
while(a)
{
s.push_back((a%10)+'0');
a/=10;
cnt++;
if(cnt%3==0)
{
s+=",";
}
}
while(s.size()!=0&&s.back()==',') s.pop_back();
reverse(s.begin(),s.end());
cout<<s;
}
1002 A+B for Polynomials (25 分)
不懂这个题为什么卡精度
#include<bits/stdc++.h>
using namespace std;
const int N = 2010;
map<int,double>mapt;
int a;
double b;
int n,m;
typedef pair<int,double>PID;
vector<PID>c;
double eps = 1e-8;
bool cmp(PID a,PID b)
{
return a.first>b.first;
}
int main()
{
cin>>n;
while(n--)
{
cin>>a>>b;
if(fabs(b)-0<eps)
{
continue;
}
if(mapt.count(a))
{
mapt[a]+=b;
}
else{
mapt[a] = b;
}
}
cin>>m;
while(m--)
{
cin>>a>>b;
if(fabs(b)-0<eps)
{
continue;
}
if(mapt.count(a))
{
mapt[a]+=b;
}
else{
mapt[a] = b;
}
}
for(auto t: mapt)
{
if(fabs(t.second)-0<eps)
{
continue;
}
c.push_back({t.first,t.second});
}
if(c.size()==0)
{
cout<<"0";
return 0;
}
cout<<c.size()<<' ';
sort(c.begin(),c.end(),cmp);
for(int i = 0;i<c.size();i++)
{
if(i==0)
printf("%d %.1lf",c[i].first,c[i].second);
else
printf(" %d %.1lf",c[i].first,c[i].second);
}
return 0;
}
1003 Emergency (25 分)
要注意很多细节呜呜呜
#include<bits/stdc++.h>
using namespace std;
const int N = 2010;
int g[N][N];
int n,m;
int cnt[N],a[N];
int dist[N],num[N];
bool st[N];
void dijkstra(int start)
{
dist[start] = 0;
cnt[start] = a[start];//不要忘记...初始化
num[start] = 1;
for(int i = 0;i<n;i++)
{
int t = -1;
for(int j = 0;j<n;j++)
{
if(!st[j]&&(t==-1||dist[t]>dist[j]))
{
t = j;
}
}
st[t] = true;
for(int j = 0;j<n;j++)
{
if(t==j) continue;
if(dist[j]>dist[t]+g[t][j])
{
cnt[j] = cnt[t]+a[j];
num[j] = num[t];
dist[j] = dist[t]+g[t][j];
}
else if(dist[j]==dist[t]+g[t][j])
{
num[j] += num[t];
cnt[j] = max(cnt[j],cnt[t]+a[j]);
}
}
}
}
int main()
{
memset(g,0x3f3f3f3f,sizeof g);
memset(dist,0x3f3f3f3f,sizeof dist);
int start,end;
cin>>n>>m>>start>>end;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
if(i==j) g[i][j] = 0;
}
for(int i = 0;i<n;i++)
{
cin>>a[i];
}
for(int i = 1;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b] = g[b][a] = min(g[a][b],c);
}
dijkstra(start);
cout<<num[end]<<' '<<cnt[end];
return 0;
}
1004 Counting Leaves (30 分)
1005 Spell It Right (20 分)
模拟即可
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<string>ans;
vector<int>num;
int main()
{
string s;
cin>>s;
for(int i = 0;i<s.size();i++)
{
num.push_back(s[i]-'0');
}
ll sum = 0;
for(int i = 0;i<num.size();i++)
{
sum+=num[i];
}
if(sum==0)
{
cout<<"zero";
return 0;
}
while(sum)
{
int num = sum%10;
sum/=10;
switch(num)
{
case 1:ans.push_back("one");break;
case 2:ans.push_back("two");break;
case 3:ans.push_back("three");break;
case 4:ans.push_back("four");break;
case 5:ans.push_back("five");break;
case 6:ans.push_back("six");break;
case 7:ans.push_back("seven");break;
case 8:ans.push_back("eight");break;
case 9:ans.push_back("nine");break;
case 0:ans.push_back("zero");break;
}
}
reverse(ans.begin(),ans.end());
for(int i = 0;i<ans.size();i++)
{
if(i==0)
cout<<ans[i];
else
cout<<" "<<ans[i];
}
return 0;
}
1006 Sign In and Sign Out (25 分)
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef struct person
{
string id;
string start;
string end;
}person;
person p[N];
int n;
bool cmp1(person a,person b)
{
return a.start<b.start;
}
bool cmp2(person a,person b)
{
return a.end>b.end;
}
int main()
{
cin>>n;
for(int i = 0;i<n;i++)
{
cin>>p[i].id>>p[i].start>>p[i].end;
}
sort(p,p+n,cmp1);
cout<<p[0].id<<" ";
sort(p,p+n,cmp2);
cout<<p[0].id;
}