首页 技术 正文
技术 2022年11月15日
0 收藏 564 点赞 4,953 浏览 1480 个字

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

题意:大数相加

思路:获取小数位置,对齐小数点,两边补零,记录小数点位置,去掉小数点,相加,输出时输出小数点。

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>using namespace std;int FindDec(string &str) {
int i;
int len = str.size();
for (i = 1; i < len; i++) {
if (str[i] == '.') break;
}
return i;
}int main() {
string a, b; while (cin >> a >> b) {
//整数位位数
int len_int_a = FindDec(a), len_int_b = FindDec(b);
//小数位位数
int len_dec_a = a.size() - len_int_a - 1;
int len_dec_b = b.size() - len_int_b - 1;
//整书补零
if (len_int_a > len_int_b)
for (int i = 0; i < len_int_a - len_int_b; ++i) b = '0' + b;
else
for (int i = 0; i < len_int_b - len_int_a; ++i) a = '0' + a;
//小数补0
if (len_dec_a > len_dec_b)
for (int i = 0; i < len_dec_a - len_dec_b; ++i) b = b + '0';
else
for (int i = 0; i < len_dec_b - len_dec_a; ++i) a = a + '0';
//记录小数点位置,去除小数点
int decPos = max(len_int_a, len_int_b);
a = a.erase(decPos, 1);
b = b.erase(decPos, 1);
//计算
int car = 0;
int len = a.size();
for (int i = len - 1; i >= 0; --i) {
int x = a[i] - '0', y = b[i] - '0';
car += (x + y);
a[i] = (car % 10 + '0');
car /= 10;
}
if (car) {
a = '1' + a;
len = a.size();
decPos += 1;
}
//输出
int i, j;
for (i = 0; i < len; i++)
if (a[i] != '0') break;
for (j = i; j < decPos; j++) cout << a[j];
for (j = len - 1; j >= decPos; j--)
if (a[j] != '0') break;
if (j >= decPos) {
cout << ".";
for (i = decPos; i <= j; i++) cout << a[i];
}
cout << endl;
}
system("pause");
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857