作为一名应届毕业生,算法不是那么的大神,但充分体现了我半个数学生的身份,hhhhh,我们知道杨辉三角每个数等于它上方两个数之和,刚开始我就很老实,搞了一个二维数组列出杨辉三角n行的所有数,后来我发现当n大于一定值后就会数组溢出,作为一名半个数学系的人我怎么可以忍受得了这个限制,然后我发现杨辉三角第n行第m列的值无C(n-1,m-1),那为何不用这个公式求解。用这个公式那就要牵扯到阶乘的问题,但是吧,int类型只能求20 以内的阶乘,long类型只能求到50以内的阶乘,这又有的限制,那现在只好研究研究大数阶乘的问题,enmmmmm,这下就成了大数阶乘大数相除的算法研究了,我是利用数组存数据,然后利用数组进行大数相乘以及大数相除;话不多说,上代码。欢迎大家来讨论更优化的算法。
这里写代码片
public class GetYangShuZhi {
//杨辉三角第n行第m列的值为C(n-1,m-1)=n!/m!*(n-m)!
public static String getYSnumber(int n,int m)
{
n–;
m–;
int[] beichushu;
int[] chushu;
if(n-m>=m){
beichushu = Getnumber.method(n, n-m+1);//获取被除数
System.out.print("被除数");
for(int i=0;i<beichushu.length;i++){
System.out.print(beichushu[i]);
}
System.out.println();
System.out.print("除数");
chushu = Getnumber.method(m, 1);
for(int i=0;i<chushu.length;i++){
System.out.print(chushu[i]);
}
System.out.println();
}else{
beichushu = Getnumber.method(n, m+1);//获取除数
System.out.print("被除数");
for(int i=0;i<beichushu.length;i++){
System.out.print(beichushu[i]);
}
System.out.println();
chushu = Getnumber.method(n-m, 1);
System.out.print("除数");
for(int i=0;i<chushu.length;i++){
System.out.print(chushu[i]);
}
}
getChu c = new getChu();
String result=c.chu(beichushu, chushu);
System.out.println("jieguo=="+result);
return result;
}
//将结果格式化,以便于输出在文本框里
public static String getResult(int n ,int m){
java.util.Date date = new java.util.Date();
long startime = date.getTime();
String result = getYSnumber(n,m);
int[] result1 = getChu.stringToIntArr(result);
java.util.Date end = new java.util.Date();
long endtime = end.getTime();
result1 = ArryUtils.deletZero(result1, 2);
StringBuilder s = new StringBuilder();
for(int i=0;i<result1.length;i++){
s.append(result1[i]);
if(i % 60 == 0&& i!=0) {
//System.out.println("\n"+result1[i]);
s.append("\n"+result1[i]);
}
}
System.out.println("hh"+(endtime-startime));
return s.toString();
}
public static void main(String[] args) {
getResult(10,6);
}
}
public class ArryUtils {
//除去数组前面的0,限制数组的长度不能短于limit。若短于limit自动补零
public static int[] deletZero(int[] arry,int limit)
{
int result[];
if(arry.length>limit){
int length=0;
for(int i=0;i<arry.length;i++)
{
if(arry[i]!=0){
length=arry.length-i;
break;
}
}
if(length<limit){
result= new int[limit];
int index=0;
while(result[0]==0){
if(index+limit<arry.length){
for(int i=0;i<result.length;i++){
result[i]=arry[index+i];
}
index++;
}
else{
break;
}
}
for(int i =0;i<result.length;i++){
result[i]=arry[index+i];
}
return result;
}
else{
result = new int[length];
int index=0;
while(result[0]==0){
for(int i=0;i<result.length;i++){
result[i]=arry[index+i];
}
index++;
}
return result;
}
}
else{
result = new int[limit];
for(int i=0;i<arry.length;i++){
result[i+(limit-arry.length)]=arry[i];
}
return result;
}
}
public static void main(String[] args) {
int n[] = {6,8,9};
int[] result=ArryUtils.deletZero(n,4);
for(int i=0;i<result.length;i++){
System.out.print(result[i]);
}
}
}
import java.math.BigInteger;
public class getChu {
public String chu(int[] beichushu,int[] chushu)
{
StringBuilder quotient = new StringBuilder();
StringBuilder remainder = new StringBuilder();
for (int i = 0; i < beichushu.length; i++) {
remainder.append(beichushu[i]);
if ((i + 1) < chushu.length) {
quotient.append(0);
continue;
}
System.out.println("商 "+quotient.toString()+" 余数"+remainder.toString());
switch (intArrCompaerTo(stringToIntArr(remainder.toString()), chushu,chushu.length)) {
case -1:
quotient.append(0);
continue;
case 0:
quotient.append(1);
remainder.delete(0, remainder.length());
break;
case 1:
int jTemp[] = new int[1];
int proTemp[] = null;
for (int j = 1; j < 10; j++) {
jTemp[0] = j;
proTemp = mulNum(jTemp, chushu);
int c = intArrCompaerTo(proTemp, stringToIntArr(remainder.toString()),chushu.length);
if (c == -1 && j != 9) {
continue;
}if ((c == -1 && j == 9) || c == 0) {
} else {
jTemp[0] = --j;
System.out.println("往前执行一次"+j);
proTemp = mulNum(jTemp, chushu);
}
quotient.append(j);
int[] remTemp = stringToIntArr(remainder.toString());
remainder.delete(0, remainder.length());
remainder.append(intArrToString(subNum(remTemp, proTemp,chushu.length)));
System.out.print("集合余数相减结果");
System.out.println(intArrToString(subNum(remTemp, proTemp,chushu.length)));
break;
}
break;
}
}
return quotient.toString();
}
/**
* 将数组转化为字符串
* params:将要转化的数组
* return:转换后的字符串
* **/
public static String intArrToString(int[] atty) {
// TODO Auto-generated method stub
StringBuilder s = new StringBuilder();
for(int i = 0;i