節(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&¤t.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