描述
根据输入的日期,计算是这一年的第几天。保证年份为4位数且日期合法。进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(1)\O(1)
输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天
示例1
输入:
2012 12 31
输出:
366
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int y=in.nextInt();
int m=in.nextInt();
int d=in.nextInt();
Calendar c1=Calendar.getInstance();//实例化
c1.set(y, m-1, d);//注意月份从0开始
System.out.println(c1.get(Calendar.DAY_OF_YEAR));
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int[] month_day = {31,28,31,30,31,30,31,31,30,31,30,31};
int sum = 0;
for(int i = 0; i < month - 1; i++){
sum += month_day[i];
}
sum += day;
if(month > 2 && is_leap(year)){
sum += 1;
}
System.out.println(sum);
}
}
public static boolean is_leap(int n){
if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0){
return true;
}else{
return false;
}
}
}