您的位置:首页 > 理论基础

浙大计算机研究生复试上机考试-2010年

2012-02-08 18:07 351 查看
/************************************************************************/
/* A+B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 604    Accepted Submission(s): 380

Problem Description
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

Input
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

Output
请计算A+B的结果,并以正常形式输出,每组数据占一行。

Sample Input
-234,567,890 123,456,789
1,234 2,345,678

Sample Output
-111111101
2346912

Source
浙大计算机研究生复试上机考试-2010年
*/
/************************************************************************/

#include"iostream"
#include "string.h"
#include "math.h"
using namespace std;

int trans(char *input)
{
	//printf("%s",input);
	int res=0;
	int i,j;
	for (i=0;i<strlen(input);i++)
	{
		if(input[i]>='0'&&input[i]<='9')
		{
			res*=10;
			res+=(input[i]-'0');
		}
	}
	if(input[0]=='-')
		res=-res;
	return res;
}

int main()
{
	char *inp1,*inp2;
	inp1=(char*)malloc(15*sizeof(char));
	inp2=(char*)malloc(15*sizeof(char));
	while(scanf("%s",inp1)!=EOF)
	{
		scanf("%s",inp2);

		int a=trans(inp1);
		int b=trans(inp2);
		printf("%d\n",a+b);
	}
}


/************************************************************************/
/*                                   ZOJ问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 657    Accepted Submission(s): 201

Problem Description
对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。

是否AC的规则如下:
1. zoj能AC;
2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空;
3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空;

Input
输入包含多组测试用例,每行有一个只包含'z','o','j'三种字符的字符串,字符串长度小于等于1000;

Output
对于给定的字符串,如果能AC则请输出字符串“Accepted”,否则请输出“Wrong Answer”。

Sample Input
zoj
ozojo
ozoojoo
oozoojoooo
zooj
ozojo
oooozojo
zojoooo

Sample Output
Accepted
Accepted
Accepted
Accepted
Accepted
Accepted
Wrong Answer
Wrong Answer

Source
浙大计算机研究生复试上机考试-2010年

                                   */
/************************************************************************/
/*analyse:
Accept情况:(x----n个o,n>=1)
1. xzojx
2. (x)z(o*n)j(x*n)
*/

#include "iostream"
#include "string"
using namespace std;

int main()
{
	char* inp;
	int o1,o2,o3,z,j,i;
	int flagz,flagj,flag;
	inp=(char*)malloc(1020*sizeof(char));
	while(gets(inp))
	{
		flagz=flagj=flag=0;
		for(i=0;i<strlen(inp);i++)
		{
			if(inp[i]=='z')
				flagz++,z=i;
			else if(inp[i]=='j')
				flagj++,j=i;
			else if(inp[i]!='o')
				flag=2;
		}
		if(flagz!=1||flagj!=1||flag!=0)
		{
			puts("Wrong Answer");
			continue;
		}
		o1=z;
		o2=j-z-1;
		o3=strlen(inp)-j-1;
		if(o1*o2==o3&&o2>=1)
			puts("Accepted");
		else
			puts("Wrong Answer");		
	}
}


/************************************************************************/
/* 奥运排序问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 690    Accepted Submission(s): 167

Problem Description
按要求,给国家进行排名。

Input
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。

Output
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例 
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例 
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。

Sample Input
4 4
4 8 1
6 6 2
4 8 2
2 12 4
0 1 2 3
4 2
8 10 1
8 11 2
8 12 3
8 13 4
0 3

Sample Output
1:3
1:1
2:1
1:2

1:1
1:1

Source
浙大计算机研究生复试上机考试-2010年
                                                                     */
/************************************************************************/

#include "iostream"
using namespace std;

struct Nation
{
	int jin;
	int jiang;
	int people;
	int index;
	int num[6];//num[0,1,2,3]record sort res,num[4]record min sort num, num[5] record min sort mode
};

int mode;
int comp(const void *A,const void* B)
{
	struct Nation* a=(Nation*)A;
	struct Nation* b=(Nation*)B;

	switch(mode)
	{
	case 1:    
		return b->jin-a->jin;
	case 2:
		return b->jiang-a->jiang;
	case 3:
		return b->jin*a->people-a->jin*b->people;
	case 4:
		return b->jiang*a->people-a->jiang*b->people;
	}
}

bool Same(Nation a,Nation b,int mode)
{
	switch(mode)
	{
	case 1:
		return a.jin==b.jin;
	case 2:
		return a.jiang==b.jiang;
	case 3:
		return a.jin*b.people==b.jin*a.people;
	case 4:
		return a.jiang*b.people==b.jiang*a.people;
	}
}

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j,k=0,t;
		Nation* country=(Nation*)malloc(n*sizeof(Nation));
		Nation* c=(Nation*)malloc((m+10)*sizeof(Nation));
		Nation* res=(Nation*)malloc((m+10)*sizeof(Nation));
		for (i=0;i<n;i++)
		{
			scanf("%d%d%d",&country[i].jin,&country[i].jiang,&country[i].people);
		}
		for (i=0;i<m;i++)
		{
			scanf("%d",&t);
			c[k]=country[t];
			for(j=0;j<5;j++)
				c[k].num[j]=m+1;
			c[k++].index=i;
		}
		for (mode=4;mode>=1;mode--)
		{
			qsort(c,k,sizeof(c[0]),comp);
			c[0].num[mode-1]=1;
			for(i=1;i<k;i++)
				c[i].num[mode-1]=Same(c[i],c[i-1],mode)?c[i-1].num[mode-1]:(i+1);
		}
		for (i=0;i<k;i++)
		{
			for (mode=4;mode>0;mode--)
			{
				if (c[i].num[mode-1]<=c[i].num[4])
				{
					c[i].num[4]=c[i].num[mode-1];
					c[i].num[5]=mode;
				}
			}
			res[c[i].index]=c[i];
		}
		for(i=0;i<m;i++)
			printf("%d:%d\n",res[i].num[4],res[i].num[5]);
		cout<<endl;
	}
}


/************************************************************************/
/* 最短路径问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2114    Accepted Submission(s): 659

Problem Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。

Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)

Output
输出 一行有两个数, 最短距离及其花费。

Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0

Sample Output
9 11

Source
浙大计算机研究生复试上机考试-2010年
                                                                     */
/************************************************************************/

//这个是SPFA法,Dijkstra法见下个代码
#include"iostream"
#include"queue"
#include"vector"
using namespace std;

#define N 1005
#define INF 100000000
int n,m;

struct MAP
{
	int node;
	int dis;
	int cost;
	MAP(int a,int d,int p)
	{
		node=a,dis=d,cost=p;
	}
};
vector<MAP>map
;//map[i][j]表与节点i相连的第j条边的信息
int minres
[2];//min_dis/cost from s to i

void spfa(int s,int e)
{
	queue<int>Q;
	bool used
={false};
	Q.push(s);
	used[s]=true;
	int i;
	for(i=1;i<=n;i++)
		minres[i][0]=minres[i][1]=INF;
	minres[s][0]=minres[s][1]=0;
	while(!Q.empty())
	{
		int now=Q.front();
		Q.pop();
		used[now]=false;//
		for(i=0;i<map[now].size();i++)
		{
			int tmpend=map[now][i].node;
			int dis=map[now][i].dis;
			int cost=map[now][i].cost;

			if(minres[tmpend][0]>minres[now][0]+dis||
				(minres[tmpend][0]==minres[now][0]+dis&&
				minres[tmpend][1]>minres[now][1]+cost))
			{
				minres[tmpend][0]=minres[now][0]+dis;
				minres[tmpend][1]=minres[now][1]+cost;
				if(!used[tmpend])
					Q.push(tmpend);
				used[tmpend]=true;
			}
		}
	}
}

int main()
{
	while(scanf("%d %d",&n,&m)!=EOF&&!(m==0&&n==0))
	{
		int a,b,d,p,i,j;
		for (i=1;i<=n;i++)
			map[i].clear();
		while (m--)
		{
			scanf("%d%d%d%d",&a,&b,&d,&p);
			map[a].push_back(MAP(b,d,p));//注意双向边
			map[b].push_back(MAP(a,d,p));
		}
		scanf("%d%d",&a,&b);
		spfa(a,b);
		printf("%d %d\n",minres[b][0],minres[b][1]);
	}
}


法二 Dijkstra

#include"iostream"
#include"vector"
using namespace std;
#define N 1005
#define INF 1000000000
int n,m;

struct Map
{
	int point;//another point of edges connect this point
	int dis;
	int cost;
	Map(int x,int y,int z):point(x),dis(y),cost(z){}
};
vector<Map>map
;
int minres
[2];

void bfs(int s,int e)
{
	int i,j;
	for(i=1;i<=n;i++)
		minres[i][0]=minres[i][1]=INF;
	minres[s][0]=minres[s][1]=0;

	bool used
={false};//false--in the end point set
	
	for(j=0;j<n;j++)//n loops
	{
		int mindis,mincost,tmp;
		mindis=mincost=INF;
		for(i=1;i<=n;i++)//find a point with min dis and min cost
		{
			if(!used[i]&&(minres[i][0]<mindis||minres[i][0]==mindis&&minres[i][1]<mincost))
			{
				mindis=minres[i][0];
				mincost=minres[i][1];
				tmp=i;
			}
		}
		int p1,d1,c1;
		//update paths connect to point tmp

		for (i=0;i<map[tmp].size();i++)
		{
			p1=map[tmp][i].point;
			d1=map[tmp][i].dis;
			c1=map[tmp][i].cost;
			if((minres[p1][0]>minres[tmp][0]+d1||
				minres[p1][0]==minres[tmp][0]+d1&&minres[p1][1]>minres[tmp][1]+c1)&&
				!used[p1])
			{
				minres[p1][0]=minres[tmp][0]+d1;
				minres[p1][1]=minres[tmp][1]+c1;
			}
		}	
		used[tmp]=true;
	}
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF&&!(m==0&&n==0))
	{ 
		int a,b,d,p;
		for (int i=1;i<=n;i++)
			map[i].clear();///////////千万注意!
		while(m--)
		{
			scanf("%d%d%d%d",&a,&b,&d,&p);
			map[a].push_back(Map(b,d,p));
			map[b].push_back(Map(a,d,p));
		}
		scanf("%d%d",&a,&b);
		bfs(a,b);
		printf("%d %d\n",minres[b][0],minres[b][1]);
	}
}


/************************************************************************/
/*
@Function: Min Path
@Method: Dijikstra+Priority_Queue                                                        
@Author: zrq sophia
*/
/************************************************************************/
#include"iostream"
#include"vector"
#include "queue"
using namespace std;
#define N 1005
#define INF 1000000000
int n,m;

struct Map
{
	int point;//another point of edges connect this point
	int dis;
	int cost;
	Map(int x,int y,int z):point(x),dis(y),cost(z){}
};
vector<Map>map
;
int minres
[2];

struct WAY
{
	int dis;
	int cost;
	int point;
	bool operator < (const WAY a)const
	{
		if(a.dis==dis)
			return a.cost<cost;//sort from small to big
		return a.dis<dis;
	}
};

void bfs(int s,int e)
{
	int i,j;
	for(i=1;i<=n;i++)
		minres[i][0]=minres[i][1]=INF;
	minres[s][0]=minres[s][1]=0;
	priority_queue<WAY>Q;
	WAY now,temp;
	now.point=s;
	now.dis=now.cost=0;
	Q.push(now);

	bool used
={false};//false--in the end point set
	int tmp;
	while(!Q.empty())
	{
		now=Q.top();
		Q.pop();//use priority queue to ensure pop the point with min minres[][]
		tmp=now.point;

		int p1,d1,c1;
		//update paths connect to point tmp

		for (i=0;i<map[tmp].size();i++)
		{
			p1=map[tmp][i].point;
			d1=map[tmp][i].dis;
			c1=map[tmp][i].cost;
			if((minres[p1][0]>minres[tmp][0]+d1||
				minres[p1][0]==minres[tmp][0]+d1&&minres[p1][1]>minres[tmp][1]+c1)&&
				!used[p1])
			{
				minres[p1][0]=minres[tmp][0]+d1;
				minres[p1][1]=minres[tmp][1]+c1;
				now.point=p1;
				now.dis=minres[p1][0];
				now.cost=minres[p1][1];
				Q.push(now);
			}
		}	
	}
	used[tmp]=true;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF&&!(m==0&&n==0))
	{ 
		int a,b,d,p;
		for (int i=1;i<=n;i++)
			map[i].clear();///////////千万注意!
		while(m--)
		{
			scanf("%d%d%d%d",&a,&b,&d,&p);
			map[a].push_back(Map(b,d,p));
			map[b].push_back(Map(a,d,p));
		}
		scanf("%d%d",&a,&b);
		bfs(a,b);
		printf("%d %d\n",minres[b][0],minres[b][1]);
	}
}


/************************************************************************/
/*3791  二叉搜索树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 556    Accepted Submission(s): 242

Problem Description
判断两序列是否为同一二叉搜索树序列

Input
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。

Output
如果序列相同则输出YES,否则输出NO

Sample Input
2
567432
543267
576342
0

Sample Output
YES
NO

Source
浙大计算机研究生复试上机考试-2010年
                                                                     */
/************************************************************************/

#include "iostream"
#include "string"
using namespace std;

string pro;
string origin;

struct Tree
{
    char name;
    Tree* l;
    Tree* r;
    Tree()
    {
        l=r=NULL;
    }
};

bool equal(Tree *a,Tree *b)
{
    if(a==NULL&&b==NULL)
        return true;
    else if(a==NULL&&b!=NULL||a!=NULL&&b==NULL)
        return false;
    if (a->name==b->name)
        return equal(a->l,b->l)&&equal(a->r,b->r);
    return false;
}

void insert(Tree** root,char c)
{
    if (*root==NULL)
    {
        *root=new Tree;
        (*root)->name=c;
    }
    else
    {
        if (c<(*root)->name)
            insert(&(*root)->l,c);
        else
            insert(&(*root)->r,c);
    }
}

int main()
{
    int n,in;
    while (scanf("%d",&n)!=EOF&&n!=0)
    {
        cin>>origin;
        Tree *t1=NULL;
        for(in=0;in<origin.length();in++)
            insert(&t1,origin[in]);

        for (int i=0;i<n;i++)
        {
            cin>>pro;        

            Tree *t2=NULL;
            for(in=0;in<pro.length();in++)
                insert(&t2,pro[in]);

            if(equal(t1,t2))
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: