标题意味着小神童。加减可以计算。

只是说这个小神童的学科知识,究竟有多神,自己给自己找。

最后,因为数据是非常非常巨大的,我听说关闭50k结束了50000数字总和,可以想见他神教。

这似乎也是考试题目IQ什么!

水题,依照一般加减法做,肯定是WA了。

这里给出使用string的加减法运算。由于string是长度可增可减的,所以无论是多少位,仅仅要内存支持,那么本算法都能够支持了。也能够使用vector这些容器。只是string应该更加省点内存。

注意: POJ比較讨厌的就是不支持C++11,并且不支持string的back()和pob_back()操作。希望POJ赶快更新系统啦。

难度是有点的,只是也是算法的基础了,高手们应该都熟悉了。直接使用Java的big number的就弱了点啦。

#include <stdio.h>
#include <string>
#include <algorithm>
using std::string; const int MAX_B = 5120;
char buf[MAX_B];
int id = 0, len = 0; inline char getFromBuf()
{
if (id >= len)
{
len = fread(buf, 1, MAX_B, stdin);
id = 0;
}
return buf[id++];
} void getIntFromBuf(string &n)
{
char a = getFromBuf();
while ((a == ' ' || a == '\n') && len) a = getFromBuf(); bool sign = true;
if (a == '-' || a == '+')
{
if (a == '-') sign = false;
a = getFromBuf();
}
n.clear();
while ((a != ' ' && a != '\n') && len)//老是写&&,错成||
{
n.push_back(a);
a = getFromBuf();
}
if (sign) n.push_back('+');
else n.push_back('-');
} string operator+(string &a, string &b)
{
string c;
int N1 = (int)a.size(), N2 = (int)b.size();
int carry = 0;
for (int i = N1-1, j = N2-1; i>=0 || j>=0 || carry; i--, j--)
{
int an = i>=0? a[i]-'0' : 0;
int bn = j>=0? b[j]-'0' : 0;
int sum = an + bn + carry;
carry = sum / 10;
c.push_back(sum % 10 + '0');
}
reverse(c.begin(), c.end());
return c;
} string operator-(string &a, string &b)
{
string c;
int N1 = (int)a.size(), N2 = (int)b.size();
int carry = 0;
for (int i = N1-1, j = N2-1; i>=0 || j>=0 || carry; i--, j--)
{
int an = i>=0? a[i]-'0' : 0;
int bn = j>=0? b[j]-'0' : 0;
int sum = an - bn + carry;
if (sum < 0)
{
carry = -1;
sum += 10;
}
else carry = 0;
c.push_back(sum % 10 + '0');
}
reverse(c.begin(), c.end());
return c;
} int cmpAbsStr(string &a, string &b)
{
if (a.size() < b.size()) return -1;
else if (a.size() > b.size()) return 1;
if (a == b) return 0; for (int i = 0; i < (int)a.size(); i++)
{
if (a[i] < b[i]) return -1;
else if (a[i] > b[i]) return 1;
}
return 0;
} int main()
{
int T;
scanf("%d", &T);
getchar();
string n1, n2;
while (T--)
{
getIntFromBuf(n1);
getIntFromBuf(n2);
if (n1[n1.size()-1] == '+' && n2[n2.size()-1] == '+'
|| n1[n1.size()-1] == '-' && n2[n2.size()-1] == '-')
{
if (n1[n1.size()-1] == '-' && n2[n2.size()-1] == '-') putchar('-');
//n1.pop_back();n2.pop_back();
n1.erase(n1.size()-1); n2.erase(n2.size()-1);
string c = n1 + n2;
puts(c.c_str());
}
else
{
if (n1[n1.size()-1] == '-' && n2[n2.size()-1] == '+') n1.swap(n2);
//n1.pop_back(); n2.pop_back();
n1.erase(n1.size()-1), n2.erase(n2.size()-1); int sign = cmpAbsStr(n1, n2);
if (sign == 0) puts("0");
else if (sign == 1)
{
string c = n1 - n2;
puts(c.c_str());
}
else
{
string c = n2 - n1;
putchar('-');
puts(c.c_str());
}
}
}
return 0;
}

版权声明:笔者靖心脏。景空间地址:http://blog.csdn.net/kenden23/。只有经过作者同意转载。

POJ 2756 Autumn is a Genius 采用string大数减法的更多相关文章

  1. POJ 2756 Autumn is a Genius 大数加减法

    Description Jiajia and Wind have a very cute daughter called Autumn. She is so clever that she can d ...

  2. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  3. poj 1625 (AC自动机好模版,大数好模版)

    题目 给n个字母,构成长度为m的串,总共有n^m种.给p个字符串,问n^m种字符串中不包含(不是子串)这p个字符串的个数. 将p个不能包含的字符串建立AC自动机,每个结点用val值来标记以当前节点为后 ...

  4. java语言中Object转为String的几种形式

    在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结.常用的方法有Object.toString(),(String)要转换的对象,St ...

  5. Java中区别.toString() ,(String),valueOf()方法

    在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结.常用的方法有Object.toString(),(String)要转换的对象,St ...

  6. poj 1702 三进制问题

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3906   Accepted: 1924 Description Eva h ...

  7. Android课程---String、StringBuffer 、StringBuilder 的区别(转)

    String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全)  简要的说, String 类型和 StringBuffer 类型的主 ...

  8. String 及其数组的相关问题

    由其他类型转String一般用三种方法 方法1:采用 Object.toString()方法 请看下面的例子: Object object = getObject(); System.out.prin ...

  9. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

随机推荐

  1. The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)

    配置项目,从tomcat低版本,放到tomcat8时,正常的项目居然报错了: The method getDispatcherType() is undefined for the type Http ...

  2. 利用python 提取log 文件里的关键句子,并进行统计分析

    利用python开发了一个提取sim.log 中的各个关键步骤中的时间并进行统计的程序: #!/usr/bin/python2.6 import re,datetime file_name='/hom ...

  3. thinkphp框架的相关总结

    参考链接地址:http://gongwen.sinaapp.com/article-205.html 1. 模板中不能使用的标签 {$content} {$i} 2. If标签 如: <if c ...

  4. vmware无法链接U盘:vm-->removeable devices.

    vmware无法链接U盘:vm-->removeable devices.

  5. Cocos2D &amp; SpriteBuilder Developer Guide

    https://www.makegameswith.us/docs/#!/cocos2d/1.0/overview

  6. Objective-C Json 使用

    Objective-c json ];   for(int i  = 0;i<myProduct.count;++i) {       //NSLog(@"-------------- ...

  7. _beginThreadex创建多线程解读

    _beginThreadex创建多线程解读 一.须要的头文件支持 #include <process.h>         // for _beginthread() 须要的设置:Proj ...

  8. 每日回顾Shell —cat,tail,head

    Shell中常常会用到cat命令.可是总是不是特别清楚: cat命令的用途是连接文件或标准输入并打印. 这个命令经常使用来显示文件内容.或者将几个文件连接起来显示.或者从标准输入读取内容并显示,它常与 ...

  9. Makefile 管理工具 — Automake and Autoconf

    该project下载路径:http://files.cnblogs.com/iTsihang/hello-2.0.zip automake 參考资料:http://www.linuxforum.net ...

  10. J2SE基础:4.面向对象的特性一

    面向对象的特性 封装 继承多态 封装: 定义: 通过对象的封装,实现了模块化和信息隐藏. 通过对类的成员施以一定的訪问权限,实现了类中成员 的信息隐藏 注意点: 对象自已该做的一些事情与方法不能交与其 ...