1503:Integer Inquiry

总时间限制: 
1000ms

内存限制: 
65536kB
描述
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 
输入
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself. 

输出
Your program should output the sum of the VeryLongIntegers given in the input.
样例输入
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
样例输出
370370367037037036703703703670
  • 先说说为啥要用算法实现大数加法:

    我们来看看数据:

    bool型为布尔型,占1个字节,取值0或1。

    BOOL型为int型,一般认为占4个字节,取值TRUE/FALSE/ERROR。

    sbyte型为有符号8位整数,占1个字节,取值范围在128~127之间。

    bytet型为无符号16位整数,占2个字节,取值范围在0~255之间。

    short型为有符号16位整数,占2个字节,取值范围在-32,768~32,767之间。

    ushort型为无符号16位整数,占2个字节,取值范围在0~65,535之间。

    int型为有符号32位整数,占4个字节,取值范围在-2,147,483,648~2,147,483,647之间。

    uint型为无符号32位整数,占4个字节,取值范围在0~4,294,967,295之间。

    long型为64位有符号整数,占8个字节,取值范围在9,223,372,036,854,775,808~9,223,372,036,854,775,807之间。

    ulong型为64位无符号整数,占8个字节,取值范围在0~18,446,744,073,709,551,615之间。

    float型为32位单精度实数,占4个字节,取值范围3.4E+10的负38次方~3.4E+10的38次方之间。

    double型为64位实数,占8个字节,取值范围1.7E+10的负308次方~1.7E+10的正308次方。

 int num1[MAX],num2[MAX];
void Add(char a[],char b[]){
int i,j,len1,len2,max;
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
len1 = strlen(a);
len2 = strlen(b);
//获得最长数组的长度
//两数相加最多不会超过最大数的位数+1,减少循环次数
max = len1 > len2 ? len1 : len2;
//将两字符串数组转换为数字数组
for(i = len1 - ,j = ;i >= ;i--){
num1[j++] = a[i] - '';
}
for(i = len2 - ,j = ;i >= ;i--){
num2[j++] = b[i] - '';
}
//模拟进位运算
for(i = ;i <= max;i++){
num1[i] += num2[i];
if(num1[i] > ){
num1[i] -= ;
num1[i + ] ++;
}
}
//输出
int ac = ;
for(i = max;i >= ;i--){//输出排除0
if(num1[i] != )
ac = ;
if(ac){
printf("%d",num1[i]);
}
}
}
  • 感觉学会了,于是乎,就找了题,继续练练手,就是POJ-1503.
  • 然后就陷入了深坑中,首先就是那个输入,竟然一直想要用二维数组,真是。。。只能怪练得还太少。
  • 解题过程中,脑中想的方法过于复杂,那肯定就是错误的,一定要跳出这个思维,想别的思路解决。
 //POJ-1503-1
#include <cstdio>
#include <cstring>
const int MAXN = ;
int main()
{
char s[MAXN];
int i, j;
int m;
scanf("%d",&m);
while(m--){
int sum[MAXN] = {};
while(gets(s))
{
int len = strlen(s);
if(s[] == '' && len == )
break;
//sum[]保存所有组的各位的和
for(i = , j = len-; j >= ; i--, j--)
{
sum[i] += s[j]-'';
}
}
//将sum[]每个元素,进行进位
for(i = ; i > ; i--)
{
sum[i-] += sum[i] / ;
sum[i] %= ;
}
//排除和为0的情况
for(i = ; sum[i] == && i < ; i++)
{
if(i == )//意味着全为零
{
printf("0\n");
}
}
for( ; i < ; i++)
{
printf("%d",sum[i]);
}
printf("\n\n");
}
return ;
}

A+B Problem II

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

A,B must be positive.

 
输入
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
输出
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation.
样例输入
2
1 2
112233445566778899 998877665544332211
样例输出
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
 //NYOJ-103
#include<cstdio>
#include<cstring>
const int MAX = ;
char str1[MAX],str2[MAX];
int num1[MAX],num2[MAX];
void Add(char a[],char b[]){
int i,j,len1,len2,max;
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
len1 = strlen(a);
len2 = strlen(b);
//获得最长数组的长度
//两数相加最多不会超过最大数的位数+1,减少循环次数
max = len1 > len2 ? len1 : len2;
//将两字符串数组转换为数字数组
for(i = len1 - ,j = ;i >= ;i--){
num1[j++] = a[i] - '';
}
for(i = len2 - ,j = ;i >= ;i--){
num2[j++] = b[i] - '';
}
//模拟进位运算
for(i = ;i <= max;i++){
num1[i] += num2[i];
if(num1[i] > ){
num1[i] -= ;
num1[i + ] ++;
}
}
//输出
int ac = ;
for(i = max;i >= ;i--){//输出排除0
if(num1[i] != )
ac = ;
if(ac){
printf("%d",num1[i]);
}
}
}
int main(){
int n,i = ;
scanf("%d",&n);
while(n--){
scanf("%s %s",str1,str2);
printf("Case %d:\n%s + %s = ",i,str1,str2);
if(str1[] == '' && str2[] == ''){
printf("0\n");
}else{
Add(str1,str2);
printf("\n");
}
i++;
}
return ;
}
  • 减法、乘法、除法明天继续

【大数加法】POJ-1503、NYOJ-103的更多相关文章

  1. 51nod 1005 大数加法

    #include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...

  2. c#大数加法

    在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...

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

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

  4. Leetcode 67 Add Binary 大数加法+字符串处理

    题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...

  5. nyoj 103 A + B problem II

    点击打开链接 A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...

  6. HDU1002大数加法

    大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...

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

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

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

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

  9. A + B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

随机推荐

  1. 使用函数的递归调用来解决Hanoi(汉诺)塔问题。

    #include<stdio.h> void hanoi(int n, char x, char y, char z); void move(char x, char y); int ti ...

  2. firefox无法安装未通过验证的扩展

    firefox43版本无法安装未验证附加组件,利用以下方法: 1.进入firefox  about:config页面中 2.搜索xpinstall.signatures.required,将值改为fa ...

  3. 2016,除了 DevOps,企业还应该知道 CMDB!

    CMDB 是 Configuration Management Database(配置管理数据库)的简称,CMDB 存储与管理企业 IT 架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧密 ...

  4. POJ 1731

    #include<iostream> #include<string> #include<algorithm> using namespace std; int m ...

  5. Struts2 本是非单例的,与Spring集成就默认为单例

    1.Struts2本身action类是多例,此设计的原因在于本身action担任了数据载体,如果做成单例,则会便多用户数据受到影响: 2.当Struts2 与 spring整合时,Struts2的Ac ...

  6. Python新式类和旧式类的区别

    新式类是为了统一**而在2.2中开始引入的. 代码讲解 上面的例子比较明白的说明了问题. B是定义的新式类.那么输入b的时候,不论是type(b),还是b.__class__都是输出的<clas ...

  7. ubuntu 13.10 64bit装BeyondCompare

    1. Beyond Compare官网下载amd-64位的,安装失败,依赖于ia32-libs,但是这个文件已经不在源里了: 2. 官网下载tar.gz源码包,解压安装失败: 3. 直接装32位的,可 ...

  8. lintcode 中等题:Palindrome Linked List 回文链表

    题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...

  9. asp.net中当服务器出错时显示指定的错误页面

    http://blog.csdn.net/helloxiaoyu/article/details/2943537 此篇文章描述了当异常再ASP.NET中发生时怎样使用C#.NET代码去拦截和相应异常. ...

  10. WP之Sql Server CE数据库

    如何在WP8中进行数据存储,你首先想到应该是独立存储,但是独立存储似乎存储文件更方便,如果我们希望像处理对象的形式,该怎么办呢,答案就是Sql Server CE. Sql Server CE并不是新 ...