大數(shù)階乘java實現(xiàn)

節(jié)點類

public class Node {
Node pre;
Node next;
int data;

public Node() {
    super();
    
}

public Node(int data) {
    super();
    this.data = data;
}

public Node(Node pre, int data, Node next) {
    super();
    this.pre = pre;
    this.next = next;
    this.data = data;
}

public Node getPre() {
    return pre;
}

public void setPre(Node pre) {
    this.pre = pre;
}

public Node getNext() {
    return next;
}

public void setNext(Node next) {
    this.next = next;
}

public int getData() {
    return data;
}

public void setData(int data) {
    this.data = data;
}

}

雙向鏈表類

public class DblList {

private Node first;
private Node tail;
public void setFirst(Node first) {
    this.first = first;
}

public void setTail(Node tail) {
    this.tail = tail;
}
public DblList() {
    super();
    this.first=new Node();
}

public Node getTail() {
    return tail;
}

public Node getFirst() {
    return first;
}
private Node insert(int data) {
    Node firstnode =this.getFirst();
    //如果鏈表為空,需要設置尾結點為最先插入的節(jié)點
    if(firstnode.next==null) {
        //此時尾結點為空,tailnode不等于尾結點
        Node newNode=new Node(data);
        //設置尾結點
        this.tail=newNode;
        firstnode.next=newNode;
        newNode.pre=firstnode;
        return newNode;
    }
    else {
        Node next = firstnode.getNext();
        Node newNode = new Node(firstnode,data,next);
        next.pre=newNode;
        firstnode.next=newNode;
        return newNode;
    }
}


//大數(shù)階乘核心函數(shù),每個節(jié)點最多三位,不足三位的在輸出的時候需要補0
public void fac(int n) throws Exception {
    if(n<0) {
        throw new Exception("輸入?yún)?shù)不能小于0");
    }
    Node tailNode = this.tail;
    //每次取被乘數(shù)都是從后取,判斷鏈表是否為空,為空則先要處理一下鏈表。確保能取出來節(jié)點
    if(tailNode==null) {
        this.insert(1);
        tailNode = this.tail;
    }
    
    //循環(huán)乘n次,每次乘都是從后往前遍歷。乘數(shù)是從1-->n
    for (int i = 1; i < n+1; i++) {
        
        //存放余數(shù),一定放在for循環(huán)后,不能之前,否則出錯
        int remain=0;
        //設置成從后往前遍歷的游標
        Node current=tailNode;
        int result=0;
        //遍歷所有節(jié)點和乘數(shù)相乘
        while(current!=this.first) {
            result=current.data*i;
            //結果和余數(shù)相加再取余,結果保留三位
            result=result+remain;
            current.data=result%1000;
            remain=result/1000;
            
            //如果有余數(shù)且當前節(jié)點的前一個節(jié)點為頭結點則創(chuàng)建一個節(jié)點
            if(remain!=0&&current.pre==this.first) {
                while(remain>999) {
                    //一定要注意取新插入節(jié)點為當前節(jié)點,否則current=current.pre;這句語句不會轉(zhuǎn)到頭結點去,而會到新插入節(jié)點去,則又進入第一個while循環(huán)
                    Node insertNode = this.insert(remain%1000);
                    current=insertNode;
                    remain=remain/1000;
                }
                Node insertNode = this.insert(remain);
                //一定要注意取新插入節(jié)點為當前節(jié)點,否則current=current.pre;這句語句不會轉(zhuǎn)到頭結點去,而會到新插入節(jié)點去,則又進入第一個while循環(huán)
                current=insertNode;
            }
            current=current.pre;
        }
    }
    
}
public String output() throws Exception {
    Node firstnode = this.getFirst();
    StringBuffer sb;
    if(firstnode.next==null) {
        throw new Exception("空鏈表");
    }
    else {
        Node current=firstnode.next;
        sb=new StringBuffer();
        while(current!=null) {
            sb.append(String.format("%3d",current.data ).replace(" ", "0") );
            current=current.next;
        }
    }
    return sb.toString();
    
}

}

測試類

import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception {

for (int i = 20; i < 26; i++) {
    DblList dblList = new DblList();
    dblList.fac(i);
    System.out.println("i="+i+"結果如下");
    System.out.println(dblList.output());
    }
}

}
i=20結果如下
002432902008176640000
i=21結果如下
051090942171709440000
i=22結果如下
001124000727777607680000
i=23結果如下
025852016738884976640000
i=24結果如下
620448401733239439360000
i=25結果如下
015511210043330985984000000

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容