1001 A+B Format (20 分)
分析 :题目中的standard format是从最后一位往前每三位一个逗号,而不是从第一位往后
总结 :题目中的要求可能和你先入为主的印象不一样,即使样例和你想的是一样的
Time : 7 min 44 s
#include <bits/stdc++.h> using namespace std; vector<int> ans; int main() { int a,b,sum; cin>>a>>b; sum=a+b; if(sum>=0) { if(sum==0) { cout<<0; return 0; } while(sum) { ans.push_back(sum%10); sum/=10; } for(int i=ans.size()-1;i>=0;i--) { cout<<ans[i]; if(i%3==0&&i) cout<<","; } } else { sum*=-1; cout<<"-"; while(sum) { ans.push_back(sum%10); sum/=10; } for(int i=ans.size()-1;i>=0;i--) { cout<<ans[i]; if(i%3==0&&i) cout<<","; } } return 0; }1002 A+B for Polynomials (25 分)
分析:where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤1000.
感觉PAT的题表达都很不清楚要靠猜,样例也不会说明,这题如果系数和为0不输出
总结:新单词polynomial———多项式,exponents——指数,coefficient——系数
Time: 16 min 23 s
#include <bits/stdc++.h> using namespace std; typedef long long ll; map<int,double> mp; struct Node { int x; double y; bool operator < (const Node &T) const { return x>T.x; } }pp[25]; int main() { int k,x; double y; scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d%lf",&x,&y); mp[x]+=y; } scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d%lf",&x,&y); mp[x]+=y; } int cnt=0; map<int,double>::iterator it; for(it=mp.begin();it!=mp.end();it++) { if((*it).second==0) continue; pp[++cnt].x=(*it).first; pp[cnt].y=(*it).second; } sort(pp+1,pp+1+cnt); printf("%d",cnt); for(int i=1;i<=cnt;i++) printf(" %d %.1f",pp[i].x,pp[i].y); return 0; }1048 Find Coins (25分)
分析 :找自信,乱模拟
Time : 5 min
code :
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1002; int vis[maxn],a[maxn*1000]; int main() { int n,m,x=5000,y=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n); int flag=0; for(int i=1;i<=n;i++) { if(m>a[i]&&vis[m-a[i]]) { flag=1; if(m-a[i]<x) x=m-a[i],y=a[i]; if(a[i]<x) x=a[i],y=m-a[i]; if(x>y) swap(x,y); } vis[a[i]]=1; } if(flag) printf("%d %d",x,y); else printf("No Solution"); return 0; }
1093 Count PAT's (25分)
分析:递推
Time : 2 min 30 s
code:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1000000007; const int maxn = 1e6 + 5; char s[maxn]; ll f[maxn][3]; int main() { scanf("%s",s+1); int n=strlen(s+1); for(int i=1;i<=n;i++) { if(s[i]=='P') f[i][0]=(f[i-1][0]+1)%mod,f[i][1]=f[i-1][1],f[i][2]=f[i-1][2]; else if(s[i]=='A') f[i][0]=f[i-1][0],f[i][1]=(f[i-1][0]+f[i-1][1])%mod,f[i][2]=f[i-1][2]; else if(s[i]=='T') f[i][0]=f[i-1][0],f[i][1]=f[i-1][1],f[i][2]=(f[i-1][2]+f[i-1][1])%mod; } printf("%lld",f[n][2]); return 0; }