sicily 1381(高精度)

来源:互联网 发布:局域网交流软件 编辑:程序博客网 时间:2024/06/05 15:00
 
 #include "iostream"#include "string"#include "string.h"#include "sstream"using namespace std;int main (){ int T; string str1, str2;//输入的两个整数用字符串来表示 cin >> T; while (T--) {  cin >> str1 >> str2;  int length1 = str1.size();  int length2 = str2.size();  int *a = new int [length1];  int *b = new int [length2];  for (int i = 0; i < length1 ; i++)//将字符串转化为数字,保存在数组中  {   stringstream s1;   s1 << str1[i];   s1 >> a[i];  }  for (int i = 0; i < length2 ; i++)  {   stringstream s2;   s2 << str2[i];   s2 >> b[i];  }  int *c = new int [length1 + length2];//c数组存放两整数相乘的结果  memset(c, 0, 4*(length1+length2));  int s = 0;  for (int i = length2-1; i >= 0; i--, s++)  {   int k = s;   for (int j = length1-1; j >= 0 ; j--)//根据乘法相乘的规律来得出结果   {    c[k] += a[j] * b[i];    k = k + 1;   }  }  for (int i = 0; i < (length1 + length2); i++)//判断是否需要进位!   if (c[i] >= 10)   {    c[i+1] += c[i] / 10;    c[i] = c[i] % 10;   }  int q;  for (q = (length1 + length2)-1; q >= 0  ; q--)//判断数组前面是否存在0,如果存在就去掉!  {   if (c[q] != 0)   {    for (int i = q; i >= 0; i--)             cout << c[i];    break;   }   if (q == 0) //如果数组存放的全部是0,就输出0即可!    cout << 0 ;  }  cout << endl;  } }