2013北邮复试上机题目

  1. 日期问题
    题目描述
    请你计算出第X年Y月Z日是第X年的第几天。其中,1月1日是第一天,1月2日是第二天,以此类推。

计算时请注意闰年的影响。对于非整百年,年数能整除4是闰年,否则不是闰年;对于整百年,年数能整除400是闰年,否则不是闰年。如1900年和1901年不是闰年,而2000年和2004年是闰年。

输入格式
第一行有一个整数T (T≤100),表示一共有T组数据需要你处理。
接下来一共有T行,每行是一个如下格式的字符串:X:Y:Z,表示你需要计算第X年Y月Z日是第X年的第几天。其中X是一个大于0,小于2100的整数。保证字符串的格式都是合法的,字符串所表示的日期也都是存在的。

输出格式
对于每组数据,你需要输出一个整数,表示所求得的结果。

输入:

2
2013:4:12
112:4:12

分析:日期问题的模板问题 看王道书上面的模板
代码:


# include<cstdio>
# include<iostream>
# include<vector>
using namespace std;
int daytab[2][13]= {
	{0,31,28,31,30,  31,30,31,31, 30,31,30,31},
	{0,31,29,31,30,  31,30,31,31, 30,31,30,31}
};
bool IsYeapYear(int year) {
	return (year%4==0&&year%100!=0)||year%400==0;
}

int main() {
	int t;
	cin>>t;
	while(t--) {
		int year,month,day;
		scanf("%d:%d:%d",&year,&month,&day);
		int res=0;
		int row=IsYeapYear(year);
		for(int i=0; i<month; i++) {
			res+=daytab[row][i];
		}
		res+=day;
		cout<<res<<endl;
	}
	return 0;
}
  1. 统计结点的个数
    分析:刚开始把这个问题复杂化了,除了根结点之外的其他结点的入度未0(原因一个结点最多有一个父亲(前驱),但是却可以有很多儿子(后继),用一个parent数组来记录结点的父亲,根结点的父亲为-1;如果是根结点就不需要检查父亲结点的度是否小于于自己的度,其他的每个结点都需要;同时也需要检查儿子结点的度是否小于自己的度;结果这样做应该很烦。
    度这个概念是在图当中提出来的,很多树的题目完全可以当作图来解决,如果用邻接表来存储就会简单很多,这里把树当中无向图,入度和出度直接可以在输入的时候统计出来,只需要判断下标为index的所有与他有关系的顶点的度的大小关系。
    代码1:
# include<cstdio>
# include<iostream>
# include<vector>
# include<cstring>
using namespace std;
const int maxv=1000;
vector<int> child[maxv];
int ans[maxv];
int parent[maxv];
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		memset(child,0,sizeof(child));
		memset(ans,0,sizeof(ans));
		fill(parent,parent+n,-1);
		int m=n-1;
		while(m--) {
			int from,to;
			cin>>from>>to;
			parent[to]=from;
			child[from].push_back(to);
			ans[from]++;
			ans[to]++;
		}
		//对于根结点:无父亲结点,只需要比较所有孩子结点就可以了
		int count=0;
		bool flag;
		for(int i=0; i<n; i++) {
			flag=true;
			//父亲
			int father=parent[i];
			if(father!=-1) {
				if(ans[i]<ans[father]) {
					flag=false;
				}
			}
			//所有儿子
			for(int j=0; j<child[i].size(); j++) {
				int son=child[i][j];
				if(ans[i]<ans[son]) {
					flag=false;
					break;
				}
			}
			if(flag==true)count++;
		}
		cout<<count<<endl;
	}
	return 0;
}

代码2:

# include<cstdio>
# include<iostream>
# include<vector>
# include<cstring>
using namespace std;
const int maxv=1000;
vector<int> graph[maxv];
int ans[maxv];
int main(){
	int t;
	cin>>t;
	while(t--){
		memset(graph,0,sizeof(graph));
		memset(ans,0,sizeof(ans));
		int n;
		cin>>n;
		int m=n-1;
		while(m--){
			int from,to;
			cin>>from>>to;
			graph[from].push_back(to);
			graph[to].push_back(from);
			ans[from]++;
			ans[to]++;	
		}
		int count=0;
		for(int i=0;i<n;i++){
			bool flag=true;
			for(int j=0;j<graph[i].size();j++){
				if(ans[i]<ans[graph[i][j]]){
					flag=false;
					break;
				}		
			}
			if(flag==true)count++;		
		}
		cout<<count<<endl;
	}
	return 0;	
} 
  1. 这一题主要是利用二叉排序树的性质来完成的 代码很简单不贴
  2. 我感觉我的代码和这个大佬
    用map以空间换时间的方式差不多,但是还是运行超时了。等有时间再来看。但是好像用dp更好,还没来得及看。(2020/04/10)
    题目链接:
    链接1: http://10.105.242.83/problem/127 链接2: http://10.105.242.80/problem/p/94
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值