特殊日期
【问题描述】
对于一个日期,我们可以计算出年份的各个数位上的数字之和,也可以分别计算月和日的各位数字之和。
请问从 1900 年 1 月 1 日至 9999 年 12 月 31 日,总共有多少天,年份的数位数字之和等于月的数位数字之和加日的数位数字之和。
【样例输入】
例如,2022年11月13日满足要求,因为 2+0+2+2=(1+1)+(1+3) 。
【答案要求】
请提交满足条件的日期的总数量。
【解题思路】
思路一:用java.time.LocalDate得到开始日期,结束日期;用for循环遍历开始日期到结束日期;if判断满足条件的日期
思路二:while循环,到达最后一天break;if判断满足条件的日期
import java.time.LocalDate; public class _01特殊日期 { public static void main(String[] args) { int count = 0; //设置开始日期 LocalDate startDate = LocalDate.of(1900, 1, 1); while (true) { if (startDate.getYear() == 9999 && startDate.getMonth().getValue() == 12//结束条件 && startDate.getDayOfMonth() == 31) { break; } int y1 = startDate.getYear() / 1000%10; int y2 = startDate.getYear() / 100 % 10; int y3 = startDate.getYear() / 10 % 10; int y4 = startDate.getYear() /1% 10; int m1 = startDate.getMonth().getValue() / 10; int m2 = startDate.getMonth().getValue() % 10; int d1 = startDate.getDayOfMonth() / 10; int d2 = startDate.getDayOfMonth() % 10; if (y1 + y2 + y3 + y4 == m1 + m2 + d1 + d2) { count++; } startDate = startDate.plusDays(1); } System.out.println(count); } }
浙公网安备 33010602011771号