【HDOJ】1753 大明A+B
注意数据格式,可以是整数,并且注意输出最简化浮点数。
#include <stdio.h>
#include <string.h> #define MAXNUM 420 char a[MAXNUM], b[MAXNUM], c[MAXNUM], d[MAXNUM];
int lena, lenb;
int posa, posb; int addf(int bega, int begb, int *carry);
int addi(int enda, int endb, int carry);
void add();
void output(int in, int fn); int main() { while (scanf("%s %s", a, b) != EOF) {
add();
memset(a, , sizeof(a));
memset(b, , sizeof(b));
memset(c, , sizeof(c));
memset(d, , sizeof(d));
} return ;
} void add() {
int i, carry, in, fn; posa = posb = -;
for (i=; i<MAXNUM; ++i) {
if (a[i] == '\0')
break;
else if (a[i] == '.')
posa = i;
else
a[i] -= '';
}
lena = i;
if (posa == -)
posa = lena; for (i=; i<MAXNUM; ++i) {
if (b[i] == '\0')
break;
else if (b[i] == '.')
posb = i;
else
b[i] -= '';
}
lenb = i;
if (posb == -)
posb = lenb; fn = addf(posa+, posb+, &carry);
in = addi(posa, posb, carry);
output(in, fn);
} void output(int in, int fn) {
int i; // printf the integer part
while (in>= && c[in]==)
--in;
if (in < )
printf("");
else {
for (i=in; i>=; --i)
printf("%d", c[i]);
} fflush(stdout);
// printf the float part
while (fn>= && d[fn]==)
--fn;
if (fn>=) {
printf(".");
for (i=; i<=fn; ++i)
printf("%d", d[i]);
}
printf("\n");
fflush(stdout);
} int addi(int enda, int endb, int carry) {
int cn = ; while (enda || endb) {
c[cn] += carry;
if (enda) {
--enda;
c[cn] += a[enda];
}
if (endb) {
--endb;
c[cn] += b[endb];
}
carry = c[cn]/;
c[cn] %= ;
++cn;
}
c[cn] += carry; return cn;
} int addf(int bega, int begb, int *carry) {
int i, len, ov = ;
int flena = lena - bega;
int flenb = lenb - begb; len = (flena>flenb) ? flena : flenb; for (i=len-; i>=; --i) {
d[i] += ov;
if (bega+i < lena)
d[i] += a[bega+i];
if (begb+i < lenb)
d[i] += b[begb+i];
ov = d[i]/;
d[i] %= ;
} *carry = ov; return len;
}
【HDOJ】1753 大明A+B的更多相关文章
- hdoj 1753 大明A+B 高精度/java
大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDOJ 1753 大明A+B
JAVA大数.... 大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- [HDOJ] 1753.大明A+B (大数加法)
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是那个只会做100以内加法的那个"小明 ...
- HDOJ 1753 明朝A+B
http://acm.hdu.edu.cn/showproblem.php? pid=1753 大明A+B Time Limit: 3000/1000 MS (Java/Others) M ...
- hdu 1753 大明A+B
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1753 容易出错的事例: 0.1 0.2 1.88 22.22 1 0.01 大概出错的几个点,做久了思维根 ...
- hdu 1753 大明A+B(高精度小数加法)
//深刻认识到自己的粗心,为此浪费了一天.. Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是 ...
- HDU 1753 大明A+B (大正小数加法、字符串处理)
大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 1753 大明A+B(字符串模拟,简单题)
简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...
- hdu 1753 大明A+B(大数)
题意:小数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<string.h> using na ...
随机推荐
- (转) ASP.NET反射
原文:http://www.cnblogs.com/zizo/p/3509895.html 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何 ...
- 关于bootstrap的datepicker在meteor应用中的使用(不包含bootstrap框架)
1.安装bootstrap3-datepicker包 meteor add rajit:bootstrap3-datepicker 2.使用方法 Example In your handlebars ...
- ADB操作多台设备
1.adb devices 查看所有连接设备. 2.adb -s <设备名> shell 指定device来执行adb shell. 3.adb -s <设备名> <指令 ...
- StringToInt
public class Stringtoint { public static void stringtoint(String s){ //判断字符串是否为null和是否为空 ...
- /etc/resolv.conf文件详解
大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...
- Javascript 迭代法实现数组多条件排序
多条件排序可能有很多种思路,效率也各不相同,我的方法可能只适合自己用,毕竟目的是为了实现功能,所以采用了最笨的方法,不过效果还是很理想的,经过多次测试,6列1000行数据,平均排序时间大约是:28ms ...
- requirejs实践一 加载JavaScript文件
首先,目录如下(根目录有一个index.html文件.有一个scripts文件夹): scripts文件夹有如下的文件 以下是index.html代码 <!DOCTYPE html> &l ...
- 九度OJ 1113 二叉树
题目地址:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...
- shell中的双引号,单引号,反引号
在shell中引号分为三种:单引号,双引号和反引号. 单引号 ‘ 由单引号括起来的字符都作为普通字符出现.特殊字符用单引号括起来以后,也会失去原有意义,而只作为普通字符解释.单引号用于保持引号内所有字 ...
- Android Activity 生命周期详解
学习android开发这么久对于activity的生命周期还没有仔细思考过,所以,我大致的把这些东西整理一下,希望通过这使自己理解的更透彻点吧! 首先看一下Activity生命周期图和它的的四个阶段 ...