11
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise
1069:
由下降序和上升序两个四位数不断相减,然后得到新数据,始终会到达一个数字终止。
递归能够写,简单。
AC代码:
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int p[4]; void dfs(int a,int b) { int del=a-b; printf("%04d - %04d = %04d ",a,b,del); if(del==6174||del==0) return; int i=0; while(i<4) { p[i]=del%10; del/=10; i++; } sort(p,p+4); int m1=p[3]*1000+p[2]*100+p[1]*10+p[0]; int m2=p[0]*1000+p[1]*100+p[2]*10+p[3]; dfs(m1,m2); } int main() { int n; while(cin>>n) { int i=0; while(i<4) { p[i]=n%10; n/=10; i++; } sort(p,p+4); int m1=p[3]*1000+p[2]*100+p[1]*10+p[0]; int m2=p[0]*1000+p[1]*100+p[2]*10+p[3]; dfs(m1,m2); } return 0; }
1070:
依照比值排序。遍历就可以。
AC代码:
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const double eps=1e-12; struct node { double num; double price; }nod[1005]; int cmp(node p1,node p2) { if(p1.price/p1.num>=p2.price/p2.num) return 1; return 0; } int main() { int n,i; double total; while(cin>>n>>total) { for(i=0;i<n;i++) cin>>nod[i].num; for(i=0;i<n;i++) cin>>nod[i].price; sort(nod,nod+n,cmp); double res=0; for(i=0;i<n;i++) { if(total>nod[i].num) { res+=nod[i].price; total-=nod[i].num; } else { res+=nod[i].price*(total/nod[i].num); break; } } printf("%.2f ",res); } return 0; } /* 3 200 180 150 100 7.5 7.2 4.5 */
1071:
就是让你在一段文字中找出现最多次数的单词,模拟就可以。
AC代码:
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<map> #include<cstring> #include<string> #define maxn 1050000 using namespace std; char str[maxn]; char ans[maxn]; char tmp[maxn]; int ans1; map <string,int> mq; int judge(char p1) { if(p1>='0'&&p1<='9') return 1; if(p1>='a'&&p1<='z') return 1; if(p1>='A'&&p1<='Z') return 1; return 0; } int main() { int len,i,p; while(gets(str)) { strcpy(ans,""); //³õʼ»¯ ans1=0; mq.clear(); len=strlen(str); p=0; for(i=0; i<len; i++) { if(judge(str[i])) { if(str[i]>='A'&&str[i]<='Z') str[i]=str[i]+('a'-'A'); tmp[p++]=str[i]; } else if(!judge(str[i])&&p>0) { tmp[p]='