PTA 1081~1085
今天总结一下,昨天做的pta的题~
[1081:检查密码](https://pintia.cn/problem-sets/994805260223102976/problems/994805261217153024)
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int main(){ int n; cin >> n; getchar();// 吃回车 for(int i = 0 ; i < n ; i ++){ int numa = 0,numn = 0, nump = 0; string u; getline(cin,u); //getline int len = u.length(); if(len < 6) { cout <<"Your password is tai duan le."<<endl; } else{ for(int i = 0; i < len ; i ++){ if((u[i]<='z' && u[i]>='a') || (u[i]>='A'&& u[i] <= 'Z')){ numa++; } else if(u[i]>='0' && u[i]<='9'){ numn++; } else{ if(u[i] != '.') nump++; } } //cout << numa << " " << numn << " " << nump << endl; if(nump){ cout << "Your password is tai luan le."<<endl; continue; } else if(!numa&&numn){ cout << "Your password needs zi mu."<<endl; continue; } else if(numa&&!numn){ cout <<"Your password needs shu zi." <<endl; continue; } else if(numa&&numn){ cout << "Your password is wan mei." <<endl; continue; } } } }
1:这道题主要注意的是,输入的密码会有空格,所以不能简单地用string的cin 输入,得用getline输入一整行,注意格式getline(cin ,s);
2:在用getline之前还有输入的时候,一定要加getchar(),这里的getchar()吃回车
[1082 射击比赛](https://pintia.cn/problem-sets/994805260223102976/problems/994805260990660608)
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> using namespace std; struct node{ string id; int x,y; double d; }N[10005]; int cmp(node A,node B){ return A.d < B.d; } int main(){ int n; cin >> n; for(int i = 0 ; i < n ; i ++) { cin >>N[i].id; cin >> N[i].x >> N[i].y; N[i].d = sqrt(N[i].x*N[i].x+N[i].y*N[i].y); //cout << N[i].d << endl; } sort(N,N+n,cmp); cout << N[0].id << " " << N[n-1].id; }
这道题其实就是求两点之间的距离
[1083 是否存在相等的差](https://pintia.cn/problem-sets/994805260223102976/problems/994805260780945408)
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<map> using namespace std; map<int,int,greater<int>>M; int num[10005]; int main(){ int n; cin >> n; for(int i = 1 ; i <= n ; i ++){ cin >> num[i]; int cha= abs(num[i] - i); M[cha]++; } map<int,int>::iterator it; for(it = M.begin();it!=M.end();it++){ if(it->second == 1) continue; cout << it->first << " " << it->second << endl; } }
这道题需要让你把差从大到小的排序,并且输入相应的个数,当然仔细读题,发现个数出现一次是不需要输入的
首先让我想到STL库里的map,但是这里需要从大到小,所以用
map<int,int,greater<int>>M;来定义一个key值从大到小的Map;
map<int,int,less<int>>M; 用来定义一个key值从小到大的Map;(当然变量名随意啦~)
[1084 外观数列](https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120)
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<map> using namespace std; string a; map<int,int>M; int n,m; void form(string &a,map<int,int>&M,int num ){ if(num == m){ cout << a << endl; return ; } string b; int len = a.length(); int ans = 1; for(int i = 0 ; i < len; i ++){ if(a[i+1] == a[i]){ ++ans; continue; }else{ M[a[i]-'0'] = ans; b += ( a[i]-'0' +'0' ); b += ( ans +'0'); ans = 1; } } a = b; b.clear(); M.clear(); form(a,M,num+1); } int main(){ cin >> n >> m; a = n+'0'; form(a,M,1); }
这道题我用的思想主要是递归~
[1085 pat单位排行](https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400)
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<map> using namespace std; struct node{ double score; int num=0; string sc; }re[100005]; int cmp(node A,node B){ if(A.score != B.score){ return A.score > B.score; }else{ if(A.num != B.num) return A.num < B.num; else return A.sc < B.sc; } } map<string,node>M; int main(){ int n; cin >> n; for(int i = 1 ; i <= n ;i ++){ string a,sch; double p; double pp; cin >> a >> p >> sch; transform( sch.begin(), sch.end() ,sch.begin(), ::tolower); //cout << sch <<endl; if(a[0] == 'B') p /=1.5; else if(a[0] == 'T') p*=1.5; pp = p; M[sch].score+=pp; M[sch].num++; } map<string,node>::iterator it; int i = -1; for(it = M.begin(); it!=M.end(); it++){ re[++i].sc = it->first; re[i].num = it->second.num; re[i].score = (int)it->second.score; } sort(re,re+i+1,cmp); int rank = 0; int ll = 0; cout << M.size() << endl; for(int j = 0 ; j <= i ;j ++){ if(re[j].score == re[j-1].score){ ll++; cout << rank << " "; }else{ cout <<(rank+ll+1) <<" "; rank += ll +1; ll = 0; } cout << re[j].sc << " " << re[j].score << " " <<re[j].num << endl; } }
首先一定要注意成绩的计算,double类型,并且输出的时候需要输出整数,呢就再变为int类型
第二,排名的输入读者应该仔细思考一下,之前没怎么全面,提交一直是20,好在终于发现了错误并且改正