判断闰年的条件, 能被4整除同时不能被100整除,或者能被400整除
public static boolean leapYear(int year) {
if (year < 0) {
throw new IllegalArgumentException(year + "");
}
if (year % 4 == 0 && year % 100 != 0) {
return true;
} else if (year % 400 == 0) {
return true;
}
return false;
}
一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数
public static Set<Integer> wanshu(int start, int limit) {
if (start < 1 || start > limit) {
throw new IllegalArgumentException("start=" + start + " limit=" + limit);
}
Set<Integer> set = new TreeSet<>();
for (int i = start; i < limit; i++) {
int s = 0;
for(int j = 1; j < i; j++) {
if(i % j == 0) {
s+=j;
}
}
if(s == i) {
set.add(i);
}
}
return set;
}
不增加新的变量来交换a b两个变量的值
//方式一
a = a + b;
b = a - b;
a = a - b;
//方式二
a = a ^ b;
b = a ^ b;
a = a ^ b;