题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1753
解题思路:
大数相加问题,但是有三种情况:2个整数,1个整数1个小数,2个小数。
这就需要我们单独处理每种情况,而且这道题需要保证没有多余的前导0和后导0,刚开始想到简单了。结果到最后搞了150行才过。悲剧。
这道题的巧妙做法是:把每种情况都转换成2个小数来搞,这样就只有1种情况了。
代码如下:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 810;
char a[N], b[N];
int c[N], d[N];
int main()
{
int flag1, flag2, len1, len2, pos;
int up, top, down;
while(scanf("%s %s", a, b) != EOF)
{
flag1 = flag2 = -1;
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
len1 = strlen(a); len2 = strlen(b);
for(int i = 0; i < len1; ++i) //整数部分、小数部分
if(a[i] == '.') flag1 = i;
for(int i = 0; i < len2; ++i) //整数部分、小数部分
if(b[i] == '.') flag2 = i;
if(flag1 == - 1 && flag2 == -1) //2整
{
for(int i = len1 - 1, k = 0; i >= 0; --i)
c[k++] = a[i] - '0';
for(int i = len2 - 1, k = 0; i >= 0; --i)
d[k++] = b[i] - '0';
for(int i = 0, up = 0; i < N; ++i)
{
c[i] = c[i] + d[i] + up;
up = c[i] / 10;
c[i] %= 10;
}
for(top = N -1; !c[top] && top >= 0; --top);
if(top < 0)
{
printf("0\n");
continue;
}
for(int i = top; i >= 0; --i)
printf("%d", c[i]);
printf("\n");
}
else if(flag1 != -1 && flag2 != -1) //2小
{
if(len1 - 1 - flag1 > len2 - 1 - flag2) //a小数部分长
{
for(int i = 0; i < len1 - len2 + flag2 - flag1; ++i)
b[len2 + i] = '0';
b[len1 + flag2 - flag1 + 1] = '\0';
len2 += len1 - len2 + flag2 - flag1;
}
else if((len1 - 1 - flag1 < len2 - 1 - flag2))
{
for(int i = 0; i < len2 - len1 + flag1 - flag2; ++i)
a[len1 + i] = '0';
a[len2 + flag1 - flag2 + 1] = '\0';
len1 += len2 - len1 + flag1 - flag2;
}
pos = 0;
for(int i = len1 - 1; i >= 0; --i)
if(a[i] != '.') pos++;
else break;
for(int i = len1 - 1, k = 0; i >= 0; --i)
if(a[i] != '.')
c[k++] = a[i] - '0';
for(int i = len2 - 1, k = 0; i >= 0; --i)
if(b[i] != '.')
d[k++] = b[i] - '0';
for(int i = 0, up = 0; i < N; ++i)
{
c[i] = c[i] + d[i] + up;
up = c[i] / 10;
c[i] = c[i] % 10;
}
for(int i = 0; i < N; ++i)
a[i] = c[i] + '0';
for(top = N - 1; a[top] == '0'; --top); //去前导0
if(top < 0)
{
printf("0\n");
continue;
}
for(down = 0; a[down] == '0' && down < pos; ++down); //去后导0
for(int i = top; i >= down; --i)
if(i == pos && down != pos) printf("%c.", a[i]);
else printf("%c", a[i]);
printf("\n");
}
else //1整、1小
{
if(flag1 == -1) //b小
{
for(int i = len1 - 1, k = 0; i >= 0; --i)
c[k++] = a[i] - '0';
for(int i = flag2 - 1, k = 0; i >= 0; --i)
d[k++] = b[i] - '0';
for(int i = 0, up = 0; i < N; ++i)
{
c[i] = c[i] + d[i] + up;
up = c[i] / 10;
c[i] %= 10;
}
for(top = N - 1; !c[top]; --top); //去前导0
if(top < 0)
{
printf("0\n");
continue;
}
for(down = len2 - 1; b[down] == '0'; --down); //去后导0
for(int i = top; i >= 0; --i)
printf("%d", c[i]);
if(b[down] != '.')
for(int i = flag2; i <= down; ++i)
printf("%c", b[i]);
printf("\n");
}
else //a小
{
for(int i = flag1 - 1, k = 0; i >= 0; --i)
c[k++] = a[i] - '0';
for(int i = len2 - 1, k = 0; i >= 0; --i)
d[k++] = b[i] - '0';
for(int i = 0, up = 0; i < N; ++i)
{
c[i] = c[i] + d[i] + up;
up = c[i] / 10;
c[i] %= 10;
}
for(top = N - 1; !c[top]; --top); //去前导0
if(top < 0)
{
printf("0\n");
continue;
}
for(down = len1 - 1; a[down] == '0'; --down); //去后导0
for(int i = top; i >= 0; --i)
printf("%d", c[i]);
if(a[down] != '.')
for(int i = flag1; i <= down; ++i)
printf("%c", a[i]);
printf("\n");
}
}
}
return 0;
}
JAVA则超级轻松水过啊!!!!!!!!150行和20行,郁闷啊。。。。
JAVA,你太强悍了。ORZ
代码如下:
import java.math.BigDecimal;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
BigDecimal a, b, c;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
a = cin.nextBigDecimal();
b = cin.nextBigDecimal();
c = a.add(b);
if(c.compareTo(BigDecimal.ZERO) == 0)
System.out.println("0");
else
System.out.println(c.stripTrailingZeros().toPlainString());
}
}
}