1.判断一个日期是否合法
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int year,int month,int day)
{
if (month == 0 || month > 12) return false;
if(((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) days[2] = 29;
if (day > days[month] || day <= 0) return false;
days[2] = 28;//是闰年改为28,不是也不影响原来的值。
return true;
}
可以用来遍历日期
2.判断是否闰年
static boolean is(int x)
{
return x%400==0 || (x%4==0 && x%100!=0);
}
3.最大公约数
//最大公约数
public static long gcd(long x,long y)
{
return y==0?x:gcd(y, x%y);
}
4.最小公倍数
public static int lcm(int a,int b) {
return a/gcd(a,b)*b;
}
先除再乘可以防止溢出
5.找素数
public static boolean is(int n) {
if(n==1)
return false;
for(int i=2;i*i<=Math.sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
6.计算阶乘
public static int jie(int n)//计算阶乘
{
if(n==1)
{
return 1;
}
else {
return n*jie(n-1);
}
}
7.计算组合数
public static int xuan(int n,int x)//计算组合数
{
return jie(n)/(jie(x)*jie(n-x));
}