import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int year = scanner.nextInt();
int month = scanner.nextInt();
int day = scanner.nextInt();
int[] monthDay = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (year % 4 == 0 && year % 100 != 0 ||year % 400 == 0) {
monthDay[2] = 29;
}
int sum = 0;
for (int i = 0; i < month; i++) {
sum += monthDay[i];
}
sum+=day;
System.out.println(sum);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static char[][] key = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
int[] year = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] leap_year = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while ((s = br.readLine()) != null) {
String[] ss = s.split(" ");
int y = Integer.parseInt(ss[0]);
int m = Integer.parseInt(ss[1]);
int d = Integer.parseInt(ss[2]);
int sum = 0;
for (int i = 0; i < m; i++) {
if (i == m - 1)
sum += d;
else {
if ((y % 4 == 0) || (y % 100 == 0 && y % 400 == 0))
sum += leap_year[i];
else
sum += year[i];
}
}
System.out.println(sum);
}
}
}
先设两个数组,将闰年和普通年份的日期初始化。
再思考核心就是闰年判断以及日期相加。
year用于判断日期,month用于判断月份相加的天数,最后加上日期,
public class Main {
public static int isYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}else {
return 0;
}
}
public static void main(String[] args) {
int dayMonth[][]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
int year,month,day,yearJudge;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int dayNumber=0;
year = scanner.nextInt();
month = scanner.nextInt();
day = scanner.nextInt();
yearJudge = isYear(year);
for (int i = 0; i < month - 1; i++) {
dayNumber += dayMonth[yearJudge][i];
}
dayNumber += day;
System.out.println(dayNumber);
}
}
}
主要是要知道闰年判断的标准
import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt())
{
int year=sc.nextInt();
int month=sc.nextInt();
int day=sc.nextInt();
System.out.println(getNumOfDay(year,month,day));
}
}
private static int getNumOfDay(int year,int month,int day)
{
int result=0;
int [][] arr=
{
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
if((year%4==0&&year%100!=0)||year%400==0)//如果是闰年
{
for(int i=0;i<=month-2;i++)
{
result+=arr[1][i];
}
result+=day;
}
else
{
for(int i=0;i<=month-2;i++)
{
result+=arr[0][i];
}
result+=day;
}
return result ;
}
}
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
int year = scanner.nextInt();
int mouth = scanner.nextInt();
int day = scanner.nextInt();
LocalDate date = LocalDate.of(year, mouth, day);
LocalDate newYear = LocalDate.of(year, 1, 1);
System.out.println(newYear.until(date, ChronoUnit.DAYS)+1);
}
}
}