PAT-B 1031 查验身份证
AC代码
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main(){
int w[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int N;
char M[11]={'1','0','X','9','8','7','6','5','4','3','2'};
cin>>N;
vector<string> v;
while(N--){
string id;
cin>>id;
int sum=0,flag=1;
for(int i=0;i<id.length()-1;i++) {
if(id[i]>='0'&&id[i]<='9')sum+=(id[i]-'0')*w[i];
else { flag=0;v.push_back(id);break;}
}
if(flag){
int Z=sum%11;
if(M[Z]!=id[id.length()-1])v.push_back(id);
}
}
if(v.size()==0) cout<<"All passed";
else {
for(int i=0;i<v.size();i++) cout<<v[i]<<endl;
}
return 0;
}
PAT-B 1032 挖掘机技术哪家强
#include<iostream>
using namespace std;
int main(){
int N,maxG=-1,maxId;
cin>>N;
int sch[N+1]={0};//下标对应学校编号
for(int i=1;i<=N;i++){
int id,grade;
cin>>id>>grade;
sch[id]+=grade;
if(sch[id]>maxG){
maxG=sch[id];
maxId=id;
}
}
cout<<maxId<<" "<<maxG;
return 0;
}
PAT-B 1033 旧键盘打字
思路:定义整型ascii数组对应,下标对应128种字符的ASCII值,若键盘坏了其值为1,正常为0。用str.find()找‘+’上档键是否坏了,若无找到其值等于string::npos。遍历待输出字符串str2,若str2[i]是大写字母且’+’坏了 与 ascii[str2[i]]==1 不输出 仅输出ascii[str2[i]]==0
AC代码
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
int main(){
string str1,str2;
getline(cin,str1);//测点2
getline(cin,str2);
int flag=0;//代表上档键是否坏了
if(str1.find('+')!=string::npos) flag=1;
int ascii[128]={0};
for(int i=0;i<str1.length();i++) {
if(isupper(str1[i])) ascii[tolower(str1[i])]=1;
ascii[str1[i]]=1;
}
for(int i=0;i<str2.length();i++) {
if(isupper(str2[i])&&flag) continue;
else {
if(ascii[str2[i]]==0) cout<<str2[i];
}
}
return 0;
}
PAT-B 1034 有理数的四则运算
分开计算 不要混一起求 加减乘除运算没什么好说的,格式就是麻烦了
AC代码
#include<iostream>
#include<cmath>
long long int fz1,fm1,fz2,fm2;
using namespace std;
long long int GCD(long long int n1,long long int n2){
return n2==0?n1:GCD(n2, n1 % n2);
/* while(n1!=n2){//最后测试点超时
if(n1>n2) n1=n1-n2;
else n2=n2-n1;
}
return n1;*/
}
void Print(long long int z,long long int m){
int cnt1=1,cnt2=1;
if(z==0) {
cout<<"0";
return;
}
if(m==0) {
cout<<"Inf";
return;
}
if(z<0) { z=abs(z);cnt1=-1;}
if(m<0) { m=abs(m);cnt2=-1;}
int cnt=cnt1*cnt2;
if(z==m) {//结果的分子分母相等
if(cnt1==1) cout<<"1";
else cout<<"(-1)";
return;
}
if(cnt==-1) cout<<"(-";
int gcd=GCD(z,m);
z/=gcd;
m/=gcd;
if(z/m) cout<<z/m;
if(z/m&&z%m) cout<<" ";
if(z%m) cout<<z%m<<"/"<<m;
if(cnt==-1) cout<<")";
}
void Add(){
long long int rz,rm;
rz=fz1*fm2+fz2*fm1;
rm=fm1*fm2;
Print(fz1,fm1);
cout<<" + ";
Print(fz2,fm2);
cout<<" = ";
Print(rz,rm);
}
void Sub(){
long long int rz,rm;
rz=fz1*fm2-fz2*fm1;
rm=fm1*fm2;
Print(fz1,fm1);
cout<<" - ";
Print(fz2,fm2);
cout<<" = ";
Print(rz,rm);
}
void Multi(){
long long int rz,rm;
rz=fz1*fz2;
rm=fm1*fm2;
Print(fz1,fm1);
cout<<" * ";
Print(fz2,fm2);
cout<<" = ";
Print(rz,rm);
}
void Div(){
long long int rz,rm;
rz=fz1*fm2;
rm=fz2*fm1;
Print(fz1,fm1);
cout<<" / ";
Print(fz2,fm2);
cout<<" = ";
Print(rz,rm);
}
int main(){
scanf("%lld/%lld %lld/%lld",&fz1,&fm1,&fz2,&fm2);
Add();
cout<<endl;
Sub();
cout<<endl;
Multi();
cout<<endl;
Div();
return 0;
}
PAT-B 1035 插入与归并
没什么好说的 刚开始自己思路错 看了柳大神的
AC代码
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int N,flag=0,pos=0;
cin>>N;
vector<int> v1(N),v2(N);
for(int i=0;i<N;i++) cin>>v1[i];
for(int i=0;i<N;i++) cin>>v2[i];
for(int i=0;i<N-1;i++)
if(v2[i]>v2[i+1]) { pos=i;break;}
for(int i=pos+1;i<N;i++)
if(v1[i]!=v2[i]) { flag=1;break;}
if(flag==0){
cout<<"Insertion Sort"<<endl;
sort(v1.begin(),v1.begin()+(pos+2));
} else {
cout<<"Merge Sort"<<endl;
int syn=1,k=1;
while(syn){
syn=0;
for(int i=0;i<N;i++){
if(v1[i]!=v2[i]){
syn=1;
break;
}
}
k=k*2;
int i;
for(i=0;i+k<N;i+=k)
sort(v1.begin()+i,v1.begin()+i+k);
sort(v1.begin()+i,v1.end());
}
}
for(int i=0;i<N;i++){
if(i!=0) cout<<" ";
cout<<v1[i];
}
return 0;
}