2014年12月CCF软考试题

A.门禁系统

题目链接:http://118.190.20.162/view.page?gpid=T21

水题,使用一个map秒解。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	map<int,int> mp;
	while(n--){
		int tp;
		scanf("%d",&tp);
		mp[tp]++;
		printf("%d",mp[tp]);
		if(n>0) printf(" ");
	}
	return 0;
}

B.Z字形扫描

题目链接:http://118.190.20.162/view.page?gpid=T20

这个题一开始超时,能用while循环解决的就不要用dfs递归,发现递归很容易超时。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
const int MAXV=510;
const int UP=1;
const int DOWN=2;
int n;
int G[MAXV][MAXV];
int main(){
	scanf("%d",&n);
	memset(G,0,sizeof(G)); 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			scanf("%d",&G[i][j]);
		}
	}
	int i=1,j=1;
	int fx=UP;
	while(i!=n || j!=n){
		printf("%d ",G[i][j]);
		if(i==1 && fx==UP){
			if(j==n) i++;
			else j++;
			fx=DOWN;
		}else if(j==1 && fx==DOWN){
			if(i==n) j++;
			else i++;
			fx=UP;
		}else if(i==n && fx==DOWN){
			j++;
			fx=UP;
		}else if(j==n && fx==UP){
			i++;
			fx=DOWN;
		}else if(fx==UP){
			i--;
			j++;
		}else if(fx==DOWN){
			i++;
			j--;
		}
	}
	printf("%d",G[i][j]);
	return 0;
}

C.集合竞价

题目链接:http://118.190.20.162/view.page?gpid=T19

这个题挺坑的,没有说清楚cancel是个什么意思,两种情况:1.如果cancel的记录是cancel,那么不做处理;2.如果cancel的记录之前被cancel撤销,则恢复。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int BUY=1;
const int SELL=2;
const int CANCEL=3;
const int MAXV=5010;
struct Node{
	int type;
	double price;
	ll num;
	bool use;
	Node(int t,double p,ll n){
		type=t;
		price=p;
		num=n;
		use=true;
	}
};
vector<Node> vall;
int main(){
	char buf[10];
	while(scanf("%s",buf)!=EOF){
		string ord(buf);
		if(ord=="buy" || ord=="sell"){
			double p;
			ll n;
			scanf("%lf",&p);
			scanf("%lld",&n);
			if(ord=="buy") vall.push_back(Node(BUY,p,n));
			if(ord=="sell") vall.push_back(Node(SELL,p,n));
		}else if(ord=="cancel"){
			int num;
			scanf("%d",&num);
			vall.push_back(Node(CANCEL,0.0,0));
			num--;
			if(vall[num].type!=CANCEL){
				vall[num].use=!vall[num].use;
			}
		}
	}
	vector<Node> bvn,svn;
	for(int i=0;i<vall.size();i++){
		if(vall[i].use && vall[i].type==BUY) bvn.push_back(vall[i]);
		if(vall[i].use && vall[i].type==SELL) svn.push_back(vall[i]);
	}
	ll ans=0;double pri=0.0;
	for(int i=0;i<bvn.size();i++){
		ll tpb=0,tps=0;
		for(int j=0;j<bvn.size();j++){
			if(bvn[j].price>=bvn[i].price){
				tpb+=bvn[j].num;
			}
		}
		for(int k=0;k<svn.size();k++){
			if(bvn[i].price>=svn[k].price){
				tps+=svn[k].num;
			}
		}
		if(min(tps,tpb)>ans){
			ans=min(tps,tpb);
			pri=bvn[i].price;
		}
	}
	printf("%.2lf %lld",pri,ans);
	return 0;
}

D.最优灌溉

题目链接:http://118.190.20.162/view.page?gpid=T18

一道典型的最小生成树问题,直接套用Prim算法就可以了。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
const int MAXV=1010;
const int INF=0x7fff;
int n,m,G[MAXV][MAXV];
int d[MAXV];
bool vis[MAXV]={false};
int prim(){
	fill(d,d+MAXV,INF);
	d[1]=0;
	int ans=0;
	for(int i=1;i<=n;i++){
		int u=-1,MIN=INF;
		for(int j=1;j<=n;j++){
			if(vis[j]==false && d[j]<MIN){
				u=j;
				MIN=d[j];
			}
		}
		if(u==-1) return -1;
		vis[u]=true;
		ans+=d[u];
		for(int v=1;v<=n;v++){
			if(vis[v]==false && G[u][v]!=INF && G[u][v]<d[v]){
				d[v]=G[u][v];
			}
		}
	}
	return ans;
}
int main(){
	int u,v,w;
	scanf("%d%d",&n,&m);	//顶点个数,边数
	fill(G[0],G[0]+MAXV*MAXV,INF);
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&u,&v,&w);
		G[u][v]=G[v][u]=w;
	} 
	int ans=prim();
	printf("%d\n",ans);
	return 0;
}

E.货物调度

题目链接:http://118.190.20.162/view.page?gpid=T17

先丢下。。。。要不怕刷不完

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值