CCF通过测试的代码 2015-03(2)

节日

问题描述
  有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日。
  现在,给你a,b,c和y1, y2(1850 ≤ y1, y2 ≤ 2050),希望你输出从公元y1年到公元y2年间的每年的a月的第b个星期c的日期。
  提示:关于闰年的规则:年份是400的整数倍时是闰年,否则年份是4的倍数并且不是100的倍数时是闰年,其他年份都不是闰年。例如1900年就不是闰年,而2000年是闰年。
  为了方便你推算,已知1850年1月1日是星期二。
  
输入格式
  输入包含恰好一行,有五个整数a, b, c, y1, y2。其中c=1, 2, ……, 6, 7分别表示星期一、二、……、六、日。
  
输出格式
  对于y1和y2之间的每一个年份,包括y1和y2,按照年份从小到大的顺序输出一行。
  如果该年的a月第b个星期c确实存在,则以"yyyy/mm/dd"的格式输出,即输出四位数的年份,两位数的月份,两位数的日期,中间用斜杠“/”分隔,位数不足时前补零。
  如果该年的a月第b个星期c并不存在,则输出"none"(不包含双引号)。
  
样例输入
5 2 7 2014 2015

样例输出
2014/05/11
2015/05/10

评测用例规模与约定
  所有评测用例都满足:1 ≤ a ≤ 12,1 ≤ b ≤ 5,1 ≤ c ≤ 7,1850 ≤ y1, y2 ≤ 2050。

package CCF;

import java.util.*;

public class fifteen_three_three {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner myscan = new Scanner(System.in);
		String[] s = myscan.nextLine().split(" ");
		myscan.close();
		
		int[] num = new int[s.length];
		for(int i=0;i<s.length;i++) {
			num[i] = Integer.parseInt(s[i]);
		}
		
		int start_year = num[3];
		int end_year = num[4];
		int need_time = 2;
		for(int i=0;i<start_year-1850;i++) {
			int now_year = 1850+i;
			int days = 0;
			if(now_year%400 == 0||((now_year%4==0)&&(now_year%100 != 0))) {
				days = 366;//System.out.println(now_year);
			}
			else {
				days = 365;
			}
			need_time = need_time+days;
		}//System.out.println(need_time);
		//need_time = need_time%7;//System.out.println(need_time);
		//System.out.println((month_days+need_time)%7);
		for(int i=start_year;i<end_year+1;i++) {
			int month_days = get_month(i,num[0]);
			int use_day = (need_time+month_days)%7;//System.out.println(use_day);
			int really_day = 1;
			if(use_day == 0) {
				use_day = 7;
			}
			if(use_day<num[2]) {
				really_day = really_day+num[2]-use_day;
			}
			else if(use_day>num[2]) {
				really_day = really_day+num[2]-use_day+7;
			}//System.out.println(really_day);
			really_day = really_day+(num[1]-1)*7;//System.out.println(use_day);
			if(!judge_month(i,num[0],really_day)) {
				System.out.println("none");
			}
			else {
				if(num[0]<10) {
					if(really_day<10) {
						System.out.println(i+"/0"+num[0]+"/0"+really_day);
					}
					else {
						System.out.println(i+"/0"+num[0]+"/"+really_day);
					}
				}
				else {
					if(really_day<10) {
						System.out.println(i+"/"+num[0]+"/0"+really_day);
					}
					else {
						System.out.println(i+"/"+num[0]+"/"+really_day);
					}
				}
			}
			
			if(i%400 == 0||((i%4==0)&&(i%100 != 0))) {
				need_time = need_time+366;//System.out.println(now_year);
			}
			else {
				need_time = need_time+365;
			}
		}
	}
	
	public static int get_month(int year,int num) {
		int[] months = new int[12];
		for(int i=0;i<12;i++) {
			if(i+1==1||i+1==3||i+1==5||i+1==7||i+1==8||i+1==10||i+1==12) {
				months[i] = 31;
			}
			else if(i+1==2) {
				if(year%400==0 ||((year%4==0)&&(year%100!=0))) {
					months[i] = 29;
				}
				else {
					months[i] = 28;
				}
			}
			else {
				months[i] = 30;
			}
		}
		
		int days = 0;
		for(int i=0;i<num-1;i++) {
			days = days+months[i];
		}
		return days;
	}
	
	public static boolean judge_month(int year,int month,int days) {
		int[] months = new int[12];
		for(int i=0;i<12;i++) {
			if(i+1==1||i+1==3||i+1==5||i+1==7||i+1==8||i+1==10||i+1==12) {
				months[i] = 31;
			}
			else if(i+1==2) {
				if(year%400==0 ||((year%4==0)&&(year%100!=0))) {
					months[i] = 29;
				}
				else {
					months[i] = 28;
				}
			}
			else {
				months[i] = 30;
			}
		}
		
		if(days>months[month-1]) {
			return false;
		}
		else {
			return true;
		}
	}
}

网络延时

问题描述
  给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机、交换机与电脑之间使用网络连接。交换机按层级设置,编号为1的交换机为根交换机,层级为1。其他的交换机都连接到一台比自己上一层的交换机上,其层级为对应交换机的层级加1。所有的终端电脑都直接连接到交换机上。
  当信息在电脑、交换机之间传递时,每一步只能通过自己传递到自己所连接的另一台电脑或交换机。请问,电脑与电脑之间传递消息、或者电脑与交换机之间传递消息、或者交换机与交换机之间传递消息最多需要多少步。
输入格式
  输入的第一行包含两个整数n, m,分别表示交换机的台数和终端电脑的台数。
  第二行包含n - 1个整数,分别表示第2、3、……、n台交换机所连接的比自己上一层的交换机的编号。第i台交换机所连接的上一层的交换机编号一定比自己的编号小。
  第三行包含m个整数,分别表示第1、2、……、m台终端电脑所连接的交换机的编号。
  
输出格式
  输出一个整数,表示消息传递最多需要的步数。
  
样例输入
4 2
1 1 3
2 1

样例输出
4

样例说明
  样例的网络连接模式如下,其中圆圈表示交换机,方框表示电脑:
在这里插入图片描述
  其中电脑1与交换机4之间的消息传递花费的时间最长,为4个单位时间。
  
样例输入
4 4
1 2 2
3 4 4 4

样例输出
4

样例说明
  样例的网络连接模式如下:
在这里插入图片描述
  其中电脑1与电脑4之间的消息传递花费的时间最长,为4个单位时间。
  
评测用例规模与约定
  前30%的评测用例满足:n ≤ 5, m ≤ 5。
  前50%的评测用例满足:n ≤ 20, m ≤ 20。
  前70%的评测用例满足:n ≤ 100, m ≤ 100。
  所有评测用例都满足:1 ≤ n ≤ 10000,1 ≤ m ≤ 10000。

package CCF;

import java.util.*;

public class fifteen_three_four {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner myscan = new Scanner(System.in);
		String[] s = myscan.nextLine().split(" ");
		int computer_num = Integer.parseInt(s[1]);
		int switch_num = Integer.parseInt(s[0]);
		fift_three_switch[] all_switch = new fift_three_switch[switch_num];
		fift_three_computer[] all_computer = new fift_three_computer[computer_num];
		for(int i=0;i<switch_num;i++) {
			all_switch[i] = new fift_three_switch(i+1);
		}
		for(int i=0;i<computer_num;i++) {
			all_computer[i] = new fift_three_computer(i+1);
		}
		
		s = myscan.nextLine().split(" ");
		int switch_front,k=0;
		for(int i=0;i<all_switch.length;i++) {
			if(i+1==1) {
				all_switch[i].front_switch = 0;
			}
			else {
				switch_front = Integer.parseInt(s[k]);
				k++;
				all_switch[i].front_switch = switch_front;
				for(int j=0;j<all_switch.length;j++) {
					if(all_switch[j].switch_noble == switch_front) {
						all_switch[j].behind_switch.add(all_switch[i].switch_noble);
						break;
					}
				}
			}
		}
		int computer_front;
		s = myscan.nextLine().split(" ");
		for(int i=0;i<all_computer.length;i++) {
			computer_front = Integer.parseInt(s[i]);
			all_computer[i].front_switch = computer_front;
			for(int j=0;j<all_switch.length;j++) {
				if(all_switch[j].switch_noble == computer_front) {
					all_switch[j].behind_computer.add(i+1);
					break;
				}
			}
		}
		myscan.close();	
		fift_three_tree tree = new fift_three_tree(all_switch,all_computer);
		tree.start_find();
	}
}

class fift_three_tree{
	int max_way;
	fift_three_switch[] all_switch;
	fift_three_computer[] all_computer;
	int max_level;
	public fift_three_tree(fift_three_switch[] all_switch,fift_three_computer[] all_computer) {
		this.all_switch = all_switch;
		this.all_computer = all_computer;
		max_way = 0;
	}
	
	public void rebuild_all() {
		for(int i=0;i<all_switch.length;i++) {
			all_switch[i].visited = false;
		}
		for(int i=0;i<all_computer.length;i++) {
			all_computer[i].visited = false;
		}
	}
	
	public int find_level(fift_three_switch now_switch,int level) {
		now_switch.level = level;
		int next_level = level+1;
		int max_level = level,now_level = level;
		if(now_switch.behind_switch != null) {
			for(int i=0;i<now_switch.behind_switch.size();i++) {
				now_level = find_level(all_switch[now_switch.behind_switch.get(i)-1],next_level);
				if(now_level>max_level) {
					max_level = now_level;
				}
			}
		}
		if(now_switch.behind_computer!=null) {
			for(int i=0;i<now_switch.behind_computer.size();i++) {
				all_computer[now_switch.behind_computer.get(i)-1].level = next_level;
				now_level = next_level;
				if(now_level>max_level) {
					max_level = now_level;
				}
			}
		}
		return max_level;
	}
	
	public void start_find() {
		max_level = find_level(all_switch[0],1);
		/*for(int i=0;i<all_switch.length;i++) {
			all_switch[i].printit();
		}
		for(int i=0;i<all_computer.length;i++) {
			all_computer[i].printit();
		}
		System.out.println("Max Level: "+max_level);
		*/
		int now_way = 0;
		for(int i=0;i<all_switch.length;i++) {
			if(all_switch[i].level == max_level) {
				now_way = visit_leaf_switch(all_switch[i]);
				rebuild_all();
				if(now_way>max_way) {
					max_way = now_way;
				}
			}
		}
		for(int i=0;i<all_computer.length;i++) {
			if(all_computer[i].level == max_level) {
				now_way = visit_leaf_computer(all_computer[i]);
				rebuild_all();
				if(now_way>max_way) {
					max_way = now_way;
				}
			}
		}
		System.out.println(max_way);
	}
	
	public int visit_leaf_switch(fift_three_switch now_switch) {
		int num = 0;
		now_switch.visited = true;
		if(now_switch.front_switch !=0) {
			num = visit_switch(all_switch[now_switch.front_switch-1],num);
		}
		return num;
	}
	
	public int visit_leaf_computer(fift_three_computer now_computer) {
		int num = 0;
		now_computer.visited = true;
		num = visit_switch(all_switch[now_computer.front_switch-1],num);
		return num;
	}
	
	public int visit_switch(fift_three_switch now_switch,int num) {
		num = num+1;
		int write,end = 0;
		now_switch.visited = true;
		if(now_switch.front_switch!=0) {
			if(!all_switch[now_switch.front_switch-1].visited) {
				write = visit_switch(all_switch[now_switch.front_switch-1],num);
				if(write>end) {
					end = write;
				}
			}
		}
		if(now_switch.behind_switch!=null) {
			for(int i=0;i<now_switch.behind_switch.size();i++) {
				if(!all_switch[now_switch.behind_switch.get(i)-1].visited) {
					write = visit_switch(all_switch[now_switch.behind_switch.get(i)-1],num);
					if(write>end) {
						end = write;
					}
				}
			}
		}
		if(now_switch.behind_computer!=null) {
			for(int i=0;i<now_switch.behind_computer.size();i++) {
				if(!all_computer[now_switch.behind_computer.get(i)-1].visited) {
					num = num+1;
					break;
				}
			}
		}
		if(end>num) {
			num = end;
		}
		return num;
	}
}

class fift_three_computer{
	int computer_noble;
	int front_switch;
	boolean visited = false;
	int level;
	
	public fift_three_computer(int num) {
		computer_noble = num;
	}
	
	public void printit() {
		System.out.println("Computer: "+computer_noble);
		System.out.println("Front Switch: "+front_switch);
		System.out.println("Level: "+level);
		System.out.println();
	}
}

class fift_three_switch{
	int switch_noble;
	int front_switch;
	List<Integer> behind_switch;
	List<Integer> behind_computer;
	boolean visited = false;
	int level;
	
	public fift_three_switch(int num) {
		switch_noble = num;
		behind_switch = new ArrayList<>();
		behind_computer = new ArrayList<>();
	}
	
	public void printit() {
		System.out.println("Switch: "+switch_noble);
		System.out.println("Front_switch: "+front_switch);
		System.out.println("Level: "+level);
		System.out.print("Behind Switch: ");
		for(int i=0;i<behind_switch.size();i++) {
			System.out.print(behind_switch.get(i)+" ");
		}
		System.out.println();
		System.out.print("Behind Computer: ");
		for(int i=0;i<behind_computer.size();i++) {
			System.out.print(behind_computer.get(i)+" ");
		}
		System.out.println();
		System.out.println();
	}
}
  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值