【HDOJ】1753 大明A+B

注意数据格式,可以是整数,并且注意输出最简化浮点数。

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 #define MAXNUM 420
  5 
  6 char a[MAXNUM], b[MAXNUM], c[MAXNUM], d[MAXNUM];
  7 int lena, lenb;
  8 int posa, posb;
  9 
 10 int addf(int bega, int begb, int *carry);
 11 int addi(int enda, int endb, int carry);
 12 void add();
 13 void output(int in, int fn);
 14 
 15 int main() {
 16 
 17     while (scanf("%s %s", a, b) != EOF) {
 18         add();
 19         memset(a, 0, sizeof(a));
 20         memset(b, 0, sizeof(b));
 21         memset(c, 0, sizeof(c));
 22         memset(d, 0, sizeof(d));
 23     }
 24 
 25     return 0;
 26 }
 27 
 28 void add() {
 29     int i, carry, in, fn;
 30 
 31     posa = posb = -1;
 32     for (i=0; i<MAXNUM; ++i) {
 33         if (a[i] == '\0')
 34             break;
 35         else if (a[i] == '.')
 36             posa = i;
 37         else
 38             a[i] -= '0';
 39     }
 40     lena = i;
 41     if (posa == -1)
 42         posa = lena;
 43 
 44     for (i=0; i<MAXNUM; ++i) {
 45         if (b[i] == '\0')
 46             break;
 47         else if (b[i] == '.')
 48             posb = i;
 49         else
 50             b[i] -= '0';
 51     }
 52     lenb = i;
 53     if (posb == -1)
 54         posb = lenb;
 55 
 56     fn = addf(posa+1, posb+1, &carry);
 57     in = addi(posa, posb, carry);
 58     output(in, fn);
 59 }
 60 
 61 void output(int in, int fn) {
 62     int i;
 63 
 64     // printf the integer part
 65     while (in>=0 && c[in]==0)
 66             --in;
 67     if (in < 0)
 68         printf("0");
 69     else {
 70         for (i=in; i>=0; --i)
 71             printf("%d", c[i]);
 72     }
 73 
 74     fflush(stdout);
 75     // printf the float part
 76     while (fn>=0 && d[fn]==0)
 77         --fn;
 78     if (fn>=0) {
 79         printf(".");
 80         for (i=0; i<=fn; ++i)
 81             printf("%d", d[i]);
 82     }
 83     printf("\n");
 84     fflush(stdout);
 85 }
 86 
 87 int addi(int enda, int endb, int carry) {
 88     int cn = 0;
 89 
 90     while (enda || endb) {
 91         c[cn] += carry;
 92         if (enda) {
 93             --enda;
 94             c[cn] += a[enda];
 95         }
 96         if (endb) {
 97             --endb;
 98             c[cn] += b[endb];
 99         }
100         carry = c[cn]/10;
101         c[cn] %= 10;
102         ++cn;
103     }
104     c[cn] += carry;
105 
106     return cn;
107 }
108 
109 int addf(int bega, int begb, int *carry) {
110     int i, len, ov = 0;
111     int flena = lena - bega;
112     int flenb = lenb - begb;
113 
114     len = (flena>flenb) ? flena : flenb;
115 
116     for (i=len-1; i>=0; --i) {
117         d[i] += ov;
118         if (bega+i < lena)
119             d[i] += a[bega+i];
120         if (begb+i < lenb)
121             d[i] += b[begb+i];
122         ov = d[i]/10;
123         d[i] %= 10;
124     }
125 
126     *carry = ov;
127 
128     return len;
129 }

 

posted on 2014-04-27 23:17  Bombe  阅读(209)  评论(0编辑  收藏  举报

导航