您的位置:首页 > 其它

浙江大学PAT考试1069~1072(2013-11-2)

2015-09-15 21:06 267 查看
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\n",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\n",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]='\0';
mq[tmp]++;
if(mq[tmp]>ans1||(mq[tmp]==ans1&&strcmp(tmp,ans)<=0))
{
strcpy(ans,tmp);
ans1=mq[tmp];
}
p=0;
}
}

if(p>0)
{
tmp[p]='\0';
mq[tmp]++;
if(mq[tmp]>ans1||(mq[tmp]==ans1&&strcmp(tmp,ans)<=0))
{
strcpy(ans,tmp);
ans1=mq[tmp];
}
}

cout<<ans<<" "<<ans1<<endl;
}
return 0;
}

/*
Can1: "Can a can can a can?  It can!"
*/


1072:

给你居民个数n,加油站个数m,k条路径,加油站到居民距离不能超过的最大值d。

然后接下来给了k条路径。

题目让我们求在全部的加油站中找符合不超过d的加油站。

假设加油站不唯一,按顺序优先考虑以下三个条件:

1.居民最短距离最大的加油站。

2.到全部居民距离之和最小的。

3.编号最小的。

这个题目坑了非常久,没能AC,有两个case过不了,仅仅有22分。。。。





好累。。。

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1e9;

int n,m,k,d;
//居民个数,候选加油站,多少条路,距离限制
int mp[1505][1505]; //存地图
int dis[1505]; //最短路
int visi[1505];

struct node
{
int index;  //编号
int sum;    //总距离
int mi;     //最短的距离
};

int calnumber()
{
char tmp[105];
cin>>tmp;
int ans=0,i;
int len=strlen(tmp);
if(tmp[0]=='G')
{
for(i=1;i<len;i++)
ans=ans*10+(tmp[i]-'0');
return ans;
}
else
{
for(i=0;i<len;i++)
ans=ans*10+(tmp[i]-'0');
return ans+m;
}
}

void input()  //输入函数
{
int i,j;
for(i=0;i<1500;i++)
{
for(j=0;j<1500;j++)
{
if(i==j) mp[i][j]=0;
else mp[i][j]=maxn;
}
}

for(i=0;i<k;i++)
{
int p1=calnumber();
int p2=calnumber();
int weight;
cin>>weight;
mp[p1][p2]=mp[p2][p1]=weight;
}
}

void dijkstra(int p)
{
int i,j;
for(i=0;i<=1500;i++)
{
dis[i]=maxn;
visi[i]=0;
}
dis[p]=0;  //起始点

for(i=1;i<=n+m;i++)
{
int cur,mi=maxn;
for(j=1;j<=n+m;j++)   //从当前结点中找一个距离最小的。拿出来标记
{
if(!visi[j]&&dis[j]<mi)
{
mi=dis[j];
cur=j;
}
}

visi[cur]=1;
for(j=1;j<=n+m;j++)
{
if(dis[j]>dis[cur]+mp[cur][j])
dis[j]=dis[cur]+mp[cur][j];
}
}
}

int main()
{
int i,j;
int flag;
while(cin>>n>>m>>k>>d)
{
flag=0;  //flag=1表示已经有满足要求的station了
input();

node ans;
for(i=1;i<=m;i++)
{
dijkstra(i);
int tot=0,mi=maxn,ma=0;
for(j=m+1;j<=m+n;j++)   //仅仅计算house的距离
{
if(mi>dis[j])
mi=dis[j];
if(ma<dis[j])
ma=dis[j];
tot+=dis[j];
}

if(ma>k) continue;  //超出服务范围

if(flag==0)
{
ans.index=i;
ans.sum=tot;
ans.mi=mi;
flag=1;
}
else
{
int q=0;   //q=1说明要更新
if(mi>ans.mi)  //加油站越远越好
q=1;
else if(mi==ans.mi&&tot<ans.sum)
q=1;
else if(mi==ans.mi&&tot==ans.sum&&i<ans.index)
q=1;
if(q)
{
ans.index=i;
ans.sum=tot;
ans.mi=mi;
}
}

//printf("G%d\n",ans.index);
//printf("%.1f %.1f\n",(double)ans.sum/n,(double)ans.mi);
//printf("%d\n",ans.ma);
//printf("%d\n",ans.sum);
}

if(flag==0)
{
puts("No Solution");
continue;
}
printf("G%d\n",ans.index);
printf("%.1f %.1f\n",(double)ans.mi,(double)ans.sum/n);
}
return 0;
}

/*
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2

2 1 2 10
1 G1 9
2 G1 20
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: