zoukankan      html  css  js  c++  java
  • 链表实现大数类阶乘

    链表实现大数阶乘
    ##题目 大数运算——计算n的阶乘 (n≥20)。 ##基本要求 (1)数据的表示和存储: ①累积运算的中间结果和最终的计算结果的数据类型要求是整型——这是问题本身的要求。 ②试设计合适的存储结构,要求每个元素或结点最多存储数据的3位数值。 (2)数据的操作及其实现: 基于设计的存储结构实现乘法操作,要求从键盘上输入n值;在屏幕上显示最终计算结果。 ##思路 建立大数类(其实好像想麻烦了),不过面向对象的方法还挺好写的,重载乘法,这里我搞麻烦了,一开始重载了大数乘法,其实弄一个大数乘整数就行了,然后其他的比较简单了,主要是细节,一直改Bug,终于好了。 > ###1)类定义
    class Biginteger {
    	Node *first;
    	int length;
    public:
    	Biginteger() {
    		first = new Node(0);
    	}
    	Node *GetHead() {
    		return first;
    	}
    	friend ostream&operator<<(ostream &, const Biginteger&);
    	friend istream&operator>>(istream &, Biginteger&);
    	Biginteger operator+(int);
    	Biginteger operator*(Biginteger &);
    	Biginteger operator*(int);
    	bool operator==(Biginteger&);
    	Biginteger Cal_Mul(Biginteger&);
    	Biginteger Cal_Mul(int);
    	void Delete();
    };
    

    2)类实现(只有关键部分)

    Biginteger Biginteger::operator*(int R)
    {
    	Biginteger C;
    	Node *pc = C.first, *pa = GetHead()->link;
    	pc->insertAfter(0);
    	int t = R % 10;
    	int temp;
    	int n;
    	int num = 0;
    	while (R) {
    		pa = GetHead()->link;
    		pc = C.GetHead();
    		n = 0;
    		while (n < num) {
    			pc = pc->link;
    			n++;
    		}
    		num++;
    		while (pa != NULL) {
    			temp = t * pa->value;
    			if (pc->link == NULL) {
    				pc->insertAfter(temp % 10);
    			}
    			else {
    				pc->link->value += (temp % 10);
    			}
    			pc = pc->link;
    			if (pc->value >= 10) {
    				if (pc->link == NULL) {
    					pc->insertAfter((pc->value) / 10);
    				}
    				else {
    					pc->link->value += ((pc->value) / 10);
    				}
    				pc->value = (pc->value) % 10;
    			}
    			if (temp >= 10) {
    				if (pc->link == NULL) {
    					pc->insertAfter(temp / 10);
    				}
    				else {
    					pc->link->value += (temp / 10);
    				}
    				if (pc->link->value >= 10) {
    					if (pc->link->link == NULL) {
    						pc->link->insertAfter((pc->link->value) / 10);
    					}
    					else {
    						pc->link->link->value += ((pc->link->value) / 10);
    					}
    					pc->link->value = (pc->link->value) % 10;
    				}
    			}
    
    			pa = pa->link;
    		}
    		R /= 10;
    		t = R % 10;
    		if (pc->link == NULL && pa != NULL)
    			pc = pc->insertAfter(0);
    		else	pc = pc->link;
    	}
    	return C;
    }
    

    3)阶乘实现

    Biginteger Biginteger::Cal_Mul(int R) {
    	Biginteger ans;
    	ans.first->insertAfter(1);
    	if (R == 1) {
    		return ans;
    	}
    	for (int i(2); i <= R; i++) {
    		ans = ans * i;
    	}
    	return ans;
    }
    

    后记

    依旧没有注释,提供一种思路吧,其实我觉得我写的很糙,太长了,而且一个结点只存了一个数字,有点浪费,不过亲测可用,思路差不多的可以参考一下。
    2018/11/14 23: 31:14

  • 相关阅读:
    [转]Windows visio2019破解激活
    KMP模式匹配算法
    【蓝桥杯2016_C++】t3:方格填数
    【蓝桥杯2015_C++】t4:格子中输出
    【蓝桥杯2015_C++】t3:奇妙的数字
    【蓝桥杯2014_C++】t6:扑克序列
    【蓝桥杯2014_C++】t4:史丰收速算
    【蓝桥杯2014_C++】t3:神奇算式
    【蓝桥杯2017_C++】t1:迷宫
    【蓝桥杯】买不到的数目
  • 原文地址:https://www.cnblogs.com/Titordong/p/9961129.html
Copyright © 2011-2022 走看看