杭电oj~~2005

来源:互联网 发布:知是故人来 编辑:程序博客网 时间:2024/05/17 04:06

题目中涉及到很多知识点,包括强制类型转换,字符串截取,闰年的判断方法

题目描述:

第几天?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 121265    Accepted Submission(s): 43991


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

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

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

Sample Input
1985/1/202006/3/12
 

Sample Output
2071

AC代码:

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);while(in.hasNext()){//定义一年12个月的每个月天数int s=0,b[] = {31,28,31,30,31,30,31,31,30,31,30,31};String a;a = in.nextLine();//强制字符串截断,以“/”位标识int x = a.indexOf("/");int y = a.lastIndexOf("/");//截取输入字符串中年月日String year = a.substring(0,x);String month = a.substring(x+1,y);String day = a.substring(y+1);//强制类型转换,字符串转换为整型int year1 = Integer.parseInt(year);int month1 = Integer.parseInt(month);int day1 = Integer.parseInt(day);if(year1%400==0||(year1%4==0&&year1%100!=0))//判断年份是否为闰年{//闰年二月29天b[1] = b[1]+1;for(int i=0;i<month1-1;i++){s =s+ b[i];}System.out.println(s+day1);}else{for(int i=0;i<month1-1;i++){s =s+ b[i];}System.out.println(s+day1);}}}}


0 0
原创粉丝点击