📄 subject_32766.htm
字号:
<p>
序号:32766 发表者:竹叶 发表日期:2003-03-14 16:51:49
<br>主题:任意数的阶乘
<br>内容:求一种算法:<BR>用整型数来完成求任意数的阶乘<BR>要求:<BR>1)程序中出现的所有数都必须只用整型表示<BR>2)输入:任意一个整数<BR>3)输出:该整数的阶乘<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:bird 回复日期:2003-03-14 17:16:36
<br>内容:一个解决这个问题的一个方法是:动态创建一个链表,使得链表中的每个元素代表计算结果中的一个数字,这样,理论上讲,计算结果的长度仅仅受到内存大小的制约!<BR><BR>//<BR><BR>#include "stdafx.h"<BR><BR>#include "iostream.h"<BR>#include "stdlib.h"<BR><BR><BR>//here is a dual link list<BR>class Node{<BR> <BR>private:<BR> int data;<BR> Node *next;<BR> Node *prev;<BR> Node *head;<BR> Node *rear;<BR> <BR><BR>public:<BR> Node(const int& item)<BR> :data(item),prev(NULL),next(NULL),head(NULL),rear(NULL){};<BR><BR> //get next node<BR> Node* GetNextNode(){return next;};<BR> Node* GetPrevNode(){return prev;};<BR><BR> //insert after<BR> void InsertAfterMe(Node* p);<BR><BR> //Delete the appointed<BR> void DeleteMe(void);<BR><BR> int GetData(void){return data;};<BR> void SetData(int item){data = item;};<BR><BR> //reset<BR> Node* GoBacktoHead();<BR><BR> //go to the rear<BR> Node* GoForwardtoRear();<BR> //clear the whole<BR> void ClearAll(void);<BR><BR> //get the counts of the link<BR> int GetElementNum();<BR>};<BR><BR><BR>int Node::GetElementNum()<BR>{<BR> int count = 0;<BR> Node* p =GoBacktoHead();<BR> <BR> while(p->GetNextNode()!=NULL){ <BR> count++;<BR> p = p->GetNextNode();<BR> }<BR><BR> count++;<BR> return count;<BR>}<BR><BR>void Node::InsertAfterMe(Node* p)<BR>{<BR> // Node* p;<BR> if(prev == NULL) { head = this;}<BR> p->next = next;<BR> p->prev = this;<BR> next = p;<BR> if(p->next == NULL){rear = p;}<BR>};<BR><BR><BR>void Node::DeleteMe(void)<BR>{<BR> if(prev == NULL) { // if this node is the first one<BR> next->prev = NULL;<BR> head = next; // then the next one becomes the first one<BR> delete this; //delete this node<BR> return;<BR> }<BR><BR> if(next == NULL){ //if this node is the last one<BR> prev->next = NULL;<BR> rear = prev; // then the previous one becomes the last one<BR> return;<BR> }<BR><BR> prev->next = next;<BR> delete this;<BR>};<BR><BR>Node* Node::GoBacktoHead()<BR>{<BR> if(head == this){ //this is the first node<BR> return this;<BR> }<BR><BR> Node *p = this;<BR> while(p->prev != NULL){<BR> p = p->prev;<BR> }<BR><BR> return p;<BR>}<BR><BR>Node* Node::GoForwardtoRear()<BR>{<BR> if(rear == this){<BR> return this;<BR> }<BR><BR> Node *p = this;<BR> while(p->next != NULL){<BR> p = p->next;<BR> }<BR><BR> return p;<BR>}<BR><BR>void Node::ClearAll(void)<BR>{<BR> Node* p = GoBacktoHead();<BR> Node* p2;<BR> while(p->GetNextNode() != NULL){<BR> p2 = p;<BR> p = p->GetNextNode();<BR> delete p2;<BR> }<BR><BR> delete p;<BR>};<BR><BR>int main(int argc, char* argv[])<BR>{<BR> int remain;<BR> int carry;<BR> int result;<BR> int N;<BR> Node* p = new Node(1);<BR><BR> cout<<"Pls input the number:";<BR> cin>>N;<BR> for(int n=1;n<=N;n++)<BR> {<BR> remain = carry = 0;<BR> p = p->GoBacktoHead();<BR><BR> //while not the end of the list,process the element one by one<BR> while(p->GetNextNode() != NULL){<BR> result = p->GetData()*n+carry;<BR> if(result>=10){<BR> remain = result%10;<BR> carry = result/10;<BR> p->SetData(remain);<BR> }<BR> else{p->SetData(result);}<BR><BR> p = p->GetNextNode();<BR> carry = result/10;<BR> }<BR><BR> result = p->GetData()*n+carry;<BR> <BR> //if carry occurs,process the carry and <BR> //store into the newly allocated space.<BR><BR> while(result >= 10){<BR> Node * newNode = new Node(0);<BR> p->SetData(result%10);//remainder<BR> result = result/10;<BR> p->InsertAfterMe(newNode);<BR> p = p->GetNextNode();<BR> }<BR><BR> p->SetData(result);<BR><BR> }//end of if<BR><BR> p = p->GoForwardtoRear();<BR><BR> while(p->GetPrevNode()!=NULL){<BR> cout<<p->GetData();<BR> p=p->GetPrevNode();<BR> }<BR><BR> cout<<p->GetData()<<endl;<BR> int num = p->GetElementNum();<BR> if(num >=5){<BR> p = p->GoForwardtoRear();<BR><BR> cout<<endl<<"Or"<<endl<<endl;<BR><BR> cout<<p->GetData()<<".";<BR> p = p->GetPrevNode();<BR><BR> for(int i=1;i<5;i++){<BR> cout<<p->GetData();<BR> p = p->GetPrevNode();<BR> }<BR><BR> cout<<"E"<<num-1<<endl;<BR> }<BR><BR> //clear the memory<BR> p->ClearAll();<BR><BR> return 0;<BR>}<BR>2003-3-14 17:18:20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -