之前会了大数加法,感觉乘法会难很多就一直没管,今天算是学会了,顺便也理解了一下大数除法。

大数乘法代码如下:

主要是模拟手算,然后不进位,最后在进位。

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
void dscf(char *num1,char *num2)
{
  int res[400];
  int n1[210];
  int n2[210];
  memset(res, 0, sizeof(res));
  memset(n1, 0, sizeof(n1));
  memset(n2, 0, sizeof(n2));
  int len1 = strlen(num1);  //第一个数的长度
  int len2 = strlen(num2);  //第二个数的长度
  for (int i = len1-1; i >= 0; i--)
  {
    n1[len1 - i - 1] = num1[i] - '0';
  }
  for (int i = len2 - 1; i >= 0; i--)
  {
    n2[len2 - i - 1] = num2[i] - '0';
  }
  for (int i = 0; i < len1; i++)
  {
    for (int j = 0; j < len2; j++)
    {
      res[i + j] += n1[i] * n2[j];    //这个是重点
    } 
  }
  //进位
   for (int i = 0; i < len1 + len2; i++)
  {
    while (res[i] > 9)
    {
      res[i] -= 10;
      res[i + 1]++;
    }
  }
  int tlen;
  if (res[len1 + len2 - 1] != 0)
  {
    tlen = len1 + len2 - 1;
  }
  else
  {
    tlen = len1 + len2 - 2;
  }
  for (int i = 0; i <= tlen / 2; i++)
  {
    int t;
    t = res[i];
    res[i] = res[tlen - i];
    res[tlen - i] = t;
  }
  for (int i = 0; i <= tlen; i++)
  {
    cout << res[i];
  }
  cout << endl;
}
int main()
{
  char num1[200];
  char num2[200];
  memset(num1, 0, sizeof(num1));
  memset(num2, 0, sizeof(num2));
  cin >> num1 >> num2;
  dscf(num1, num2);
  system("pause");
  return 0;
}

大整数乘法

Description

输入两个正整数M,N(0<M,N<10^100),输出二者的乘积。

Input

100位以内的正整数M,N

Output

M,N的乘积

Sample Input 1
1000000000 2000000000
Sample Output 1
2000000000000000000
Sample Input 2
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
Sample Output 2
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
Hint
模拟笔算原理注意:如果按字符输出字符数组,不要输出多余的'\0'

主要是这一题,用了乘法也用了除法,AC代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
void doit(char *num1)
{
  int res[400];
  int n1[210];
  int n2[210];
  memset(res, 0, sizeof(res));
  memset(n1, 0, sizeof(n1));
  memset(n2, 0, sizeof(n2));
  int len1 = strlen(num1);  //第一个数的长度
  for (int i = len1 - 1; i >= 0; i--)
  {
    n1[len1 - i - 1] = num1[i] - '0';
  }
  memcpy(n2, n1, sizeof(n2));
  n2[0]++;
  //进位
  if (n2[0] > 9)
  {
    for (int i = 0; i < len1; i++)
    {
      while (res[i] > 9)
      {
        res[i] -= 10;
        res[i + 1]++;
      }
    }
  }
  int len2;
  if (n2[len1+1] != 0)
  {
    len2 = len1+1;
  }
  else
  {
    len2 = len1;
  }
  for (int i = 0; i < len1; i++)
  {
    for (int j = 0; j < len2; j++)
    {
      res[i + j] += n1[i] * n2[j];
    }
  }
  //进位
  for (int i = 0; i < len1 + len2; i++)
  {
    while (res[i] > 9)
    {
      res[i] -= 10;
      res[i + 1]++;
    }
  }
  int tlen;
  if (res[len1 + len2 - 1] != 0)
  {
    tlen = len1 + len2 - 1;
  }
  else
  {
    tlen = len1 + len2 - 2;
  }
  for (int i = 0; i <= tlen / 2; i++)
  {
    int t;
    t = res[i];
    res[i] = res[tlen - i];
    res[tlen - i] = t;
  }
  int jw[200];
  memset(jw, 0, sizeof(jw));
  bool flag = true;
  for (int i = 0; i <= tlen; i++)
  {
    if (res[i] == 0)
    {
      continue;
    }
    flag = true;
    while (res[i] >= 2)
    {
      res[i] -= 2;
      jw[i]++;
    }
    if (res[i] == 1)
    {
      int tres = 10 + res[i + 1];
      res[i]--;
      if (res[i + 1] % 2 == 0)
      {
        res[i + 1] = 0;
      }
      else
      {
        res[i + 1] = 1;
      }
      jw[i + 1] = tres / 2;
    }
  }
  flag = true;
  for (int i = 0; i <= tlen; i++)
  {
    if (flag && jw[i] == 0)
    {
      continue;
    }
    flag = false;
    cout << jw[i];
  }
  cout << endl;
}
int main()
{
  char num[200];;
  memset(num, 0, sizeof(num));
  while (cin >> num)
  {
    doit(num);
  }
  return 0;
}
最后修改:2019 年 03 月 01 日 12 : 34 AM
如果觉得我的文章对你有用,请随意赞赏