蓝桥杯c/java常用模板

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));
	}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值