大数加法

来源:互联网 发布:淘宝垃圾短信 编辑:程序博客网 时间:2024/05/30 05:08

大数加法


基本思路,用大数组模拟手算,先将字符串a与b转换到int型数组中,通过a,b位数的较大值来确定c数组的位数,将a与b按位相加,然后再考虑满10进位的情况,满10只要在高一位的数组元素加一就行,最后再考虑高位有0的问题,高位有0,截0后输出,反之就直接输出。


基本思路有了剩下的就是码代码了

#include<cstdio>#include<cstring>int a[1005], b[1005], c[1005], m, n;//求出a,b中谁位数多,方便确定c数组的最高位位置 int getmax(char *str1, char *str2) {  m = strlen(str1), n = strlen(str2);  return m > n ? m : n;}//把字符串转换成int型数组,方便计算 int change(char *str1, char *str2, int max) {  memset(a, 0, sizeof(int));  memset(b, 0, sizeof(int));  memset(c, 0, sizeof(int));  for(int i = 0; i < max; ++i) {    a[m-i-1] = str1[i] - '0';    b[n-i-1] = str2[i] - '0';  }}//先统一加到c数组中,再考虑满10进位的问题 void plusab(int max) {  for(int i = 0; i < max; ++i) c[i] = a[i] + b[i];    for(int i = 0; i < max; ++i) {       c[i+1] += c[i] / 10;      c[i] %= 10;    }}int main() {  char str1[1005], str2[1005];  int T, t = 1;  scanf("%d", &T);  while(T--) {    scanf("%s %s", str1, str2);    int max = getmax(str1, str2);    change(str1, str2, max);    plusab(max);    printf("Case %d:\n", t++);    printf("%s + %s = ", str1, str2);    //去高位的0     if(c[max] != 0) {      for(int i = max; i >= 0; --i) printf("%d", c[i]);    }     else {      for(int i = max - 1;i >= 0; --i) printf("%d", c[i]);    }    printf("\n");   }}

同样也可以写个大数类

#include<iostream>#include<cstring>//using namespace std;using std::ostream;using std::istream;using std::cout;using std::cin;using std::endl;class BigInt {   private:     char *n;   public:     BigInt() { n = 0; };    BigInt(char *);    BigInt(const BigInt &);    ~BigInt();    BigInt& operator=(const BigInt &);    friend BigInt operator+(const BigInt &, const BigInt &);    friend ostream& operator<<(ostream &, const BigInt &);    friend istream& operator>>(istream &, BigInt &);  }; BigInt::BigInt(char *s) {  int i = 0;  while (*s == ' ') {    s++;    i++;  }  n = new char[strlen(s) + 1];  strcpy(n, s);}BigInt::BigInt(const BigInt &a) {  n = new char[strlen(a.n) + 1];  strcpy(n, a.n);}BigInt::~BigInt() {  delete[] n;}ostream &operator<<(ostream &os, const BigInt &a) {   os << a.n;   return os;}  istream& operator>>(istream &is, BigInt &a) {  char temp[1001];  is >> temp;  a = temp;  return is;}BigInt& BigInt::operator=(const BigInt &a) {  if (this != &a) {    delete[] n;    n = new char[strlen(a.n) + 1];    strcpy(n, a.n);  }  return *this;}BigInt operator+(const BigInt &a, const BigInt &b) {  int i = strlen(a.n), j = strlen(b.n), k;  if (i > j)    k = i + 1;  else    k = j + 1;  char *t = new char[k + 1];   t[k] = 0;  int carry = 0, s;  while (i > 0 && j > 0) {    s = a.n[--i] - '0' + b.n[--j] - '0' + carry;    t[--k] = ((s >= 10) ? s - 10 : s) + '0';    carry = (s >= 10) ? 1 : 0;  }  while (i > 0) {      s = a.n[--i] - '0' + carry;    t[--k] = ((s >= 10) ? s - 10 : s) + '0';    carry = (s >= 10) ? 1 : 0;  }  while (j > 0) {      s = b.n[--j] - '0' + carry;    t[--k] = ((s >= 10) ? s - 10 : s) + '0';    carry = (s >= 10) ? 1 : 0;  }  if (carry > 0)    t[--k] = '1';  else    t[--k] = ' ';   BigInt x(t);  delete[] t;  return x;}int main() {  int N, i = 0;  cin >> N;  while(N--) {    i++;    BigInt a, b;    cin >> a >> b;    cout << "Case " << i << ":\n";    cout << a << " + " << b << " = " << a + b << endl;  }}

相关acm练习 NYOJ第103题 大数相加问题

0 0
原创粉丝点击