北大的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. Locust 基本使用

    Locust 使用Python代码来定义用户行为,用它可以模拟百万级的并发用户来访问系统. 与其他性能工具对比如下: LoadRunner 商业性能测试工具,报告完整,不支持二次开发 开发语言:C/J ...

  2. $bzoj1027-JSOI2007$ 合金 计算几何 最小环

    题面描述 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金 ...

  3. 仔细说明CLOSE_WAIT状态的原因

    CLOSE_WAIT状态!!! 服务器收到客户端FIN报文后  服务器端口回复了ack,没有回复FIN报文. 1 说明客户端后面不会再向服务器发送用户层的数据了, 2 客户端 没有回复FIN 报文嘛 ...

  4. 基于web端去除空格小工具

    读论文时,不时需要抓取PDF版的段落,可是复制到word的时候会出现很多空格,利用javascript强大的功能,几行命令实现了去除段落里的空格,实现如下: <!DOCTYPE html PUB ...

  5. [目录]hybrid app 开发实战(基于ionic,cordova,angularjs)

    序:为什么要写这个系列 第一章:hybrid app开发之技术选型 第二章:ionic,cordova,phonegap关系浅析 第三章:ionic环境搭建之windows篇 第四章:ionic环境搭 ...

  6. div+css 制作表格

    <div class="table"> <h2 class="table-caption">花名册:</h2> <di ...

  7. SpringMVC Controller层的单元测试

    Getting Ready 测试相关Maven dependency如下: <dependency> <groupId>org.springframework</grou ...

  8. Git命令学习笔记

    一.本地代码增,删,改,查,提交,找回git checkout .                //抛弃工作区所有修改git checkout -- <file>    //抛弃工作区& ...

  9. Linux常用命令语法+示例

    原文出自:https://blog.csdn.net/seesun2012 Linux常用命令:Linux查看日志命令总结:Tomcat相关:Linux配置网卡,连接外网:Linux下安装JDK:Li ...

  10. D3基础---简介和数据

    D3.js是一种数据操作类型的javascript库(也可视其为插件):结合HTML,SVG和CSS,D3可以图形化的,生动的展现数据. d3获取:http://d3js.org/ 在代码文件中引入D ...