首页 > 编程语言 > 详细

杭电2005(第几天?)java字符串水过

时间:2015-04-16 19:58:27      阅读:246      评论:0      收藏:0      [点我收藏+]

点击打开杭电2005

1、split的应用:将字符串以某某字符为界划分为多个字符串

2、面向对象的编程


Problem Description
给定一个日期,输出这个日期是该年的第几天。
 

Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 

Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
 

Sample Input
1985/1/20 2006/3/12
 

Sample Output
20 71

import java.util.*;
class MyDate{
	int year ,month,day;
	int[] months={0,31,0,31,30,31,30,31,31,30,31,30};
	public MyDate(String str){
		this.set(str);
	}
	public void set(String str){
		String[] s=str.split("/");
		this.year=Integer.parseInt(s[0]);
		this.month=Integer.parseInt(s[1]);
		this.day=Integer.parseInt(s[2]);
	}
	public boolean isLeapYear(){
		return year%400==0||year%4==0&&year%100!=0;
	}
	public int daysOfYear(){
		if(isLeapYear()){
			this.months[2]=29;
		}else{
			this.months[2]=28;
		}
		int sum=0;
		for(int i=1;i<this.month;i++){
			sum+=months[i];
		}
		return sum+this.day;
	}
}
class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			String str=sc.next();
			MyDate d=new MyDate(str);
			System.out.println(d.daysOfYear());
		}
	}
}




杭电2005(第几天?)java字符串水过

原文:http://blog.csdn.net/u011479875/article/details/45078609

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!