北大的ACM 1001

poj.org/problem?id=1001

代码纯手动编写 - -

 #include <iostream>
#include <cstdio>
#include <cstring> class BigNumber
{
struct BigNumberNode
{
BigNumberNode():n(0), prev(NULL), next(NULL){}
BigNumberNode(int N):n(N), prev(NULL), next(NULL){} int n;
BigNumberNode *prev, *next;
}; BigNumberNode *head, *tail;
int dot_before; // 小数点之前的数量
int dot_later; // 小数点之后的数量 void InsertNodeAtBegin(int n)
{
BigNumberNode *new_node = new BigNumberNode(n);
if(head != NULL)
{
head->prev = new_node;
new_node->prev = NULL;
new_node->next = head;
head = new_node;
}
else
{
head = new_node;
tail = new_node;
new_node->prev = NULL;
new_node->next = NULL;
}
} void InsetNodeAtEnd(int n)
{
BigNumberNode *new_node = new BigNumberNode(n);
if(head != NULL)
{
new_node->prev = tail;
new_node->next = NULL;
tail->next = new_node;
tail = new_node;
}
else
{
head = new_node;
tail = new_node;
new_node->prev = NULL;
new_node->next = NULL;
}
} void DeleteBegin()
{
if(head != NULL)
{
if (head->next != NULL)
{
BigNumberNode* temp = head->next;
delete head;
head = temp;
temp->prev = NULL; if (dot_before != 0)
{
dot_before--;
}
else
{
dot_later--;
}
}
else
{
Free();
}
}
} void DeleteEnd()
{
if(tail != NULL)
{
if (tail->prev != NULL)
{
BigNumberNode* temp = tail->prev;
delete tail;
tail = temp;
temp->next = NULL; if (dot_later != 0)
{
dot_later--;
}
else
{
dot_before--;
}
}
else
{
Free();
}
}
} // 去除前/后导 0
// 去除前/后导 0
void ClearZeros()
{
// 后导 0
BigNumberNode *p; int max_limit = dot_later;
for(int i = 0; i < max_limit; i++)
{
p = tail; if(p->n == 0)
{
DeleteEnd();
}
else
{
break;
}
} // 前导 0
max_limit = dot_before;
for(int i = 0; i < max_limit; i++)
{
p = head; if(p->n == 0)
{
DeleteBegin();
}
else
{
break;
}
}
} BigNumber Mul(const BigNumber &Scale)
{
const BigNumber &a = *this, &b = Scale;
BigNumber sum_big_number; int sum = 0;
int reserve = 0;
int tag = 0; BigNumberNode *pa = a.tail, *pb = b.tail; // xxxxxx a
// xxx b
// --------- Mul
// xxxxxx
// xxxxxx
// xxxxxx
// --------- Add
// xxxxxxxxx while(pb)
{
BigNumber temp_big_number;
pa = a.tail;
while(pa)
{
sum = pa->n * pb->n + reserve;
reserve = sum / 10;
temp_big_number.InsertNodeAtBegin(sum - reserve * 10);
temp_big_number.dot_before++;
pa = pa->prev;
} if (reserve)
{
temp_big_number.InsertNodeAtBegin(reserve);
temp_big_number.dot_before++;
reserve = 0;
} for (int i = 0; i < tag; i++)
{
temp_big_number.InsetNodeAtEnd(0);
temp_big_number.dot_before++;
} sum_big_number += temp_big_number; tag++;
pb = pb->prev;
} sum_big_number.dot_later = a.dot_later + b.dot_later; if (sum_big_number.dot_before > sum_big_number.dot_later)
{
sum_big_number.dot_before -= sum_big_number.dot_later;
}
else
{
int temp = sum_big_number.dot_later - sum_big_number.dot_before;
sum_big_number.dot_before = 0;
for (int i = 0; i < temp; i++)
{
sum_big_number.InsertNodeAtBegin(0);
}
} sum_big_number.ClearZeros(); return sum_big_number;
} public: ~BigNumber()
{
Free();
} BigNumber(): head(NULL), tail(NULL), dot_before(0), dot_later(0)
{
} BigNumber(const char *Str): head(NULL), tail(NULL), dot_before(0), dot_later(0)
{
Free();
AdaptFormString(Str);
} BigNumber(const BigNumber& Source): head(NULL), tail(NULL), dot_before(0), dot_later(0)
{
*this = Source;
} const BigNumber& operator=(const BigNumber& Source)
{
if (this != &Source)
{
this->Free(); BigNumberNode *p = Source.head;
while(p)
{
this->InsetNodeAtEnd(p->n);
p = p->next;
} this->dot_before = Source.dot_before;
this->dot_later = Source.dot_later;
} return *this;
} BigNumber operator+(const BigNumber Addend)
{
const BigNumber &a = *this, &b = Addend;
BigNumber new_number; BigNumberNode *pa, *pb;
int sum, remain, odd;
int reserve = 0;
bool is_dot_before_longer_is_a; // 帮助标记小数点之前的部分
bool is_dot_later_longer_is_a; // 小数点之后
if(a.dot_later > b.dot_later)
{
pa = a.tail;
pb = b.tail;
remain = b.dot_later;
odd = a.dot_later - b.dot_later;
is_dot_later_longer_is_a = true;
}
else
{
pa = b.tail;
pb = a.tail;
remain = a.dot_later;
odd = b.dot_later - a.dot_later;
is_dot_later_longer_is_a = false;
} for (int i = 0; i < odd; i++)
{
new_number.InsertNodeAtBegin(pa->n);
new_number.dot_later++;
pa = pa->prev;
} for (int i = 0; i < remain; i++)
{
sum = pa->n + pb->n + reserve;
reserve = sum / 10;
new_number.InsertNodeAtBegin(sum - reserve * 10);
new_number.dot_later++;
pa = pa->prev;
pb = pb->prev;
} // 小数点之前
if(a.dot_before > b.dot_before)
{
remain = b.dot_before;
odd = a.dot_before - b.dot_before;
is_dot_before_longer_is_a = true;
}
else
{
remain = a.dot_before;
odd = b.dot_before - a.dot_before;
is_dot_before_longer_is_a = false;
} BigNumberNode *temp; // 用于交换 pa pb
if (is_dot_before_longer_is_a && is_dot_later_longer_is_a
|| !is_dot_before_longer_is_a && !is_dot_later_longer_is_a)
{
// 不用交换
}
else
{
temp = pa;
pa = pb;
pb = temp;
} for (int i = 0; i < remain; i++)
{
sum = pa->n + pb->n + reserve;
reserve = sum / 10;
new_number.InsertNodeAtBegin(sum - reserve * 10);
new_number.dot_before++;
pa = pa->prev;
pb = pb->prev;
} for (int i = 0; i < odd; i++)
{
sum = pa->n + reserve;
reserve = sum / 10;
new_number.InsertNodeAtBegin(sum - reserve * 10);
new_number.dot_before++;
pa = pa->prev;
} // 检测是否最后还有一位
if (reserve)
{
new_number.InsertNodeAtBegin(reserve);
new_number.dot_before++;
reserve = 0;
} new_number.ClearZeros(); return new_number;
} BigNumber operator*(const BigNumber& Scale)
{
return Mul(Scale);
} BigNumber& operator+=(const BigNumber Addend)
{
BigNumber &a = *this;
const BigNumber &b = Addend; BigNumberNode *pa = a.tail, *pb = b.tail;
int sum = 0, remain = 0, odd = 0;
int reserve = 0; // 小数点之后
if(a.dot_later > b.dot_later)
{
remain = b.dot_later;
odd = a.dot_later - b.dot_later; for (int i = 0; i < odd; i++)
{
pa = pa->prev;
}
}
else
{
remain = a.dot_later;
odd = b.dot_later - a.dot_later; char *odd_n = new char[odd];
for (int i = 1; i <= odd; i++)
{
odd_n[odd - i] = pb->n;
pb = pb->prev;
} for (int i = 0; i < odd; i++)
{
a.InsetNodeAtEnd(odd_n[i]);
a.dot_later++;
}
delete odd_n;
} for (int i = 0; i < remain; i++)
{
sum = pa->n + pb->n + reserve;
reserve = sum / 10;
pa->n = sum - reserve * 10; pa = pa->prev;
pb = pb->prev;
} // 小数点之前
if(a.dot_before > b.dot_before)
{
remain = b.dot_before;
odd = a.dot_before - b.dot_before; for (int i = 0; i < remain; i++)
{
sum = pa->n + pb->n + reserve;
reserve = sum / 10;
pa->n = sum - reserve * 10; pa = pa->prev;
pb = pb->prev;
} for (int i = 0; i < odd; i++)
{
sum = pa->n + reserve;
reserve = sum / 10;
pa->n = sum - reserve * 10; pa = pa->prev;
}
}
else
{
remain = a.dot_before;
odd = b.dot_before - a.dot_before; for (int i = 0; i < remain; i++)
{
sum = pa->n + pb->n + reserve;
reserve = sum / 10;
pa->n = sum - reserve * 10; pa = pa->prev;
pb = pb->prev;
} for (int i = 0; i < odd; i++)
{
sum = pb->n + reserve;
reserve = sum / 10;
a.InsertNodeAtBegin(sum - reserve * 10);
a.dot_before++;
pb = pb->prev;
} } // 检测是否最后还有一位
if (reserve)
{
a.InsertNodeAtBegin(reserve);
a.dot_before++;
} a.ClearZeros(); return *this;
} void _Print()
{
if(dot_before == 0 && dot_later == 0)
{
putchar('0');
}
else if (dot_later == 0)
{
BigNumberNode *p = head; while(p)
{
putchar(p->n + '0');
p = p->next;
}
}
else
{
BigNumberNode *p = head; for(int i = 0; i < dot_before; i++)
{
putchar(p->n + '0');
p = p->next;
} putchar('.'); while(p)
{
putchar(p->n + '0');
p = p->next;
}
}
} void PrintString()
{
if(dot_before == 0 && dot_later == 0)
{
putchar('0');
}
else if (dot_later == 0)
{
char *temp = new char[dot_before + dot_later + 2], *ptemp = temp;
BigNumberNode *p = head; while(p)
{
*ptemp = p->n + '0';
ptemp++;
p = p->next;
}
*ptemp = 0; std::cout<<temp<<'\n';
delete[] temp;
}
else
{
char *temp = new char[dot_before + dot_later + 2], *ptemp = temp;
BigNumberNode *p = head; for(int i = 0; i < dot_before; i++)
{
*ptemp = p->n + '0';
ptemp++;
p = p->next;
} *ptemp = '.';
ptemp++; while(p)
{
*ptemp = p->n + '0';
ptemp++;
p = p->next;
}
*ptemp = 0; std::cout<<temp<<'\n';
delete[] temp;
}
} void Free()
{
BigNumberNode *p = head, *temp; while(p)
{
temp = p;
p = p->next;
delete temp;
} head = NULL;
tail = NULL;
dot_before = 0;
dot_later = 0;
} // 从字符串建立数据,未加入错误检测
void AdaptFormString(const char *Str)
{
Free(); const char *pc = Str; // 小数点之前
while(*pc)
{
if(*pc != '.') // 0 ~ 9
{
InsetNodeAtEnd(*pc - '0');
dot_before++;
pc++;
}
else // 小数点之后
{
pc++;
while(*pc)
{
InsetNodeAtEnd(*pc - '0');
dot_later++;
pc++;
}
break;
}
} ClearZeros();
} }; // 自顶向下的动态规划
BigNumber* BigNumberPowUP(BigNumber* result[], int pow)
{
if( result[pow] )
{
return result[pow];
}
else
{
int left = pow / 2;
int right = pow - left; result[left] = BigNumberPowUP(result, left);
result[right] = BigNumberPowUP(result, right); result[pow] = new BigNumber((*result[left]) * (*result[right])); return result[pow];
}
} BigNumber BigNumberPow(const BigNumber& R, int pow)
{
BigNumber *result[26] = {0}; result[1] = new BigNumber(R); BigNumberPowUP(result, pow); BigNumber Result = *result[pow]; for (int i = 0; i <= pow; i++)
{
delete result[i];
} return Result;
} int main(void)
{
char Rstr[10];
int n; while(scanf("%s%d", Rstr, &n) != EOF)
{
BigNumber result = BigNumberPow(Rstr, n);
result.PrintString();
} return 0;
}

[acm 1001] c++ 大数加法 乘法 幂的更多相关文章

  1. vector、string实现大数加法乘法

    理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...

  2. sdut2613(This is an A+B Problem)大数加法(乘法)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  3. java实现大数加法、乘法(BigDecimal)

    之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...

  4. Hat's Fibonacci(大数加法+直接暴力)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 hdu1250: Hat's Fibonacci Time Limit: 2000/1000 M ...

  5. 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)

    题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...

  6. 【大数加法】POJ-1503、NYOJ-103

    1503:Integer Inquiry 总时间限制:  1000ms 内存限制:  65536kB 描述 One of the first users of BIT's new supercompu ...

  7. NI笔试——大数加法

    NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...

  8. A + B Problem II(大数加法)

    http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/Other ...

  9. 大数加法~HDU 1002 A + B Problem II

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...

随机推荐

  1. google浏览器安装不上的绝望经历

    手贱把google的一些文件删除了,整个浏览器都没法打开 决定重装下,但是连卸载的功能都无法打开了 于是决定上网重新下载了个安装包,发现安装包都打不来 很绝望,查了很多资料 很多人说要删除注册表的东西 ...

  2. python 面向过程和面向对象比较

    面向过程 VS 面向对象 面向过程的程序设计:核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. 优点是:复杂度 ...

  3. Shiro如何使用Ehcache实现Session共享

    最近项目中用到的Session共享场景:两个独立应用,希望实现DB层共享用户,而且用户只需要登录一次. 分析:这种场合,不适用单点,因为用户数据并不需要单独在第三方应用管理,而且添加单点也会增加整个系 ...

  4. orange1

  5. 用matplotlib绘制图像

    实例一: import numpy as np import matplotlib.pyplot as plt x=np.linspace(0,6,100) y=np.cos(2*np.pi*x)*n ...

  6. zabbix4.2 安装

    官网https://www.zabbix.com/cn/download a. 安装 数据库rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86 ...

  7. 用hashmap实现redis有什么问题

    1.容量问题 hashmap是有最大容量的 2.时效问题 redis可以持久化,也可以定时时间 hashmap不可以持久化 3.线程并发问题 hashmap不是线程安全的(并且:多线程同时调用hash ...

  8. TortoiseGit学习系列之TortoiseGit是什么?

    写该文章目的 肯定有人会遇到这样的问题: 最近换了一份新工作,新公司的源码管理都是使用GIT,习惯了之前的TFS和SVN进行项目源码管理和团队开发,第一次使用GIT进行团队开发和源码管理,颇有一些不习 ...

  9. unity对敏感词库处理的记录

    string txt = Resources.Load<TextAsset>("minganci").ToString(); //string txt = " ...

  10. Python机器学习库sklearn的安装

    Python机器学习库sklearn的安装 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上能够为用户提供各种机器学习算法接口 ...