2019浙江大学考研复试上机题

1.

#include<cstdio>
#include<cmath>
#include<string>
#include<vector>
using namespace std;
const int maxn = 20000;
bool isPrime(long long x)
{
	if(x<=1) return false;
	long long sqr = (int)sqrt(1.0*x);
	for(long long i=2; i<=sqr; i++){
		if(x%i==0) return false;
	}
	return true;
}
int p[maxn];
void init()
{
	p[0] = 2;
	int cnt=1;
	for(int i=3; cnt<maxn;i+=2){
		if(isPrime(i)){
			p[cnt++]=i;
		}
	}
}
struct factor{
	int d;
	int ep;
	factor(){
		ep = 0;
	}
}fac[maxn];
string deal(int x)
{
	string s;
	do{
		s.push_back(x%10+'0');
		x/=10;
	}while(x!=0);
	return s;
}
long long Stoi(string s)
{
	long long ans=0;
	for(int i=0; i<s.size(); i++){
		ans=ans*10+s[i]-'0';
	}
	return ans;
}
int main()
{
	int n,i,cnt;
	scanf("%d",&n);
	if(n==1){
		printf("1\nNo\n");
		return 0;
	}
	init();
	cnt = 0;
	int sqr = (int)sqrt(1.0*n);
	for(int i=0; p[i]<=sqr;i++){
		if(n%p[i]==0){
			fac[cnt].d = p[i];
			while(n%p[i]==0){
				fac[cnt].ep++;
				n/=p[i];
			}
			cnt++;
		}
	}
	if(n!=1){
		fac[cnt].d = n;
		fac[cnt].ep = 1;
		cnt++;
	}
	string ans,s;
	for(int i=0; i<cnt; i++){
		s = deal(fac[i].d);
		for(int i=s.size()-1;i>=0;i--){
			ans.push_back(s[i]);
		}
		if(fac[i].ep>1){
			s = deal(fac[i].ep);
			for(int i=s.size()-1;i>=0;i--){
				ans.push_back(s[i]);
			}
		}
	}
	long long ans1 = Stoi(ans);
	printf("%lld\n",ans1);
	if(isPrime(ans1))printf("Yes\n");
	else printf("No\n");
	return 0;
}

2.

#include<cstdio>
#include<cstdlib>
const int maxn = 100010;
struct Node{
	int data;
	int address;
	int next;
}node[maxn];

int main()
{
	int fadd,n,k,address,data,next,p;
	scanf("%d%d%d",&fadd,&n,&k);
	for(int i=0; i<n; i++){
		scanf("%d%d%d",&address,&data,&next);
		node[address].address = address;
		node[address].data = data;
		node[address].next = next;
	}
	int head = 100001;
	node[head].next = fadd;
	int pL,pMid,pR;
	pL = head;
	pMid = node[pL].next;
	pR = node[pMid].next;
	for(int i=0; i<k; i++){
		if(i==0) node[pMid].next = -1;
		else node[pMid].next = pL;
		pL = pMid;
		pMid = pR;
		pR = node[pMid].next;
	}
//	p = pL;
//	while(p!=-1){
//		printf("%05d %d ",p,node[p].data);
//		if(node[p].next!=-1)
//			printf("%05d\n",node[p].next);
//		else printf("-1\n");
//		p = node[p].next;
//	}
	node[head].next = pL;
	int head1 = 100002;
	node[head1].next = pMid;
	pL = head1;
	pMid = node[pL].next;
	pR = node[pMid].next;
	bool flag = false;
	while(true){
		if(flag==false){
			flag=true;
			node[pMid].next = -1;
		}
		else node[pMid].next = pL;
		pL = pMid;
		pMid = pR;
		if(pMid==-1) break;
		pR = node[pMid].next;
	}
//	p = pL;
//	while(p!=-1){
//		printf("%05d %d ",p,node[p].data);
//		if(node[p].next!=-1)
//			printf("%05d\n",node[p].next);
//		else printf("-1\n");
//		p = node[p].next;
//	}
	node[head1].next = pL;
	p = node[head].next;
	int now,tmp,tmpp,pp=node[head1].next;
	int pPre;
	while(p!=-1&&pp!=-1){
		tmp = node[p].next;
		tmpp = node[pp].next;
		node[pp].next = tmp;
		node[p].next = pp;
		pPre = pp;
		pp = tmpp;
		p = tmp;
	}
	if(p==-1&&pp!=-1){
		node[pPre].next = pp;
	}
	p = node[head].next;
	while(p!=-1){
		printf("%05d %d ",p,node[p].data);
		if(node[p].next!=-1)
			printf("%05d\n",node[p].next);
		else printf("-1\n");
		p = node[p].next;
	}
	return 0;
} 

3. 

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000;
int HT[maxn],TSize,M,N;
void Insert(int x)
{
	int newPos,curPos;
	newPos = curPos = x%M;
	int i=1;
	while(HT[curPos]!=-1){
		curPos = (newPos+i)%TSize;
		i++;
	}
	HT[curPos] = x;
//	printf("%d %d\n",curPos,x);
}
int main()
{
	fill(HT,HT+maxn,-1);
	int x;
	scanf("%d%d%d",&TSize,&M,&N);
	for(int i=0; i<N; i++){
		scanf("%d",&x);
		Insert(x);	
	}
	int cnt = 0;
	for(int i=0; i<M; i++){
		int k=0,curPos=i;
		while(k<TSize&&HT[curPos]!=-1){
			k++;
			curPos = (i+k)%TSize;
		}
		if(HT[curPos]==-1) {
			cnt+=(k+1);
		//	printf("k=%d\n",k);	
		}
		else{
			cnt+=(k+1);
		//	printf("k+1=%d\n",k+1);
		}
	}
	//printf("cnt=%d\n",cnt);
	printf("%.1f\n",cnt*1.0/M);
	return 0;
}
//0  1  2  3  4  5  6 7  8  9  10 11
//98 22 30 87 11 40 6 20 -1 -1 -1 -1
//9+8+7+6+5+4+3
//0  1 2
//81 5 2



4、

#include<bits/stdc++.h> 
using namespace std;
const int maxn = 1015;
const int inf = 1000000000;
int Ns,Na,M,K;
int G[maxn][maxn];
int ambulance[15];
bool vis[maxn]={false};
int d[15][maxn];
vector<int> pre[15][maxn];
void Dijkstra(int s)
{
	fill(vis,vis+maxn,false);
	fill(d[s],d[s]+maxn,inf);
	d[s][s+Ns]=0;
	while(true){
		int u=-1, Min=inf;
		for(int i=1; i<=Ns+Na; i++){
			if(vis[i]==false&&d[s][i]<Min){
				Min = d[s][i];
				u = i;
			}
		}
		if(u==-1) break;
		vis[u]=true;
		for(int v=1; v<=Ns+Na; v++){
			if(G[u][v]!=inf&&vis[v]==false){
				if(d[s][u]+G[u][v]<d[s][v]){
					d[s][v]=d[s][u]+G[u][v];
					pre[s][v].clear();
					pre[s][v].push_back(u);
				}
				else if(d[s][u]+G[u][v]==d[s][v]){
					pre[s][v].push_back(u);
				}
			}
		}
	}
}
vector<int> tmpPath,path;
int minS = inf;
int test=0;
void dfs(int now,int ed,int center)
{
	if(now==ed){
		tmpPath.push_back(now);
		if(tmpPath.size()<=minS){
			path = tmpPath;
			minS = path.size();
		}
		tmpPath.pop_back();
		return;
	}
	tmpPath.push_back(now);
	for(int i=0; i<pre[center][now].size(); i++){
		dfs(pre[center][now][i],ed,center);
	} 
	tmpPath.pop_back();
}
int main()
{
	fill(G[0],G[0]+maxn*maxn,inf);
	scanf("%d%d",&Ns,&Na);
	for(int i=1; i<=Na; i++){
		scanf("%d",&ambulance[i]);getchar();
	}
	scanf("%d",&M);
	char s1[10],s2[10];
	int time,u,v,spot;
	for(int i=0; i<M; i++){
		scanf("%s %s %d",s1,s2,&time);getchar();
		if(s1[0]=='A'){
			sscanf(s1,"A-%d",&u);
			u+=Ns;
		}
		else u = stoi(s1);
		if(s2[0]=='A'){
			sscanf(s2,"A-%d",&v);
			v+=Ns;
		}
		else v = stoi(s2);
		G[u][v]=G[v][u]=time;
	}
	for(int s=1; s<=Na; s++){
		Dijkstra(s);
	}
	scanf("%d",&K);
	for(int i=0; i<K; i++){
		scanf("%d",&spot);
		vector<int> ans;
		int minTime=inf,maxCars=-1,minStreets=inf,index=-1;
		for(int center=Ns+1; center<=Ns+Na; center++){
			tmpPath.clear();
			path.clear();
			minS = inf;
			dfs(spot,center,center-Ns);
			if(ambulance[center-Ns]>0){
				if(d[center-Ns][spot]<minTime){
					minTime = d[center-Ns][spot];
					maxCars = ambulance[center-Ns];
					minStreets = path.size();
					ans = path;
					index = center-Ns;
				}
				else if(d[center-Ns][spot]==minTime&&ambulance[center-Ns]>maxCars){
					maxCars = ambulance[center-Ns];
					minStreets = path.size();
					ans = path;
					index = center-Ns;
				}
				else if(d[center-Ns][spot]==minTime&&ambulance[center-Ns]==maxCars&&
				path.size()<minStreets){
					minStreets = path.size();
					ans = path;
					index = center-Ns;
				}
			}
		}
		if(index==-1) printf("All Busy\n");
		else{
			ambulance[index]--;
			for(int z=ans.size()-1; z>=0; z--){
				if(ans[z]>Ns) printf("A-%d",ans[z]-Ns);
				else printf("%d",ans[z]);
				if(z!=0) printf(" ");
				else printf("\n");
			}
			printf("%d\n",minTime);
		}
	}
}









 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值