PAT Advanced Level 1088-1091
2014-12-01 13:10
211 查看
1088,分数运算,运算数和结果都要约分
1089,插入排序和归并排序
1090,供应链,求最大深度及其宽度
1091,求连通1的个数,广度搜索3维数组(题目意思太难懂了)
#include <iostream> using namespace std; long long gcd(long long a,long long b){ if(b==0) return a; else return gcd(b,a%b); } void h(long long n11,long long n12,long long n21,long long n22,long long flag){ long long r1,r2; long long g=gcd(n11,n12); if(g<0) g=-g; n11/=g,n12/=g; if(n11*n12<0) cout<<"("; if(n11/n12!=0) cout<<n11/n12; if(n11/n12!=0&&n11%n12!=0) cout<<" "; if(n11%n12!=0&&n11*n12<0&&n11/n12!=0) cout<<abs(n11%n12)<<"/"<<n12; else if(n11%n12!=0) cout<<n11%n12<<"/"<<n12; if(n11/n12==0&&n11%n12==0) cout<<0; if(n11*n12<0) cout<<")"; bool inf = 0; if(flag==1){ cout<<" + "; r1=n11*n22+n21*n12; r2=n12*n22; g=gcd(r1,r2); if(g<0) g=-g; r1/=g,r2/=g; } else if(flag==2){ cout<<" - "; r1=n11*n22-n21*n12; r2=n12*n22; g=gcd(r1,r2); if(g<0) g=-g; r1/=g,r2/=g; } else if(flag==3){ cout<<" * "; r1=n11*n21; r2=n12*n22; g=gcd(r1,r2); if(g<0) g=-g; r1/=g,r2/=g; } else{ cout<<" / "; r1=n11*n22; r2=n12*n21; if(r2!=0){ g=gcd(r1,r2); if(g<0) g=-g; if(g!=0) r1/=g,r2/=g; } else inf = 1; } g=gcd(n21,n22); if(g<0) g=-g; n21/=g,n22/=g; if(n21*n22<0) cout<<"("; if(n21/n22!=0) cout<<n21/n22; if(n21/n22!=0&&n21%n22!=0) cout<<" "; if(n21%n22!=0&&n21*n22<0&&n21/n22!=0) cout<<abs(n21%n22)<<"/"<<n22; else if(n21%n22!=0) cout<<n21%n22<<"/"<<n22; if(n21/n22==0&&n21%n22==0) cout<<0; if(n21*n22<0) cout<<")"; cout<<" = "; if(inf){ cout<<"Inf"<<endl; return; } if(r2<0) r1=-r1,r2=-r2; if(r1*r2<0) cout<<"("; if(r1/r2!=0) cout<<r1/r2; if(r1/r2!=0&&r1%r2!=0) cout<<" "; if(r1%r2!=0&&r1*r2<0&&r1/r2!=0) cout<<abs(r1%r2)<<"/"<<r2; else if(r1%r2!=0) cout<<r1%r2<<"/"<<r2; if(r1/r2==0&&r1%r2==0) cout<<0; if(r1*r2<0) cout<<")"; cout<<endl; } void output(long long n11,long long n12,long long n21,long long n22){ h(n11,n12,n21,n22,1); h(n11,n12,n21,n22,2); h(n11,n12,n21,n22,3); h(n11,n12,n21,n22,4); } int main(){ freopen("C:\\Users\\devon\\Desktop\\input.txt","r",stdin); long long n11, n12, n21, n22; cin>>n11; getchar(); cin>>n12; getchar(); cin>>n21; getchar(); cin>>n22; if(n12<=0||n22<=0) return 0; output(n11,n12,n21,n22); }
1089,插入排序和归并排序
#include <iostream> #include <cstring> using namespace std; void merge(int *a,int l,int r,int len,int n){ int len2; if(r+len-1>=n-1) len2=n-r; else len2=len; int *temp=new int[len+len2]; int i=0,j=0; while(i<=len-1&&j<=len2-1){ if(a[l+i]<=a[r+j]) temp[i+j]=a[l+i],i++; else temp[i+j]=a[r+j],j++; } if(j==len2) memcpy(a+l+i+j,a+l+i,(len-i)*sizeof(int)); memcpy(a+l,temp,(i+j)*sizeof(int)); delete temp; } bool mer(int *a,int *b,int n,int flag){ int step=1; bool finish=0; while(step<n){ for(int i=0;i<=n-step-1;i+=2*step) merge(a,i,i+step,step,n); int index=-1; for(int j=0;j<n;j++){ if(a[j]!=b[j]){ index=j; break; } } if(finish) return true; if(index==-1){ if(flag) finish=1; else return true; } step*=2; } return false; } void ins(int *a,int *b,int n){ bool finish=0; for(int i=1;i<n;i++){ for(int j=i;j>=1;j--){ int temp; if(a[j]<a[j-1]) temp=a[j-1],a[j-1]=a[j],a[j]=temp; } if(finish) return; int index=-1; for(int j=0;j<n;j++){ if(a[j]!=b[j]){ index=j; break; } } if(index==-1) finish=1; } } int main(){ freopen("C:\\Users\\devon\\Desktop\\input.txt","r",stdin); int n; cin>>n; int *a = new int ; for(int i=0;i<n;i++) cin>>a[i]; int *a2 = new int ; for(int i=0;i<n;i++) a2[i]=a[i]; int *b = new int ; for(int i=0;i<n;i++) cin>>b[i]; if(n==1){ cout<<a[0]; return 0; } int flag=mer(a,b,n,0); if(flag){ cout<<"Merge Sort"<<endl; mer(a2,b,n,1); for(int i=0;i<n-1;i++) cout<<a2[i]<<" "; cout<<a2[n-1]<<endl; } else{ cout<<"Insertion Sort"<<endl; ins(a2,b,n); for(int i=0;i<n-1;i++) cout<<a2[i]<<" "; cout<<a2[n-1]<<endl; } delete a; delete a2; delete b; return 0; }
1090,供应链,求最大深度及其宽度
#include <cstdio> #include <cstring> #include <map> #include <vector> #include <queue> #include <iostream> #include <iomanip> using namespace std; int main(){ freopen("C:\\Users\\devon\\Desktop\\input.txt","r",stdin); int n; double p; double per; cin>>n>>p>>per; map<int,vector<int>> m; for(int i=0;i<n;i++){ int temp; cin>>temp; m[temp].push_back(i); } queue<int> q; queue<int> q2; q.push(-1); int layer=0; int count; while(!q.empty()){ int c=q.size(); while(!q.empty()){ int temp=q.front(); q.pop(); int len=m[temp].size(); for(int i=0;i<len;i++) q2.push(m[temp][i]); } layer++; if(q2.size()==0) count=c; while(!q2.empty()){ int temp=q2.front(); q2.pop(); q.push(temp); } } for(int i=2;i<layer;i++){ p=(1.0+per/100)*p; } printf("%.2f %d\n",p,count); }
1091,求连通1的个数,广度搜索3维数组(题目意思太难懂了)
#include <iostream> #include <queue> using namespace std; int main(){ freopen("C:\\Users\\devon\\Desktop\\input.txt","r",stdin); int m,n,l,t; cin>>m>>n>>l>>t; int ret=0; int ***a=new int**[l]; bool ***has=new bool**[l]; for(int i=0;i<l;i++){ a[i]=new int*[m]; has[i]=new bool*[m]; for(int j=0;j<m;j++) a[i][j]=new int ,has[i][j]=new bool ; } for(int i=0;i<l;i++) for(int j=0;j<m;j++) for(int k=0;k<n;k++) a[i][j][k]=0,has[i][j][k]=0; for(int i=0;i<l;i++) for(int j=0;j<m;j++) for(int k=0;k<n;k++) cin>>a[i][j][k]; int d[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; for(int i=0;i<l;i++){ for(int j=0;j<m;j++){ for(int k=0;k<n;k++){ if(a[i][j][k]==0||has[i][j][k]==1) continue; int temp_c=1; queue<int> qx,qy,qz; qx.push(i),qy.push(j),qz.push(k); has[i][j][k]=1; while(!qx.empty()){ int x=qx.front(); int y=qy.front(); int z=qz.front(); qx.pop(),qy.pop(),qz.pop(); for(int i=0;i<6;i++){ if(x+d[i][0]<0||x+d[i][0]>l-1) continue; if(y+d[i][1]<0||y+d[i][1]>m-1) continue; if(z+d[i][2]<0||z+d[i][2]>n-1) continue; if(a[x+d[i][0]][y+d[i][1]][z+d[i][2]]==1&&has[x+d[i][0]][y+d[i][1]][z+d[i][2]]==0){ qx.push(x+d[i][0]); qy.push(y+d[i][1]); qz.push(z+d[i][2]); has[x+d[i][0]][y+d[i][1]][z+d[i][2]]=1; temp_c++; } } } if(temp_c>=t) ret+=temp_c; } } } cout<<ret<<endl; }
相关文章推荐
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)
- PAT (Advanced Level) 1091. Acute Stroke (30)
- PAT (Advanced Level) Practise 1091 Acute Stroke (30)
- PAT (Advanced Level) Practise 1091 Acute Stroke (30)
- 【PAT】【Advanced Level】1088. Rational Arithmetic (20)
- Pat(Advanced Level)Practice--1088(Rational Arithmetic)
- 1091. Acute Stroke (30)【搜索】——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1091. Acute Stroke (30) 3维DFS(10M数据时栈溢出)、3维BFS(AC)
- 【PAT】【Advanced Level】1091. Acute Stroke (30)
- Pat(Advanced Level)Practice--1091(Acute Stroke)
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)
- 1015. Reversible Primes (20) @ PAT (Advanced Level) Practise
- PAT(Advanced Level) 1005 - Spell It Right (水题)
- PAT (Advanced Level) Practise 1010 Radix (25)
- PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- PAT (Advanced Level) Practise 1118 Birds in Forest (25)
- PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- PAT (Advanced Level) Practise 1059 Prime Factors (25)
- PAT (Advanced Level) Practise 1070 Mooncake (25)
- PAT (Advanced Level) Practise 1123 Is It a Complete AVL Tree (30)