本文主要是介绍PAT 甲级 2017年秋季,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 PAT 甲级 1132 Cut Integer
#include <bits/stdc++.h>
using namespace std;
string num;
void Solve(){string s1=num.substr(0,num.size()/2);string s2=num.substr(num.size()/2,num.size()/2);int a,b,c;a=atoi(num.c_str());b=atoi(s1.c_str());c=atoi(s2.c_str());if(b!=0&&c!=0&&a%(b*c)==0) cout<<"Yes\n";else cout<<"No\n";
}
int main() {int n;cin>>n;while(n--){cin>>num;Solve();}
}
2 PAT 甲级 1133 Splitting A Linked List
#include <bits/stdc++.h>
using namespace std;struct Node{int address{-1},val,next{-1},order{-1};
}node[100010];int main() {int head,n,k;cin>>head>>n>>k;for(int i=0;i<n;++i){int ad,da,ne;cin>>ad>>da>>ne;node[ad].address=ad;node[ad].val=da;node[ad].next=ne;}int ctr=0;for(int now=head;now!=-1;now=node[now].next){node[now].order=ctr;++ctr;}sort(node,node+100010,[k](Node &a,Node &b){if(a.order==-1||b.order==-1) return a.order>b.order;if(a.val<0&&b.val<0) return a.order<b.order;if(a.val<0&&b.val>=0) return true;if(a.val>=0&&b.val<0) return false;if(a.val<=k&&b.val>k) return true;if(a.val>k&&b.val<=k) return false;return a.order<b.order;});for(int i=0;i<ctr-1;++i){printf("%05d %d %05d\n",node[i].address,node[i].val,node[i+1].address);}printf("%05d %d %d\n",node[ctr-1].address,node[ctr-1].val,-1);
}
3 PAT 甲级 1134 Vertex Cover
#include <bits/stdc++.h>
using namespace std;
int vs[10010],tmp[10010];
set<int> graph[10010];
int main() {int n,m;cin>>n>>m;for(int i=0;i<m;++i){int a,b;cin>>a>>b;vs[a]++;vs[b]++;graph[a].insert(b);graph[b].insert(a);}int k;cin>>k;while(k--){int nv;cin>>nv;for(int i=0;i<10010;++i) tmp[i]=vs[i];for(int i=0;i<nv;++i){int now;cin>>now;for(auto i:graph[now]){if(tmp[i]!=0) tmp[i]--;}tmp[now]=0;}bool flag=true;for(int i=0;i<10010;++i) if(tmp[i]!=0) flag=false;if(flag) cout<<"Yes\n";else cout<<"No\n";}
}
4 PAT 甲级 1135 Is It A Red-Black Tree
#include <bits/stdc++.h>
using namespace std;
int pre[50],in[50],mp[50];
int lchild[50],rchild[50],height[50];
bool is_red[50],flag;
int Build(int l1,int r1,int l2,int r2){if(l1>r1) return -1;int root=l1;int mid;for(mid=l2;mid<r2&&in[mid]!=root;++mid);int len=mid-l2;lchild[root]=Build(l1+1,l1+len,l2,mid-1);rchild[root]=Build(l1+len+1,r1,mid+1,r2);return root;
}
int Check(int root){// 判断红节点子节点是黑色 if(root!=-1&&is_red[root]){if(lchild[root]!=-1&&is_red[lchild[root]]) flag=false;if(rchild[root]!=-1&&is_red[rchild[root]]) flag=false;}// 判断数目if(root==-1) return 1; int num1=Check(lchild[root]);int num2=Check(rchild[root]);if(num1!=num2) flag=false;return num1+!is_red[root];
}
int main() {int k;cin>>k;while(k--){flag=true;memset(is_red,false,sizeof(is_red));int n;cin>>n;for(int i=0;i<n;++i){int tmp;cin>>tmp;if(tmp<0) {is_red[i]=true;tmp*=-1;}mp[i]=tmp;pre[i]=in[i]=i;}sort(in,in+n,[](int a,int b){return mp[a]<mp[b];});int root=Build(0,n-1,0,n-1);Check(root);if(flag&&!is_red[0]) cout<<"Yes\n";else cout<<"No\n";}
}
这篇关于PAT 甲级 2017年秋季的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!