PAT 1139 1138 1137 1136
一个月不写题,有点生疏。。脑子跟不上手速,还可以啦,反正今天很开心。
PAT 1139 First Contact
18/30 找个时间再修bug
23/30
28/30
30/30 补完啦
这道题的细节坑点:
1.输出id需要补全4位 用print("%04d") 5分ok
2.需要舍弃查询的相恋男女的直接边 3分ok
3.大坑 0000 与 -0000 使用int型无法区分男女的. 2分ok 想法:用string输入 看长度和第一位符号位 判0和-0 转int
思路:图论,找男女相恋的中间人。
#include<bits/stdc++.h>
using namespace std;
/*
细节坑点:
1.输出id需要补全4位 用print("%04d") 5分ok
2.需要舍弃查询的相恋男女的直接边 3分ok
3.大坑 0000 与 -0000 使用int型无法区分男女的. 2分ok 想法:用string输入 看长度和第一位符号位 判0和-0 转int
*/
const int maxn = 10000;
vector<int> g[maxn];
int gender[10010];
int n,m;
vector<pair<int,int> >ans;
void init(){
for(int i=0;i<=10000;i++) gender[i] = 0;
}
bool cmp(pair<int,int> a,pair<int,int> b){
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
int stringToAbsInt(string s){
int x = 0;
int flag = 0;
if(s.length() == 5){
flag = 1;
for(int i=1;i<s.length();i++) x = x * 10 + (s[i]-'0');
}else{
flag = 0;
for(int i=0;i<s.length();i++) x = x * 10 + (s[i]-'0');
}
if(flag){
gender[x] = 1;
}else{
gender[x] = 0;
}
return abs(x);
}
int main(){
cin>>n>>m;
init();
for(int i=1;i<=m;i++){
// int u,v;
string uu,vv;
cin>>uu>>vv;
int absu = stringToAbsInt(uu);
int absv = stringToAbsInt(vv);
g[absu].push_back(absv);
g[absv].push_back(absu);
}
int k;
cin>>k;
for(int i=1;i<=k;i++){
int u,v;
cin>>u>>v;
int absu = abs(u);
int absv = abs(v);
int genderu = gender[absu];
int genderv = gender[absv];
int cnt = 0;
for(int c=0;c<g[absu].size();c++){
int cc = g[absu][c];
if(cc == absv) continue; //舍弃 v是u的直接朋友的边
if(gender[cc] != genderu ) continue;
for(int d=0;d<g[absv].size();d++){
int dd = g[absv][d];
if(dd == absu) continue; //舍弃 u是v的直接朋友的边
if(gender[dd] != genderv ) continue;
bool flag = false;
for(int pos = 0;pos<g[cc].size();pos++){
if(g[cc][pos] == dd){
flag = true;
break;
}
}
if(flag){
cnt++;
ans.push_back(make_pair(g[absu][c],g[absv][d]));
}
}
}
cout<<cnt<<endl;
sort(ans.begin(),ans.end(),cmp);
for(int i=0;i<cnt;i++){
printf("%04d %04d
",ans[i].first,ans[i].second); //输出补全4位
}
ans.clear();
}
return 0;
}
1138 Postorder Traversal
数据结构,二叉树先序中序建树后后序遍历
#include<bits/stdc++.h>
using namespace std;
const int maxn = 50010;
int n;
int pre[maxn];
int in[maxn];
int post[maxn];
bool flag = false;
struct node{
int v;
struct node *l;
struct node *r;
};
//建树
node* build(int l,int r,int rootIdx){
if(l > r) return NULL;
node *root = new node();
root->v = pre[rootIdx];
int pos = l;
while(in[pos] != pre[rootIdx]) pos++;
root->l = build(l,pos-1,rootIdx+1);
root->r = build(pos+1,r,rootIdx+(pos-l+1));
return root;
}
//后序遍历 打印第一个被遍历的 结点值
void postTravel(node *Root){
if(flag) return;
if(Root->l == NULL && Root->r == NULL){
cout<<Root->v<<endl;
flag = true;
return;
}
if(Root->l) postTravel(Root->l);
if(Root->r) postTravel(Root->r);
return;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>pre[i];
for(int i=1;i<=n;i++) cin>>in[i];
node *Root = new node();
Root = build(1,n,1); //建树
postTravel(Root);
return 0;
}
1137 Final Grading
数据结构,map,排序常见题型
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
const int maxn = 10010;
int p; //P the number of students having done the online programming assignmen
int m; //the number of students on the mid-term list
int n; //the number of students on the final exam list
struct student{
string id;
int gp;
int gmid;
int gfinal;
int g;
};
unordered_map<string,student > mp;
vector<student> ans;
bool cmp(student stua,student stub){
if(stua.g == stub.g){
return stua.id < stub.id;
}
return stua.g > stub.g;
}
void print(){
for(int i=0;i<ans.size();i++){
cout<<ans[i].id<<" "<<ans[i].gp<<" "<<ans[i].gmid<<" "<<ans[i].gfinal<<" "<<ans[i].g<<endl;
}
}
void input(){
cin>>p>>m>>n;
for(int i=1;i<=p;i++){
string id;
int gp;
cin>>id>>gp;
mp[id].id = id;
mp[id].gp = gp;
mp[id].gmid = -1;
mp[id].gfinal = -1;
mp[id].g = -1;
}
for(int i=1;i<=m;i++){
string id;
int gmid;
cin>>id>>gmid;
mp[id].gmid = gmid;
}
for(int i=1;i<=n;i++){
string id;
int gfinal;
cin>>id>>gfinal;
mp[id].gfinal = gfinal;
}
}
int main(){
input();
for(auto it = mp.begin();it!=mp.end();it++){
if(it->second.gp < 200) continue;
// if(it->second.gfinal < 60 || it->second.gfinal > 100) continue; //吼吼 一开始题意理解错啦 g才是final score
if(it->second.gmid > it->second.gfinal) {
int tempScore = ((it->second.gmid*4 + it->second.gfinal*6) );
if(tempScore % 10 >= 5) it->second.g = tempScore/10+1;
else it->second.g= tempScore/10;
}
else it->second.g = it->second.gfinal;
if(it->second.g >= 60 && it->second.g <=100) ans.push_back(it->second);
}
sort(ans.begin(),ans.end(),cmp);
print();
return 0;
}
1136 A Delayed Palindrome
大数模拟加法进位
#include<bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
vector<int> ans;
/*
reverse
add
*/
void print(){
for(int i=0;i<v1.size();i++) cout<<v1[i];
cout<<" + ";
for(int i=0;i<v2.size();i++) cout<<v2[i];
cout<<" = ";
for(int i=0;i<ans.size();i++) cout<<ans[i];
cout<<endl;
}
bool check(){
int len = ans.size();
for(int i=0;i<len/2;i++){
if(ans[i] != ans[len-i-1]) return false;
}
return true;
}
void clear(){
v1.clear();
v2.clear();
for(int i=0;i<ans.size();i++) v1.push_back(ans[i]);
ans.clear();
}
int main(){
int d;
while(scanf("%1d",&d)!=EOF) v1.push_back(d);
int t = 0;
bool flag = false;
while((t++) < 10){
for(int i=0;i<v1.size();i++) ans.push_back(v1[i]);
if(check()){
for(int i=0;i<ans.size();i++) cout<<ans[i];
cout<<" is a palindromic number.";
flag = true;
break;
}
ans.clear();
int ci = 0;
for(int i=0;i<v1.size();i++) v2.push_back(v1[i]);
reverse(v2.begin(),v2.end());
//大数加法模拟
for(int i=v1.size()-1;i>=0;i--){
int si = v1[i] + v2[i] + ci;
if(si >= 10) {
ci = si/10;
si = si%10;
}else{
ci = 0;
}
ans.push_back(si);
}
if(ci!=0) ans.push_back(ci);
reverse(ans.begin(),ans.end());//反转才是 结果和
ci = 0;
print();
if(check()){
for(int i=0;i<ans.size();i++) cout<<ans[i];
cout<<" is a palindromic number.";
flag = true;
break;
}
clear();
}
if(flag == false) cout<<"Not found in 10 iterations."<<endl;
return 0;
}