package jiecheng;
public class Multiply {
public Multiply(){
}
public Multiply(int x) {
this.setX(x);
}
private int x = 0;//表示x的阶乘
private int length=0;//计算结果的长度
public int getX() {
return x;
}
/**
* 设置x的值,当x值为负数时取绝对值
* @param x
* 如当x为-1时,则计算1的阶乘
*/
public void setX(int x) {
this.x = x>0?x:-x;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
/**
* 用于测试的main方法
* @param args
*/
public static void main(String[] args) {
Multiply m = new Multiply(1000);
System.out.println("运算开始...");
long startTime = System.currentTimeMillis();
byte[] des = m.calculate(new byte[1]);
for(int i=0;i<des.length;i++){
System.out.print(des[i]);
if((i+1)%50==0){
System.out.println();
}
m.setLength(m.getLength()+1);
}
long endTime = System.currentTimeMillis();
System.out.println("\n运算结束!\n共耗时:"+(endTime-startTime)/1000+"秒"+
(endTime-startTime)%1000+"毫秒");
System.out.println("总长度为:"+m.getLength());
}
/**
*
* @return result
* 以字符串形式返回结果
* 格式: "结果,数据长度,耗时毫秒"
*/
public String showResult(){
Multiply m = new Multiply(this.getX());
String result="";
long startTime = System.currentTimeMillis();
byte[] des = m.calculate(new byte[1]);
for(int i=0;i<des.length;i++){
result += des[i];
m.setLength(m.getLength()+1);
}
long endTime = System.currentTimeMillis();
return result+","+m.getLength()+","+(endTime-startTime)/1000;
}
/**
* 计算x!并存入des字节数组中
* @param des
* @return
*/
public byte[] calculate(byte[] des) {
des[0] = 1;
for (int i = 0; i <this.getX(); i++) {
des = ArrayMultiply(des ,tobyteArray(i+1, 0, new byte[1]),new byte[1]);
}
return des;
}
public byte[] calculate(int x,byte[] des) {
des = tobyteArray(x, 0, new byte[1]);
return des;
}
/**
* 一个数组乘以一个数后并调整OK
* @param b
* @param x
* @param result
* @param offset
* 补足0的个数
* @return
*/
public byte[] ArrayMultiply(byte[] b,int x,byte[] result,int offset){
int carrier = 0;//进位值
byte[] bt = (byte[])b.clone();
for(int i=bt.length-1;i>=0;i--){
bt[i] *= x;
int temp = carrier;
carrier = (bt[i]+carrier)/10;
bt[i] = (byte) ((bt[i]+temp)%10);
if(i-1<0&&carrier>0){
result = new byte[bt.length+1];
System.arraycopy(bt,0,result,1,bt.length);
result[0] = (byte) carrier;
if(offset>0){
byte[] bb = shiftArray(offset,result);
return bb;
}
return result;
}
}
if(offset>0){
byte[] bb = shiftArray(offset, bt);
return bb;
}
return bt;
}
/**
* 数组平移,最右边补足offset个零
* @param offset
* @param bt
* @return
*/
private byte[] shiftArray(int offset, byte[] bt) {
byte[] bb = new byte[bt.length+offset];
System.arraycopy(bt,0,bb,0,bt.length);
return bb;
}
/**
* 计算两个数组相乘,返回一个新的数组OK
* @param b1
* {7,5,3,9}
* @param b2
* {4,2,6,8,9,4}
* @param des
* @return
*/
private byte[] ArrayMultiply(byte[] b1,byte[] b2,byte[] des){
for(int i=b1.length-1;i>=0;i--){
//byte[] b = ArrayMultiply(b2,b1[i],new byte[1],b1.length-1-i);
des = ArrayAdd((byte[])des.clone(),ArrayMultiply(b2,b1[i],new byte[1],b1.length-1-i),new byte[1]);
}
return des;
}
/**
* 两个数组进行按位求和,并按十进制调整长度OK
* @param des
* @param bs
* @param result
* @return
*/
private byte[] ArrayAdd(byte[] des, byte[] bs,byte[] result) {
byte[] shortArray = des.length<bs.length?des:bs;
byte[] longArray = des.length<bs.length?bs:des;
for(int i=shortArray.length-1;i>=0;i--){
longArray[longArray.length-1-(shortArray.length-1-i)] += shortArray[i];
if(longArray[longArray.length-1-(shortArray.length-1-i)]>9){
longArray = AdjustArray((byte[])longArray.clone());
}
}
return longArray;
}
/**
* 调整一个数组,按10进制进行调整,由src.length-1到0位进行调整 OK
* @param src
* @return
*/
private byte[] AdjustArray(byte[] src) {
for (int i = src.length-1; i >=0; i--) {
if (src[i] > 9) {
if (i - 1 <0) {
byte[] temp = new byte[src.length + 1];
System.arraycopy(src, 0, temp, 1, src.length);
temp[0] += temp[i]/10;
temp[i] %= 10;
src = AdjustArray(temp);
} else {
src[i - 1] += src[i]/10;
src[i] %= 10;
}
}
}
return src;
}
/**
* 将一个整数转化为byte数组OK
* @param x
* @param index
* 初始为0
* @param src
* 初始长度1
* @return
*/
private byte[] tobyteArray(int x,int index,byte[] src){
src[index] = (byte) (x%10);
x /= 10;
if(x>0){
index++;
byte[] temp = new byte[src.length+1];
System.arraycopy(src,0,temp,0,src.length);
if(x<=9){
temp[index] = (byte) x;
reverseArray(temp);
return temp;
}else{
src = tobyteArray(x,index,temp);
}
}
return src;
}
/**
* 数组的反转OK
* @param src
*/
private void reverseArray(byte[] src) {
if(src.length%2==0){
for(int i=0;i<src.length/2;i++){
swap(src, i);
}
}else{
for(int i=0;i<(src.length-1)/2;i++){
swap(src, i);
}
}
}
/**
* 交换数组中两个整数的位置,不定义中间变量OK
* @param src
* @param i
*/
private void swap(byte[] src, int i) {
src[i] ^= src[src.length-i-1];
src[src.length-i-1] ^= src[i];
src[i] ^= src[src.length-i-1];
}
}