提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
题目描述
根据输入的日期,计算是这一年的第几天。。
测试用例有多组,注意循环输入
输入描述:
输入多行,每行空格分割,分别是年,月,日
输出描述:
成功:返回outDay输出计算后的第几天;
失败:返回-1
示例1
输入
2012 12 31
输出
366
题目来源:牛客网
解题过程
解题思路
分清闰年,各个月的天数即可方便地计算出天数。
import java.util.*;
public class Main{
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String str = scanner.nextLine();
String [] arr = str.split(" ");
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
int day = Integer.parseInt(arr[2]);
int count_day = 0;
for(int i = 1; i < month; i++){
if(i == 4 || i == 6 || i == 9 || i == 11){
count_day += 30;
}else if(i == 2){
if(year % 4 ==0){
count_day += 29;
}else{
count_day += 28;
}
}else{
count_day += 31;
}
}
count_day += day;
System.out.println(count_day);
}
}
}
总结
暂时没有总结,待续。。。