- 黑色星期五
要点:基姆拉尔森公式。
import java.util.Scanner;
public class day04prc1 {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
for(int m=1;m<13;m++){
int W=0;
if(m==1 || m==2){
W = (13+2*(m+12)+3*(m+13)/5+y-1+(y-1)/4-(y-1)/100+(y-1)/400+1)%7;
}
else{
W= (13+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
}
if(W==5)
System.out.println(y+"-"+m+"-"+"13");
}
}
}
- 高斯日记
思路:首先介绍java的Calendar类:
首先初始化:Calendar calendar = Calendar.getInstance();
取的化也很方便,calendar.get(Calendar.YEAR) 表示取年,同理
calendar.get(Calendar.MONTH) 表示取月份,注意月份是从0月开始算的.
calendar.get(Calendar.DATE)表示几号
calendar.set(1777,1,12+500)算当前日期后500天的日子
其就能自动变成500天后的日期,不需要管闰年大小月之类.
要点:1.提交答案的格式是yy-mm–dd!!所以最终结果写成1799-07-16!而不是1799-7-16.
2.月份是从0开始算的!
import java.util.Calendar;
public class day04prc2 {
public static void main(String []args){
Calendar calender = Calendar.getInstance();
calender.set(1777,3,30+5343);
System.out.println(calender.get(Calendar.YEAR));
System.out.println(calender.get(Calendar.MONTH)+1);
System.out.print(calender.get(Calendar.DAY_OF_MONTH)-1);
}
}
- 日期问题
思路:转化为数字查看是否合格。用set集合存储去重。
要点:要注意平年、闰年的2月份天数不同。
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class day04prc3 {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int a = Integer.parseInt(s.substring(0, 2));
int b = Integer.parseInt(s.substring(3, 5));
int c = Integer.parseInt(s.substring(6, 8));
int []arr = new int[6];
Set<String> jihe = new HashSet<String>();
arr[0] = (2000+a)*10000+b*100+c;
arr[1] = (2000+c)*10000+a*100+b;
arr[2] = (2000+c)*10000+b*100+a;
arr[3] = (1900+a)*10000+b*100+c;
arr[4] = (1900+c)*10000+a*100+b;
arr[5] = (1900+c)*10000+b*100+a;
Arrays.sort(arr);
for(int i=0;i<6;i++){
int year = arr[i]/10000;
int month = arr[i]%10000/100;
int day = arr[i]%10000%100;
if(year>=1960 && year<=2059 && month>=1 && month<=12 && check(year,month,day))
jihe.add(String.valueOf(arr[i]));
}
for(String each:jihe){
System.out.println(each.substring(0, 4)+"-"+each.substring(4, 6)+"-"+each.substring(6,8));
}
}
private static boolean check(int year, int month, int day) {
int []runDay = {31,29,31,30,31,30,31,31,30,31,30,31};
int []pingDay = {31,28,31,30,31,30,31,31,30,31,30,31};
if((year%4==0 && year%100!=0 ) || year%400 == 0)
{
if(day<=runDay[month-1] && day>=1)
return true;
}
else
{
if(day<=pingDay[month-1] && day>=1)
return true;
}
return false;
}
}