题目:http://acm.hdu.edu.cn/showproblem.php?pid=1002

复习一下大数

模板:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include <map>
#include <string>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <fstream>
#include <list>
using namespace std; const int MAXL = ;
struct BigNum
{
int num[MAXL];
int len;
BigNum()
{
memset(num,,sizeof(num));
}
}; //高精度加法
BigNum Add(BigNum &a, BigNum &b)
{
BigNum c;
int i, len;
len = (a.len > b.len) ? a.len : b.len;
memset(c.num, , sizeof(c.num));
for(i = ; i < len; i++)
{
c.num[i] += (a.num[i]+b.num[i]);
if(c.num[i] >= )
{
c.num[i+]++;
c.num[i] -= ;
}
}
if(c.num[len])
len++;
c.len = len;
return c;
}
void print(BigNum &a) //输出大数
{
int i;
for(i = a.len-; i >= ; i--)
printf("%d", a.num[i]);
printf("\n");
} void Init(BigNum &a, char *s, int &tag) //将字符串转化为大数
{
int i = , j = strlen(s);
if(s[] == '-')
{
j--;
i++;
tag *= -;
}
a.len = j;
for(; s[i] != '\0'; i++, j--)
a.num[j-] = s[i]-'';
} int main(void)
{
//freopen("in.txt","r",stdin);
int T;
char s1[MAXL], s2[MAXL];
scanf("%d",&T);
for(int t=;t<=T;t++)
{
BigNum a,b,c;
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
scanf("%s %s",s1,s2);
int tag=;
Init(a,s1,tag);
Init(b,s2,tag);
c=Add(a,b);
if(t!=)
printf("\n");
printf("Case %d:\n",t);
printf("%s + %s = ",s1,s2);
print(c);
}
return ;
}

普通:

 #include<iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
const int M=;
void xsone(int a[],int b[],int lenz)
{
for(int i=;i<lenz;i++)
a[i]=a[i]+b[i];
int temp=;
for(int i=;i<lenz+;i++)
{
a[i]+=temp;
temp=a[i]/;
a[i]%=;
}
int i;
for(i=lenz+;i>=;i--)
if(a[i]!=)
break;
for(;i>=;i--)
printf("%d",a[i]);
printf("\n");
}
int main()
{
//freopen("in.txt","r",stdin);
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
char sa[M],sb[M];
memset(sa,,sizeof(sa));
memset(sb,,sizeof(sb));
scanf("%s %s",sa,sb);
int lena = strlen(sa);
int lenb = strlen(sb);
int lenz = lena>lenb ?lena :lenb; int a[M],b[M];
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int j;
for(j=;j<lena;j++)
a[lena--j]=sa[j]-'';
for(j=;j<lenb;j++)
b[lenb--j]=sb[j]-'';
printf("Case %d:\n",i);
printf("%s + %s = ",sa,sb);
xsone(a,b,lenz);
if(i!=n)
printf("\n");
}
return ;
}

hdu 1002 A+B的更多相关文章

  1. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  2. HDU 1002 A - A + B Problem II (大数问题)

    原题代号:HDU 1002 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 原题描述: Problem Description I have a ...

  3. hdu 1002.A + B Problem II 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...

  4. hdu 1002大数(Java)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. hdu 1002 Java 大数 加法

    http://acm.hdu.edu.cn/showproblem.php?pid=1002 PE   由于最后一个CASE不须要输出空行 import java.math.BigInteger; i ...

  6. HDU 1002 A + B Problem II(高精度加法(C++/Java))

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

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

  8. HDU 1002 - A + B Problem II - [高精度]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...

  9. HDU 1002 分类: ACM 2015-06-18 23:03 9人阅读 评论(0) 收藏

    昨天做的那题其实和大整数相加类似.记得当初我大一寒假就卡在这1002题上,结果之后就再也没写题... 到今天终于把大整数相加写了一遍. 不过写的很繁琐,抽时间改进一下写简洁一点. #include&l ...

随机推荐

  1. Java应用短信猫

    首先确定短信猫正常连接到主机,并安装SIM卡.先用超级终端测试短息猫能不能用.安装minicom:#sudo apt-get install minicom安装完成后,执行#sudo minicom ...

  2. 打造简单实用的Thinkphp分页样式(Bootstrap版本)

    先吐槽一下ThinkPHP3.1版的分页样式,虽然看起来也很简单大方,但是所有的页码全是使用简单的数字,之间的空隙比较小,不大容易点,还有那个“前5页”和“后5页”显得有点多余,因为点击当前显示第一页 ...

  3. 安装gitolite,并ssh公钥无密码登录

    安装gitolite,并ssh公钥无密码登录 gitolite是管理git版本库的一种方案,它将git版本库的管理信息放在了一个特殊git版本库里.gitolite与linux操作系统集成了,需要使用 ...

  4. mysql删除、修改字段默认值

    alter table表名alter column字段名drop default; (若本身存在默认值,则先删除) alter table 表名 alter column 字段名 set defaul ...

  5. Elastix 禁用SSL(https),还原为 http 访问

    1.相关配置文件目录: Apache的配置文件:httpd.conf,位于:/etc/httpd/conf/httpd.conf,配置文件中包含 Include conf.d/*.conf ,引入了  ...

  6. 【jquery】javaScript中prototype的妙用 巧妙运用prototype属性原型链创建对象

    prototype  可以有好多有优化实现方法 http://blog.csdn.net/liuqiwen0512/article/details/8089690 在 JavaScript 中,每个函 ...

  7. jquery定义表格宽度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. .NET序员的成长之路

  9. Qt版helloworld

    跟学别的编程语言一样,Qt也不例外,一开始就想写一个helloworld.初学Qt十几天,看了一点关于Qt视频的介绍和书上的基础知识,对于Qt写工程的概念有了初步的认识,就代码的形式来说,Qt Cre ...

  10. Scrum10-22

    Time:2013-10-22 Author:居玉皓 Things we have done since yesterday's meeting: 在今天的Scrum中,STORY1 开发前期准备工作 ...