PAT甲级刷题集锦
Champhoenix
这个作者很懒,什么都没留下…
展开
-
PAT-A1102(C/C++代码解析)
1102 Invert a Binary Tree (25 分)注:静态树#include <stdio.h>#include <queue>#include <algorithm>using namespace std;const int maxn=88;int notRoot[maxn]={0},num=0,n;struct node{ int lchild; int rchild;}Node[maxn];int strToNum(cha原创 2021-10-26 23:54:56 · 168 阅读 · 0 评论 -
PAT-A1086(C/C++代码解析)
1086 Tree Traversals Again (25 分)注:先序和中序遍历重构二叉树#include <stdio.h>#include <stack>#include <string.h>using namespace std;struct node{ int data; node* lchild; node* rchild;};stack<int> st;const int maxn=88;int pre[maxn原创 2021-10-26 21:06:45 · 151 阅读 · 0 评论 -
PAT-A1020(C/C++代码解析)
1020 Tree Traversals (25 分)注:BFS,重构二叉树#include <stdio.h>#include <queue>#include <string.h>#include <algorithm>using namespace std;const int maxn=88;int post[maxn],pre[maxn],in[maxn];struct node{ int data; node* lchild;原创 2021-10-26 07:41:54 · 209 阅读 · 0 评论 -
PAT-A1091(C/C++代码解析)
1091 Acute Stroke (30 分)注:BFS,三元#include <stdio.h>#include <queue>using namespace std;struct node{ int x,y,z;}Node;int T,m,n,L;int matrix[1300][130][100];int inq[1300][130][100]={0};int X[]={0,0,1,-1,0,0};int Y[]={1,-1,0,0,0,0}原创 2021-10-19 13:45:43 · 160 阅读 · 0 评论 -
PAT-A1040(C/C++代码解析)
1040 Longest Symmetric String (25 分)法一:暴力法#include <stdio.h>#include <algorithm>#include <cstring>#include <iostream>using namespace std; int pan (int i,int j,char* str){ while (i<=j) { if (str[i]!=str[j]) return原创 2021-10-09 20:15:11 · 270 阅读 · 1 评论 -
PAT-A1045(C/C++代码解析)
1045 Favorite Color Stripe (30 分)注:散列,动态规划1.LIS#include <stdio.h>#include <algorithm>#include <cstring>using namespace std; const int maxn=300;int hashtable[maxn];int main (){ int ans=-1,a,j,qq=0,i,w,n,b,m,x; memset (h原创 2021-10-09 15:03:27 · 162 阅读 · 0 评论 -
PAT-A1103(C/C++代码解析)
1103 Integer Factorization (30 分)注:打表,DFS#include <stdio.h>#include <algorithm>#include <vector>using namespace std; int n,k,p,maxFacSum=-1;vector <int> ans,fac,temp;int power (int a){ int z=1; for (int w=0;w<p;w++)原创 2021-10-08 20:47:18 · 192 阅读 · 0 评论 -
PAT-A1007(C/C++代码解析)
1007 Maximum Subsequence Sum (25 分)注:简单题法一:双循环#include <stdio.h> const int maxn=888888;int z[maxn];int main (){ int i,j,n,sum,lmax=-1,last,first,flag=0; scanf ("%d",&n); for (i=0;i<n;i++) { scanf ("%d",&z[i]);原创 2021-10-07 19:17:50 · 199 阅读 · 0 评论 -
PAT-A1038(C/C++代码解析)
1038 Recover the Smallest Number (30 分)注:贪心#include <stdio.h>#include <algorithm>#include <string>#include <iostream>using namespace std;const int maxn=10010;string z[maxn]; bool cmp(string a,string b){ return a+b<原创 2021-10-05 23:57:03 · 149 阅读 · 0 评论 -
PAT-A1067(C/C++代码解析)
1067 Sort with Swap(0, i) (25 分)注:贪心#include <stdio.h>#include <algorithm>using namespace std;const int maxn=100100;int pos[maxn];int main (){ int n,i,a,q=0,left=0; scanf ("%d",&n); for (i=0;i<n;i++) { scanf原创 2021-10-05 19:48:44 · 178 阅读 · 0 评论 -
PAT-A1032(C/C++代码解析)
1032 Sharing (25 分)注:静态链表#include <stdio.h>#include <algorithm>using namespace std;const int maxn=100010;struct Node{ int address,next; char alp; int flag=0;}node[maxn];int main (){ int begin1,begin2,address,n,i; scanf ("原创 2021-09-29 13:26:03 · 182 阅读 · 0 评论 -
PAT-A1037(C/C++代码解析)
1037 Magic Coupon (25 分)注:贪心#include <stdio.h>#include <algorithm>using namespace std;const int maxn=888888; int a1[maxn]={0},a2[maxn]={0},b1[maxn]={0},b2[maxn]={0};bool cmp (int a,int b){ return a>b;}int main (){ int sum=原创 2021-10-05 15:56:52 · 190 阅读 · 0 评论 -
PAT-A1097(C/C++代码解析)
1097 Deduplication on a Linked List (25 分)注:静态链表#include <stdio.h>#include <algorithm>#include <math.h>#include <stdlib.h>using namespace std;const int maxn=100010;int hashtable[maxn]={0};struct Node{ int address,next;原创 2021-10-03 16:20:41 · 162 阅读 · 1 评论 -
PAT-A1052(C/C++代码解析)
1052 Linked List Sorting (25 分)注:静态链表#include <stdio.h>#include <algorithm>using namespace std;const int maxn=100010;struct Node{ int address,next; int data; int flag;}node[maxn];bool cmp (struct Node a,struct Node b){ if (a.fla原创 2021-10-02 00:46:41 · 151 阅读 · 0 评论 -
PAT-A1074(C/C++代码解析)
1074 Reversing Linked List (25 分)注:静态链表#include <stdio.h>#include <algorithm>using namespace std;const int maxn=100010;struct Node{ int address,data,next; int order;}node[maxn];bool cmp (Node a,Node b){ return a.order<b.order;原创 2021-09-29 11:41:11 · 173 阅读 · 0 评论 -
PAT-A1022(C/C++代码解析)
1022 Digital Library (30 分)注:vector+结构体+sort排序#include <stdio.h>#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;bool cmp (struct bookk,struct bookk);struct bookk{ int i原创 2021-09-26 20:01:41 · 170 阅读 · 0 评论 -
PAT-A1071(C/C++代码解析)
1071 Speech Patterns (25 分)注:map映射#include <stdio.h>#include <map>#include <iostream>#include <string>int check(char);using namespace std;map<string,int>mp;int main (){ int i=0,max=-1; string str,k; getl原创 2021-09-25 20:24:23 · 164 阅读 · 0 评论 -
PAT-A1054(C/C++代码解析)
1054 The Dominant Color (20 分)注:map映射#include <stdio.h>#include <map>using namespace std;map<int,int>count;int main (){ int m,n,k,q,max=-1; scanf ("%d %d",&m,&n); for (int i=0;i<n;i++) { for (int j=原创 2021-09-25 19:28:02 · 148 阅读 · 0 评论 -
PAT-A1100(C/C++代码解析)
1100 Mars Numbers (20 分)注:打表,map#include <stdio.h>#include <string>#include <map>#include <iostream>using namespace std;map <string,int> strToNum; //火星文->数字 string digit[13]={"tret","jan","feb","mar","apr","may"原创 2021-09-23 20:04:01 · 138 阅读 · 0 评论 -
PAT-A1051(C/C++代码解析)
1051 Pop Sequence (25 分)注:栈处理#include <stdio.h>#include <stack>using namespace std;stack <int> st;int main (){ int l,j,i,m,n,arr[1010],flag=0,current=1; scanf ("%d %d %d",&l,&m,&n); for (i=0;i<n;i++)原创 2021-09-20 13:03:56 · 194 阅读 · 0 评论 -
PAT-A1073(C/C++代码解析)
1073 Scientific Notation (20 分)注:字符串处理#include <stdio.h>#include <string.h>int main (){ char z[100001]; scanf ("%s",z); if (z[0]=='-') printf ("-"); int i,j,l=strlen(z),id,e; for (i=0;i<l;i++) { if (z[i]=='E') { id=i;原创 2021-09-18 16:27:21 · 216 阅读 · 0 评论 -
PAT-A1078(C/C++代码解析)
1078 Hashing (25 分)注:散列,素数#include <cstdio>#include <cmath>int isPrime (int n);int main(){ int p,n,i=0,hashtable[88888]={0},j,k,a,flag; scanf ("%d %d",&p,&n); if (isPrime(p)==0) { for (i=p+1;;i++) {原创 2021-09-18 13:36:00 · 175 阅读 · 0 评论 -
PAT-A1015(C/C++代码解析)
1015 Reversible Primes (20 分)注:素数和进制处理#include <cstdio>#include <cmath>int isPrime (int n);int main(){ int k,n,i=0,z[888888],sum=0,j; while (1) { scanf ("%d",&k); if (k<0) break; scanf ("%d",&n)原创 2021-09-18 12:36:22 · 98 阅读 · 0 评论 -
PAT-A1083(C/C++代码解析)
1083 List Grades (25 分)#include <stdio.h>//#include <string.h>#include <algorithm>struct student{ char name[88]; char id[88]; int grade; int flag=0;};bool cmp (student a,student b); using namespace std;int main (){ int a,b原创 2021-09-17 21:07:59 · 167 阅读 · 0 评论 -
PAT-A1077(C/C++代码解析)
1077 Kuchiguse (20 分)#include <stdio.h>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main (){ int n,i,minl=888,l,j,flag=0; scanf ("%d",&n); char k[n][888]; getchar(); for (i=0;i&l原创 2021-09-17 16:35:03 · 200 阅读 · 0 评论 -
PAT-A1001(C/C++代码解析)
1001 A+B Format (20 分)注:简单字符串处理#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main (){ int a,b; scanf ("%d %d",&a,&b); int ans=a+b,i=0,z[1000100]; if (ans<0) { printf ("-"); an原创 2021-09-17 15:20:29 · 173 阅读 · 0 评论 -
PAT-A1061(C/C++代码解析)
1061 Dating (20 分)注:简单字符串处理#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main (){ char a[88],b[88],c[88],d[88]; int i,j; char week[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"}; scanf原创 2021-09-17 14:53:50 · 168 阅读 · 0 评论 -
PAT-A1005(C/C++代码解析)
1005 Spell It Right (20 分)#include <stdio.h>#include <string.h>int main (){ char z[1001],zimu[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; gets(z); int i,sum=0,a,shuzi[10],q=0; if (z[0]-48==0) print原创 2021-09-16 21:49:46 · 185 阅读 · 0 评论 -
PAT-A1062(C/C++代码解析)
1062 Talent and Virtue (25 分)注:排序题#include <stdio.h>#include <algorithm>struct student{ int id; int de; int cai;};using namespace std;bool cmp (student a,student b){ if (a.cai+a.de!=b.cai+b.de) return a.de+a.cai>b.de+b.cai;原创 2021-09-16 13:30:57 · 313 阅读 · 1 评论 -
PAT-A1085(C/C++代码解析)
1085 Perfect Sequence (25 分)注:两种方法1.two pointers#include <stdio.h>#include <algorithm>using namespace std;int main (){ int i,n,p,j=0,dis=0; scanf ("%d %d",&n,&p); long long z[n]; for (i=0;i<n;i++) scanf ("%lld",&z原创 2021-09-16 00:12:37 · 143 阅读 · 1 评论 -
PAT-A1029(C/C++代码解析)
1029 Median (25 分)注:two pointers技巧1.合并序列思路#include <stdio.h>const int maxa=1000010;int a[maxa],b[maxa];int main (){ int n,m,i,j=0,index=0; int z[maxa]; scanf ("%d",&n); for (i=0;i<n;i++) scanf ("%d",&a[i]); scanf ("%d",&m原创 2021-09-15 19:20:11 · 236 阅读 · 0 评论 -
PAT-A1008(C/C++代码解析)
1008 Elevator (20 分)注:数学问题,简单数学#include <stdio.h>int main (){ int n,a,i,ans=0; scanf ("%d",&n); if (n==1) { scanf ("%d",&a); ans=ans+a*6+5; printf ("%d",ans); } else { scanf ("%d",&a); ans=ans+a*6+5; int now=a,to;原创 2021-09-15 14:14:32 · 149 阅读 · 0 评论 -
PAT-A1104(C/C++代码解析)
1104 Sum of Number Segments (20 分)注:1.数学问题找规律2.测试点2 long double#include <stdio.h>int main (){ int i,n; scanf ("%d",&n); double z[n]; long double ans=0.00; for (i=0;i<n;i++) scanf ("%lf",&z[i]); for (i=0;i<n;i++) { an原创 2021-09-15 13:37:29 · 171 阅读 · 0 评论 -
PAT-A1028(C/C++代码解析)
1028 List Sorting (25 分)注:注意cmp的排序#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct student{ int id; char name[10]; int grade;};bool cmp1(student,student);bool cmp2(student,student);bool cm原创 2021-09-15 00:38:18 · 127 阅读 · 0 评论 -
PAT-A1070(C/C++代码解析)
1070 Mooncake (25 分)注:贪心算法#include <stdio.h>//#include <string.#include <algorithm>using namespace std;struct mooncake{ double ton; double yuan; double per;};bool cmp(mooncake,mooncake);int main (){ int n,i; double ans,sum;原创 2021-09-14 23:22:44 · 164 阅读 · 0 评论 -
PAT-A1050(C/C++代码解析)
1050 String Subtraction (20 分)注:散列#include <stdio.h>#include <string.h>int main (){ char a[10001],b[10001]; int i,hash[130]={0}; gets(a); gets(b); for (i=0;i<strlen(b);i++) hash[b[i]]++; for (i=0;i<strlen(a);i++) { if (has原创 2021-09-14 18:57:37 · 173 阅读 · 0 评论 -
PAT-A1048(C/C++代码解析)
1048 Find Coins (25 分)1.two pointers技巧#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main (){ int i,n,su; scanf ("%d %d",&n,&su); int num[n]; for (i=0;i<n;i++) scanf ("%d",&nu原创 2021-09-14 18:41:20 · 175 阅读 · 0 评论 -
PAT-A1041(C/C++代码解析)
1041 Be Unique (20 分)注:散列#include <stdio.h>#include <string.h>int main (){ int n,flag=0; scanf ("%d",&n); int i,hash[100001]={0},bet[n]; for (i=0;i<n;i++) { scanf ("%d",&bet[i]); hash[bet[i]]++; } for (i=0;i<n;i+原创 2021-09-14 17:52:50 · 155 阅读 · 0 评论 -
PAT-A1092(C/C++代码解析)
1092 To Buy or Not to Buy (20 分)注:散列#include <stdio.h>#include <string.h>int main (){ char shop[1010],eva[1010]; scanf ("%s %s",shop,eva); int i,hash[130]={0},q=0; for (i=0;i<strlen(shop);i++) hash[shop[i]]++; for (i=0;i<strl原创 2021-09-14 17:00:35 · 249 阅读 · 0 评论 -
PAT-A1069(C/C++代码解析)
1069 The Black Hole of Numbers (20 分)注:1.输出为0的问题#include <stdio.h>#include <algorithm>bool cmp (int a,int b);int tonum (int z[]);void toarray (int n,int z[]);using namespace std;int main (){ int i=0,n,z[5],max,min; scanf ("%d",&原创 2021-09-14 14:21:46 · 86 阅读 · 0 评论