PAT乙级的题目对初学者比较友好,可以巩固基础,写完了还能拿50块代金券。
题目网址链接:link.
1001:A+B和C(15)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int cas=0,t;
int main()
{
int t;
ll a,b,c;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
if(a+b>c)
{
printf("Case #%d: true\n",++cas);
}
else
{
printf("Case #%d: false\n",++cas);
}
}
return 0;
}
1002: 数字分类 (20)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n,a1=0,a2=0,a3=0,a4=0,cnt4=0,a5=-2;
int main()
{
cin>>n;
int num,control=1;
for(int i=0;i<n;i++)
{
cin>>num;
if(num%5==0&&num%2==0)
{
a1+=num;
}
else if(num%5==1)
{
a2+=num*control;
control*=-1;
}
else if(num%5==2)
{
a3++;
}
else if(num%5==3)
{
a4+=num;
cnt4++;
}
else if(num%5==4)
{
a5=max(a5,num);
}
}
if(a1) printf("%d ",a1);
else printf("N ");
if(a2) printf("%d ",a2);
else printf("N ");
if(a3) printf("%d ",a3);
else printf("N ");
if(a4) printf("%0.1f ",1.0*a4/cnt4);
else printf("N ");
if(a5!=-2) printf("%d",a5);
else printf("N");
return 0;
}
1003: 数素数 (20)
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
bool is_prime(int num)
{
if(num<=1) return false;
for(int i=2;i*i<=num;i++)
{
if(num%i==0)
{
return false;
}
}
return true;
}
vector<int> ans;
int n,m;
int main()
{
cin>>n>>m;
int cnt=0;
for(int i=1;i<=m*100&&cnt<m;i++)
{
if(is_prime(i))
{
cnt++;
if(cnt>=n&&cnt<=m)
{
ans.push_back(i);
}
}
}
for(int i=0;i<ans.size();i++)
{
if(i%10==0)
{
printf("%d",ans[i]);
}
else
{
printf(" %d",ans[i]);
if(i%10==9&&i!=(ans.size()-1))
{
printf("\n");
}
}
}
return 0;
}
1004: 福尔摩斯的约会 (20)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
string str1,str2,str3,str4;
const string day[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
int main()
{
cin>>str1>>str2>>str3>>str4;
int cnt=0,ans3,HH;
char ans1,ans2;
for(int i=0;i<str1.length()&&i<str2.length();i++)
{
if(str1[i]==str2[i]&&str1[i]>='A'&&str1[i]<='G'&&cnt==0)
{
ans1=str1[i];
cnt++;
}
else if(str1[i]==str2[i]&&cnt==1&&(str1[i]>='0'&&str1[i]<='9'||str1[i]>='A'&&str1[i]<='N'))
{
ans2=str1[i];
break;
}
}
for(int i=0;i<str3.length()&&i<str4.length();i++)
{
if(str3[i]==str4[i]&&(str3[i]>='a'&&str3[i]<='z'||str3[i]>='A'&&str3[i]<='Z'))
{
ans3=i;
break;
}
}
cout<<day[ans1-'A']<<" ";
if(ans2>='0'&&ans2<='9')
{
cout<<"0"<<ans2<<":";
}
else
{
cout<<ans2-'A'+10<<":";
}
if(ans3>=0&&ans3<=9)
{
cout<<"0"<<ans3<<endl;
}
else
{
cout<<ans3<<endl;
}
return 0;
}
1005: 德才论 (25)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100005;
struct Student{
int id,d,c,prior;
Student(){}
Student(int _id,int _d,int _c,int _prior):id(_id),d(_d),c(_c),prior(_prior){}
bool operator < (const Student& stu) const
{
if(this->prior<stu.prior)
{
return true;
}
else if(this->prior==stu.prior)
{
if(this->d+this->c>stu.d+stu.c)
{
return true;
}
else if(this->d+this->c==stu.d+stu.c)
{
if(this->d>stu.d)
{
return true;
}
else if(this->d==stu.d)
{
return this->id<stu.id;
}
}
}
return false;
}
void print()
{
cout<<id<<" "<<d<<" "<<c<<endl;
}
}students[maxn];
int main()
{
int n,L,H,id,d,c;
scanf("%d%d%d",&n,&L,&H);
int index=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&id,&d,&c);
if(d<L||c<L)
{
continue;
}
else if(d>=H&&c>=H)
{
students[index++]=Student(id,d,c,1);
}
else if(d>=H&&c<H)
{
students[index++]=Student(id,d,c,2);
}
else if(d<H&&c<H&&d>=c)
{
students[index++]=Student(id,d,c,3);
}
else
{
students[index++]=Student(id,d,c,4);
}
}
sort(students,students+index);
cout<<index<<endl;
for(int i=0;i<index;i++)
{
students[i].print();
}
return 0;
}
1006:部分A+B(15)
#include <cstdio>
using namespace std;
int getP(int a,int da)
{
int sum=0;
while(a)
{
int r=a%10;
if(r==da)
{
sum=sum*10+da;
}
a/=10;
}
return sum;
}
int main()
{
int a,da,b,db;
scanf("%d%d%d%d",&a,&da,&b,&db);
printf("%d",getP(a,da)+getP(b,db));
return 0;
}
1007: A除以B (20)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string a;
int b,temp=0;
cin>>a>>b;
bool flag=false;
for(int i=0;i<a.length();i++)
{
temp=temp*10+a[i]-'0';
if(temp/b==0&&flag==false) continue;
else if(temp/b&&flag==false)
{
printf("%d",temp/b);
flag=true;
}
else
{
printf("%d",temp/b);
flag=true;
}
temp%=b;
}
if(flag==false) printf("0");
printf(" %d",temp);
return 0;
}
1008: 锤子剪刀布 (20)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int cnt[2][3]; //B、C、J
const char alpha[3]={'B','C','J'};
int main()
{
int n;
scanf("%d",&n);
string jia,yi;
int yin=0,pingju=0,shu=0,len=n;
while(len--)
{
cin>>jia>>yi;
if(jia[0]=='C')
{
if(yi[0]=='C')
{
pingju++;
}
if(yi[0]=='J')
{
yin++;
cnt[0][1]++; //甲用锤子赢了
}
if(yi[0]=='B')
{
shu++;
cnt[1][0]++; //乙用布赢了
}
}
else if(jia[0]=='J')
{
if(yi[0]=='C')
{
shu++;
cnt[1][1]++;
}
if(yi[0]=='J'){
pingju++;
}
if(yi[0]=='B')
{
yin++;
cnt[0][2]++;
}
}
else
{
if(yi[0]=='C')
{
yin++;
cnt[0][0]++;
}
if(yi[0]=='J'){
shu++;
cnt[1][2]++;
}
if(yi[0]=='B')
{
pingju++;
}
}
}
int index1=-1,maxs=-1;
for(int i=0;i<3;i++)
{
if(cnt[0][i]>maxs)
{
maxs=cnt[0][i];
index1=i;
}
}
maxs=-1;
int index2;
for(int i=0;i<3;i++)
{
if(cnt[1][i]>maxs)
{
maxs=cnt[0][i];
index2=i;
}
}
printf("%d %d %d\n%d %d %d\n%c %c",yin,pingju,shu,shu,pingju,yin,alpha[index1],alpha[index2]);
return 0;
}
1009: 数字黑洞 (20)
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> change1(int num)
{
vector<int> ans;
int flag=4;
while(num||flag)
{
ans.push_back(num%10);
num/=10;
flag--;
}
sort(ans.begin(),ans.end(),greater<int>());
return ans;
}
vector<int> change2(int num)
{
vector<int> ans;
int flag=4;
while(num||flag)
{
ans.push_back(num%10);
num/=10;
flag--;
}
sort(ans.begin(),ans.end());
return ans;
}
bool judge(int num)
{
vector<int> ans;
int flag=4;
while(num||flag)
{
ans.push_back(num%10);
num/=10;
flag--;
}
for(int i=0;i<ans.size()-1;i++)
{
if(ans[i+1]!=ans[i])
{
return false;
}
}
return true;
}
int main()
{
int num;
cin>>num;
if(judge(num))
{
printf("%d - %d = 0000\n",num,num);
}
else
{
while(1)
{
vector<int> num1=change1(num);
vector<int> num2=change2(num);
int rnum1=0,rnum2=0;
for(int i=0;i<4;i++)
{
rnum1=rnum1*10+num1[i];
}
for(int i=0;i<4;i++)
{
rnum2=rnum2*10+num2[i];
}
for(int i=0;i<4;i++)
{
printf("%d",num1[i]);
}
printf(" - ");
for(int i=0;i<4;i++)
{
printf("%d",num2[i]);
}
printf(" = %d\n",rnum1-rnum2);
num=rnum1-rnum2;
if(num==6174) break;
}
}
return 0;
}
1010: 月饼(25)
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1005;
int n,d;
struct Moon_cake{
int store,total;
float single;
bool operator < (const Moon_cake& m) const{
return this->single > m.single;
}
}moon_cake[maxn];
int main()
{
cin>>n>>d;
for(int i=0;i<n;i++)
{
cin>>moon_cake[i].store;
}
for(int i=0;i<n;i++)
{
cin>>moon_cake[i].total;
}
for(int i=0;i<n;i++)
{
moon_cake[i].single=1.0*moon_cake[i].total/moon_cake[i].store;
}
sort(moon_cake,moon_cake+n);
int num=0,index=0;
float sum=0;
while(1)
{
if(num+moon_cake[index].store<=d)
{
num+=moon_cake[index].store;
sum+=moon_cake[index].total;
index++;
}
else
{
break;
}
}
int rest=d-num;
sum+=rest*moon_cake[index].single;
printf("%0.2f\n",sum);
return 0;
}
1011: 个位数统计 (15)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
string str;
int cnt[10];
int main()
{
cin>>str;
for(int i=0;i<str.length();i++)
{
cnt[str[i]-'0']++;
}
for(int i=0;i<10;i++)
{
if(cnt[i]>=1)
{
printf("%d:%d\n",i,cnt[i]);
}
}
return 0;
}
1012: D进制的A+B (20)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
string ans;
int a,b,d;
int main()
{
scanf("%d%d%d",&a,&b,&d);
int num=a+b;
while(num)
{
ans.push_back(num%d+'0');
num/=d;
}
for(int i=ans.size()-1;i>=0;i--)
{
printf("%c",ans[i]);
}
printf("\n");
return 0;
}
10013:组个最小数(20)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int cnt[10];
string ans;
bool judge()
{
for(int i=0;i<10;i++)
{
if(cnt[i]>0) return true;
}
return false;
}
int main()
{
for(int i=0;i<10;i++)
{
scanf("%d",&cnt[i]);
}
//第一个数不能为0,在1到9挑最小的数放
for(int i=1;i<10;i++)
{
if(cnt[i]>0)
{
cnt[i]--;
ans.push_back(i+'0');
break;
}
}
for(int i=0;i<10;i++)
{
while(cnt[i])
{
cnt[i]--;
ans.push_back(i+'0');
}
}
cout<<ans<<endl;
return 0;
}
1014: 科学计数法 (20)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
string str;
char fuhao; //符号位
string youxiao; //有效数字
int jiema; //阶码
int main()
{
cin>>str;
fuhao=str[0];
int index=1;
while(str[index]!='E') index++;
youxiao=str.substr(1,index-1);
index+=1;
int cur=index+1;
jiema=0;
while(cur<str.length())
{
jiema=jiema*10+str[cur++]-'0';
}
if(str[index]=='-') jiema*=-1;
if(fuhao=='-') cout<<"-";
if(jiema==0)
{
cout<<youxiao<<endl;
}
else if(jiema<0)
{
cout<<"0.";
for(int i=1;i<(-jiema);i++)
{
cout<<"0";
}
for(int i=0;i<youxiao.length();i++)
{
if(youxiao[i]!='.') cout<<youxiao[i];
}
}
else
{
string qian="";
qian=qian+youxiao[0];
int i,record;
for(i=2;i<youxiao.length();i++)
{
if(jiema>0)
{
qian=qian+youxiao[i];
}
jiema--;
if(jiema==0)
{
record=i;
}
}
if(jiema>=0)
{
while(jiema)
{
qian=qian+'0';
jiema--;
}
cout<<qian<<endl;
}
else
{
cout<<qian<<"."<<youxiao.substr(record+1);
}
}
return 0;
}