大明A+B

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7545    Accepted Submission(s): 2662

Problem Description
话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。

现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。

 
Input
本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
 
Output
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
 
Sample Input
1.1 2.9
1.1111111111 2.3444323343
1 1.1
 
Sample Output
4
3.4555434454
2.1
 
Author
linle
 
Source
 
 
代码:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 405
char sa[maxn],sb[maxn];
struct node
{
int NN[],point[];
int pos,nlen;
void init()
{
memset(NN,,sizeof(NN));
memset(point,,sizeof(point));
}
};
node aa,bb;
void cal(char *str,node *p) //将大小数分解
{
int i,k,j=,temp;
p->pos=strlen(str)-;
for(i=;str[i]!='.'&&i<=p->pos;i++)
p->NN[j++]=str[i]-'';
for(k=i-,j=;k>j;k--,j++){
temp=p->NN[k];
p->NN[k]=p->NN[j];
p->NN[j]=temp;
}
if(i<=p->pos) p->nlen=i++; //NN长度点位置
else p->nlen=p->pos;
p->pos-=p->nlen ; //小数位数
for(j=;i<strlen(str);i++)
p->point[j++]=str[i]-''; //不进行逆序
}
void work(node sa ,node sb){
int i,j,cc,maxc,minc,p_len=;
if(sa.pos>sb.pos){
minc=sb.pos;
maxc=sa.pos;
}
else{
minc=sa.pos;
maxc=sb.pos;
}
cc=;
p_len=maxc-;
for(i=maxc-;i>=;i--){
sa.point[i]+=sb.point[i]+cc;
cc= sa.point[i]/;
sa.point[i]%=;
}
if(sa.nlen>sb.nlen){
maxc=sa.nlen;
minc=sb.nlen;
}
else{
maxc=sb.nlen;
minc=sa.nlen;
}
//判断小数点是否进
maxc++;
for(i=,j=;j<=maxc;j++){
sa.NN[j]+=sb.NN[i++]+cc ;
cc=sa.NN[j]/ ;
sa.NN[j]%= ;
}
while(maxc>&&sa.NN[maxc]==) maxc--;
while(maxc>=)
printf("%d",sa.NN[maxc--]);
i=;
while(sa.point[p_len]==) p_len--;
if(i<=p_len) printf(".");
for(j=i;j<=p_len;j++)
printf("%d",sa.point[j]);
printf("\n");
}
int main()
{
while(scanf("%s%s",sa,sb)!=EOF)
{
aa.init();
bb.init();
cal(sa,&aa);
cal(sb,&bb);
work(aa,bb);
}
return ;
}
 

HDUOJ-------1753大明A+B(大数之小数加法)的更多相关文章

  1. HDU 1753 大明A+B (大正小数加法、字符串处理)

    大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. Codeforces Round #260 (Div. 2) A B C 水 找规律(大数对小数取模) dp

    A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. 【hdoj_1753】大明A+B(大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1753 本题要求是,进行多位的小数加法,由于位数很多,所以不能用double类型存储,可以用字符串存储,然后 ...

  4. hdu 1753 大明A+B(高精度小数加法)

    //深刻认识到自己的粗心,为此浪费了一天.. Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是 ...

  5. [HDOJ] 1753.大明A+B (大数加法)

    Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是那个只会做100以内加法的那个"小明 ...

  6. hdu 1753 大明A+B(大数)

    题意:小数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<string.h> using na ...

  7. HDOJ 1753 大明A+B

    JAVA大数.... 大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdoj 1753 大明A+B 高精度/java

    大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. HDU 1753 大明A+B(字符串模拟,简单题)

    简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...

随机推荐

  1. 如何在Jenkins中使用环境变量

    以BUILD_NUMBER为例, 1.在windows batch命令中使用此环境变量,使用%BUILD_NUMBER%即可 2.在Linux shell命令中使用此环境变量,使用${BUILD_NU ...

  2. 混沌数学之Standard模型

    相关软件混沌数学之离散点集图形DEMO 相关代码: class StandardEquation : public DiscreteEquation { public: StandardEquatio ...

  3. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  4. Git 忽略规则 .gitignore文件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. hdu 4491 Windmill Animation

    A windmill animation works as follows: A two-dimensional set of points, no three of which lie on a l ...

  6. [Backbone] Working with forms

    Our first step is to add a template to the AppointmentForm below. Have the template produce a form w ...

  7. (笔试题)删除K位数字

    题目: 现有一个 n 位数,你需要删除其中的 k 位,请问如何删除才能使得剩下的数最大? 比如当数为 2319274, k=1 时,删去 2 变成 319274 后是可能的最大值. 思路: 1.贪心算 ...

  8. Your Customers Do Not Mean What They Say

    Your Customers Do Not Mean What They Say Nate Jackson I'VE NEVER MET A CUSTOMER YET that wasn't all ...

  9. Android 四大组件之 Service(一)

    Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互. 1.Service的继承关系: java.lang.Object → andr ...

  10. Hibernate(十四)缓存

    一.什么是缓存 缓存是介于应用程序和永久必数据存储源之间,目的是为了降低应用程序直接读写永久必数据存储源的频率,从而提高运行性能 缓存通常是在内存中的如: Office中的Word.excel Hib ...