PAT(乙级)2021年春季考试

PAT(乙级)2021年春季考试

7-1 打印三角形拼图 (15 分)

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
	int n;
	char a,b,c;
	cin>>n>>a>>b>>c;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j)
				printf("%c",c);
			if(i<j)
				printf("%c",a);
			if(i>j)
				printf("%c",b);
		}
		printf("\n");
	}
	return 0;
}

7-2 赌马 (20 分)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-8;
bool equal(double a,double b)
{
	if(abs(a-b)<eps)
		return true;
	return false;
}
struct node
{
	int num;
	double time;
	bool operator < (const node b) const
	{
		if(equal(time,b.time))
			return num<b.num;
		return time<b.time;
	}
}p[10005];
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,m;
	double a,b;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>m>>a>>b;
		p[i].num=m;
		p[i].time=a/b;
	}
	sort(p,p+n);
	printf("%d %d %d\n",p[0].num,p[1].num,p[2].num);
	return 0;
}

7-3 拼题 A 是真爱 (20 分)

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	string s;
	getline(cin,s);
	int len=s.length();
	for(int i=0;i<len;i++)
		if(s[i]=='P')
			s[i]+=32;
	s+=".";
	int ans=0;
	int pos=s.find("pintia");
	if(pos==0&&(s[pos+6]=='.'||s[pos+6]==','||s[pos+6]==' ') || pos>0&&(s[pos-1]=='.'||s[pos-1]==','||s[pos-1]==' ')&&(s[pos+6]=='.'||s[pos+6]==','||s[pos+6]==' '))
		ans++;
	while(pos>=0)
	{
		pos=s.find("pintia",pos+1);
		if(pos>0&&(s[pos-1]=='.'||s[pos-1]==','||s[pos-1]==' ')&&(s[pos+6]=='.'||s[pos+6]==','||s[pos+6]==' '))
			ans++;
	}
	cout<<ans<<endl;
	if(ans==0)
		cout<<"wu gan"<<endl;
	if(ans>=1&&ans<=3)
		cout<<"you ai"<<endl;
	if(ans>=4)
		cout<<"zhen ai la"<<endl;
	return 0;
}

7-4 素数等差数列 (20 分)

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=100005;
bool p[maxn];
int prime[maxn];
int main()
{
	int n,m,size=0;
	cin>>n>>m;
	for(int i=2;i<=m;i++)
	{
		if(!p[i])
		{
			prime[size++]=i;
			for(int j=2*i;j<=m;j+=i)
				p[j]=true;
		}
	}//素数打表 
	int maxdif=(m-2)/(n-1),begin,d,found=0;
	if(n>1)
		for(int i=maxdif;i>0;i--)
		{
			if(found) break;
			for(int j=size-1;j>=0;j--)
			{
				int cnt=1,next=prime[j]-i;
				while(!p[next]&&next>=2)
				{
					cnt++;
					if(cnt==n) break;
					next-=i;
				}
				if(cnt==n)
				{
					d=i;
					begin=next;
					//begin=prime[j]-d*(n-1);
					found=1;
					break;
				}
			}
		}
	if(found)
	{
		printf("%d",begin);
		for(int i=1;i<n;i++)
			printf(" %d",begin+i*d);
	}
	else
	{
		for(int i=m;i>=2;i--)
		{
			if(!p[i])
			{
				printf("%d",i);
				break;
			}
		}
	}
	return 0;
}

7-5 实验室使用排期 (25 分)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
	int begin,end;
	bool operator < (const node b) const
	{
		return end<b.end;
	}
}p[2005];
int main()
{
	int n,h,m,s;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d:%d:%d",&h,&m,&s);
		p[i].begin=h*3600+m*60+s;
		scanf("%d:%d:%d",&h,&m,&s);
		p[i].end=h*3600+m*60+s;
	}
	sort(p,p+n);
	int sta=p[0].end,ans=1;
	for(int i=1;i<n;i++)
	{
		if(p[i].begin>=sta)
		{
			ans++;
			sta=p[i].end;
		}
	}
	cout<<ans<<endl;
	return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

球王武磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值