欢迎访问我的CCF认证解题目录
题目描述
思路过程
定义一个数组记录每个月的天数,注意判断是否为闰年,遍历这个数组,不断减去当月的天数,如果不够减,则说明是本月,输出
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int year = in.nextInt(), day = in.nextInt();
int[] m = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//每月的天数
if ( (year%4 == 0 && year%100 != 0) || year%400 == 0 ) m[2] = 29;//如果是闰年
for ( int i = 1; i < m.length; i++ ) {
if ( day > m[i] ) day -= m[i];
else {
System.out.println(i);
System.out.println(day);
break;
}
}
}
}