您的位置:首页 > 其它

浙江大学2012研究生机试

2014-02-27 18:35 295 查看
(1)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxs=90;
const int maxn=40;
char s[maxs];
char a[maxn][maxn];
int main()
{
while(scanf("%s",s)!=EOF)
{
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
a[i][j]=' ';
int n1,n3;
n1=n3=(strlen(s)+2)/3;
int n2=strlen(s)+2-2*n1;
for(int i=0;i<n1;i++)
a[i][0]=s[i];
for(int j=1;j<n2;j++)
a[n1-1][j]=s[n1+j-1];
for(int k=n1-2;k>=0;k--)
a[k][n2-1]=s[strlen(s)-1-k];
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
printf("%c",a[i][j]);
puts("");
}

}
return 0;
}

(2)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int maxn=100000+10;

int vis[maxn];
int ad1,ad2,a,b,n;
char c,d[6];
int main()
{
// cin>>d;
// sscanf(d,"%d",&b);
// cout<<d<<" "<<b<<endl;
while(cin>>ad1>>ad2>>n)
{
memset(vis,0,sizeof(vis));
int maxnum=-1,flag=0,key=-1;
while(n--)
{
cin>>a>>c>>d;
sscanf(d,"%d",&b);
for(int i=0;i<5;i++)
if(d[i]!='0')
break;
else
vis[b]+=10;
// cout<<a<<c<<b<<endl;
maxnum=max(maxnum,b);
if(b!=-1)
{
vis[b]++;
if(vis[b]%10==2)
{
flag=1;
key=b;
}
}
}
if(flag)
{
vis[key]/=20;
for(int j=0;j<vis[key];j++)
cout<<0;
cout<<key<<endl;
}
else cout<<-1<<endl;
}
return 0;
}


(3)
///模块化,写出子函数,这样思路似乎会清晰很多
#include <iostream>
#include <cstdio>
#include <cfloat>
#include <algorithm>
using namespace std;

const int maxn=500+10;
float Cmax,D,Davg;
int N;
float cap,tot;
typedef struct pd
{
float p,d;///faluty
}pd;
pd price[maxn];

bool cmp(pd a,pd b)
{
return a.d<b.d;
}
void station(int &s)
{
int j=s+1;
float minp=FLT_MAX,minnum=-1;
while(price[j].d-price[s].d<=Davg*Cmax)
{
if(price[j].p<minp)
{
minp=price[j].p;
minnum=j;
}
if(price[j].p<price[s].p)
{
float cost=(price[j].d-price[s].d)/Davg;
if(cost>cap)
{
tot+=price[s].p*(cost-cap);
cap=0;
}
else cap-=cost;
s=j;
return;
}
j++;
}
///如果一路上没有车站比所在的车站还要便宜
j=minnum;
tot+=(Cmax-cap)*price[s].p;///装满///消费多少
cap=Cmax-(price[j].d-price[s].d)/Davg;///剩余多少容量
s=j;///到了哪个站
}
int main()
{
while(scanf("%f%f%f%d",&Cmax,&D,&Davg,&N)!=EOF)
{
for(int i=0;i<N;i++)
scanf("%f%f",&price[i].p,&price[i].d);
price
.d=D,price
.p=0;
sort(price,price+N,cmp);///faulty
if(price[0].d>0)
{
printf("The maximum travel distance = 0.00\n");
continue;
}
int flag=0;
for(int i=0;i<N;i++)
if(price[i+1].d-price[i].d>Cmax*Davg)
{
printf("The maximum travel distance = %.2f\n",price[i].d+Cmax*Davg);
flag=1;
break;
}
if(flag) continue;
int s=0;
cap=0,tot=0;
while(s<N)
{
station(s);
}
printf("%.2f\n",tot);
}
return 0;
}


(4)需要用到并查集
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>

using namespace std;

const int maxn=1000+10;
int p[2*maxn],len[2*maxn],v[2*maxn];
map<string,int> people,ham;
int n,k;

int find(int x)
{
return x==p[x]?x:p[x]=find(p[x]);//faulty
}

int main()
{
while(cin>>n>>k)
{
people.clear();
map<string,int>::iterator iter;
string s,t;
int i,tlen;
for(i=1;i<=2*n;i++)
p[i]=i;
i=1;
while(n--)
{
cin>>s>>t>>tlen;
iter=people.find(s);
if(iter!=people.end())///如果已经在图中
{
len[people[s]]+=tlen;///修改对应该string的结点通话时间
}
else
{
people[s]=i++;///新进入的结点
len[people[s]]=tlen;
}
iter=people.find(t);
if(iter!=people.end())
{
len[people[t]]+=tlen;///修改对应该string的结点通话时间
}
else
{
people[t]=i++;///新进入的结点
len[people[t]]=tlen;
}
int x=find(people[s]),y=find(people[t]);
if(x!=y)//x和y不在同一个集合里
{
p[x]=y;//合并
}
}
int j,h,num=0;
memset(v,0,sizeof(v));
ham.clear();
for(j=1;j<i;j++)
{
if(!v[j])
{
int root=find(j),maxnum=-1,temp,m=0,tot=0;
for(h=1;h<i;h++)
if(root==find(h))
{
v[h]=1;
m++;
tot+=len[h];
if(len[h]>maxnum)
{
maxnum=len[h];
temp=h;///接下来需要根据temp的值找对应的字符串,m中存储的就是对应的人数
}
}
if(tot<=2*k||m<=2) continue;
for(iter=people.begin();iter!=people.end();iter++)
if(iter->second==temp)
{
ham[iter->first]=m;
num++;
break;
}
}
}///num记录有几个帮派
cout<<num<<endl;
for(iter=ham.begin();iter!=ham.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐