【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 ...
随机推荐
- 取caml查询结果的前多少行
取查询结果的前多少行的方法?spQuery.RowLimit = 1; SPList list = SPContext.Current.Web.Lists[ListNames. ...
- C#读取Excel表中的数据时,为何有些行的字段内容读取不到
转载:http://bbs.csdn.net/topics/360220285 1.当某列数据中含有混合类型时,在.NET中使用Microsoft.Jet.OLEDB.4.0来读取Excel文件造成数 ...
- MVC小系列(二十一)【带扩展名的路由可能失效】
mvc3之后:如果路由带上扩展名,比如这样: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRout ...
- 销毁session
session运行在服务器是单用户,每个session都有一个唯一的sessionid 用法:session.setAttribute("userName", "张三丰& ...
- 2014年12月23日00:42:54——PS4
http://tieba.baidu.com/p/3415598015?see_lz=1&pn=1 http://tieba.baidu.com/p/3188981817 http://tie ...
- groovy --不注意的小错误(java.lang.String.positive() is applicable)
sql 语句拼接报错: No signature of method: java.lang.String.positive() is applicable for argument types: () ...
- (转)UIColor,CGColor,CIColor三者的区别和联系
最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...
- 理解Java中的字符串类型
1.Java内置对字符串的支持: 所谓的内置支持,即不用像C语言通过char指针实现字符串类型,并且Java的字符串编码是符合Unicode编码标准,这也意味着不用像C++那样通过使用string和w ...
- Linux错误码的含义
C Name Value Description EPERM 1 Operation not permitted ENOENT 2 No such file or directory ESRCH 3 ...
- 迷你版 smarty --模板引擎和解析
http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...