基本数学问题(一)

一、判断闰年

判断闰年:闰年是历法上的一种折中,主要是为弥补因人为制定的历法而造成的年度天数与地球实际公转周期的时间差而设置的,也就是说,补上时间差的年份为闰年,闰年的一个基本规则就是“四年一闰,百年不闰,四百年再闰”,就是能被4整除,但同时不被100整除却能够被400整除的年份。

public class LeapYear {	
	static int IfLeapYear(int year) {
		if((year%400==0)|| (year%100!=0) && (year%4==0)) {
			return 1;
		}else {
			return 0;
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int year = in.nextInt();
		int flag;
		flag = IfLeapYear(year);
		
		if(flag==1) {
			System.out.println(year+"是闰年!");
		}else {
			System.out.println(year+"不是闰年!");
		}
	}

二、多项式计算

1、一维多项式

一维多项式求值
一维多项式形如:f(x)=a(n-1)*x^(n-1)+a(n-2)*x^(n-2)+a(1)*x+a(0)

首先将多项式表述成如下嵌套形式:
f(x)=(((a(n-1)*x+a(n-2))*x+a(n-3))*x+...+a1)*x+a0
然后从里往外一层一层进行计算。

public class OnePolynomial {

	static double polynomial(double a[], int n, double x) {
		int i;
		double result;
		result = a[n-1];
		for(i=n-2;i>=0;i--) {
			result = result*x+a[i];
		}
		return result;
	}
	public static void main(String[] args) {
		int i;
		double a[] = {-15.0,-7.0,7.0,2.0,-3.0,7.0,3.0};
		double[] x = {-2.0,-0.5,1.0,2.0,3.7,4.0};
		double result;
		
		DecimalFormat df = new DecimalFormat("0.0000000E000");
		DecimalFormat df1 = new DecimalFormat("0.00");
		for(i=0;i<6;i++) {
			result = polynomial(a,7,x[i]);
			System.out.println("x="+df1.format(x[i])+"时,p(x)="+df.format(result));
		}
		System.out.println();
	}

}

2.二维多项式求值

二维多项式形如f(x,y)=∑∑a(i,j)X^i*Y^j  (i=0~m-1,j=0~n-1),将二维多项式变形如下:f(x,y)=∑∑a(i,j)X^i*Y^j=∑[∑(a(i,j)*X^i)*Y^j]  (i=0~m-1,j=0~n-1),令S(i)=∑(a(i,j)*X^i)*Y^j (i=0~...。

public class TwoPolynomial {
	
	static double polynomial(double a[][],int m, int n, double x, double y) {
		int i,j;
		double result,temp,tt;
		result = 0.0;
		tt = 1.0;
		for(i=0;i<m;i++) {
			temp = a[i][n-1]*tt;
			for(j=n-2;j>=0;j--) {
				temp = temp*y+a[i][j]*tt;
			}
			result+=temp;
			tt*=x;
		}
		return result; 
	}

	public static void main(String[] args) {
		double result;
		double x,y;
		DecimalFormat df = new DecimalFormat("0.000E000");
		double a[][] = {{1.0,2.0,3.0,4.0,5.0},
				{6.0,7.0,8.0,9.0,10.0},
				{11.0,12.0,13.0,14.0,15.0},
				{16.0,17.0,18.0,19.0,20.0}};
		x=0.5;y=-2.0;
		System.out.println("二维多项式求值:");
		result = polynomial(a,4,5,x,y);
		System.out.println("P("+x+","+y+")="+df.format(result));
	}

}

三、随机算法

随机数在很多场合都有用,最典型的应用在互联网上登录论坛、网上银行等,网页会给出随机的验证码,此外,随机数在加密/解密算法也有着重要的用途。下面介绍0~1之间均匀随机数。

public class RandomAlgorithm {
	
	static double rand(double[] r) {
		double base,u,v,temp1,temp2,temp3,p;
		base = 256.0;
		u = 17.0;
		v = 139.0;
		temp1=u*(r[0])+v;
		temp2=(int)(temp1/base);
		temp3=temp1-temp2*base;
		r[0]=temp3;
		p=r[0]/base;
		return p;
	}

	public static void main(String[] args) {
		int i;
		double[] r= {5.0};
		
		System.out.println("产生10个随机数(0~1):");
		for(i=0;i<10;i++) {
			System.out.printf("%10.5f\n",rand(r));
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值