64 lines
1.4 KiB
Java
64 lines
1.4 KiB
Java
package LanQiaoOJ;
|
||
/*完全日期*/
|
||
/*如果一个日期中年月日的各位数字之和是完全平方数,则称为一个完全日期。
|
||
例如:2021 年 6 月 5 日的各位数字之和为 2+0+2+1+6+5=16而 16 是一个完全平方数,它是 4 的平方。
|
||
所以 2021 年 6 月 5 日是一个完全日期。
|
||
例如:2021 年 6 月 23 日的各位数字之和为 2+0+2+1+6+2+3=16,是一个完全平方数。所以 2021 年 6 月 23 日也是一个完全日期。
|
||
请问,从 2001 年 1 月1 日到 2021 年 12 月 31 日中,一共有多少个完全日期?*/
|
||
public class 完全日期 {
|
||
//每月天数,下标从1开始
|
||
static int [] w= {0,31,28,31,30,31,30,31,31,30,31,30,31};
|
||
//年月日
|
||
static int y=2001,m=1,d=1;
|
||
|
||
public static void main(String[] args) {
|
||
int count=0;
|
||
//循环结束条件
|
||
while(y!=2021||m!=12||d!=31) {
|
||
//判断是否闰年——>判断2月天数
|
||
if(y%400==0||y/400==0&&y/100!=0)
|
||
w[2]=29;
|
||
else
|
||
w[2]=28;
|
||
//判断是否完全平方
|
||
if(check())
|
||
count++;
|
||
//日期变更
|
||
d++;
|
||
if(d>w[m]) {
|
||
m++;
|
||
d=1;
|
||
}
|
||
if(m>12) {
|
||
y++;
|
||
m=1;
|
||
}
|
||
}
|
||
System.out.print(count);
|
||
}
|
||
static boolean check() {
|
||
//统计
|
||
int sum=0;
|
||
//y为全局变量,不可直接改动
|
||
int a=y;
|
||
//得出年份各数字之和
|
||
while(a>0) {
|
||
sum+=a%10;
|
||
a/=10;
|
||
}
|
||
int b=m;
|
||
while(b>0) {
|
||
sum+=b%10;
|
||
b/=10;
|
||
}
|
||
int c=d;
|
||
while(c>0) {
|
||
sum+=c%10;
|
||
c/=10;
|
||
}
|
||
//判断是否为平方数
|
||
int mark=(int)Math.sqrt(sum);
|
||
return mark*mark==sum;
|
||
}
|
||
}
|