高精度乘法问题,WA了两次是因为没有考虑结果为0的情况。

 Product 

The Problem

The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

  1. 12
  2. 12
  3. 2
  4. 222222222222222222222222

Sample Output

  1. 144
  2. 444444444444444444444444

AC代码:

  1. //#define LOCAL
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. const int maxn = ;
  8. char a[maxn], b[maxn];
  9. int x[maxn], y[maxn], mul[maxn * + ];
  10. void Reverse(char s[], int l);
  11.  
  12. int main(void)
  13. {
  14. #ifdef LOCAL
  15. freopen("10106in.txt", "r", stdin);
  16. #endif
  17.  
  18. while(gets(a))
  19. {
  20. gets(b);
  21. int la = strlen(a);
  22. int lb = strlen(b);
  23. memset(mul, , sizeof(mul));
  24. memset(x, , sizeof(x));
  25. memset(y, , sizeof(y));
  26. Reverse(a, la);
  27. Reverse(b, lb);
  28. int i, j;
  29. for(i = ; i < la; ++i)
  30. x[i] = a[i] - '';
  31. for(i = ; i < lb; ++i)
  32. y[i] = b[i] - '';
  33.  
  34. for(i = ; i < lb; ++i)
  35. {
  36. int s = , c = ;
  37. for(j = ; j < maxn; ++j)
  38. {
  39. s = y[i] * x[j] + c + mul[i + j];
  40. mul[i + j] = s % ;
  41. c = s / ;
  42. }
  43. }
  44.  
  45. for(i = maxn * + ; i >= ; --i)
  46. if(mul[i] != )
  47. break;
  48. if(i < )
  49. cout << ;
  50. else
  51. {
  52. for(; i >=; --i)
  53. cout << mul[i];
  54. }
  55. cout << endl;
  56. }
  57. return ;
  58. }
  59. //用来反转数组
  60. void Reverse(char s[], int l)
  61. {
  62. int i;
  63. char t;
  64. for(i = ; i < l / ; ++i)
  65. {
  66. t = s[i];
  67. s[i] = s[l - i -];
  68. s[l - i -] = t;
  69. }
  70. }

代码君

UVa 10106 Product的更多相关文章

  1. UVa 10106 Product 【大数相乘】WA

    虽然是错的代码,但是还是想贴出来,最开始WA发现是没有考虑到乘积为0的情况,后来把a*0,0*a,a*0---0(若干个0),0--0(若干个0)*a都考虑进去了:可是还是WA,实在不懂先留在这儿. ...

  2. UVA 10106 Product (大数相乘)

    Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The ...

  3. (高精度运算4.7.21)UVA 10106 Product(大数乘法)

    package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10106 ...

  4. uva 993 Product of digits (贪心 + 分解因子)

      Product of digits  For a given non-negative integer number N , find the minimal natural Q such tha ...

  5. UVA 10106 (13.08.02)

     Product  The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input T ...

  6. UVa 993: Product of digits

    这道题很简单.先将N用2,3,5,7(即10以内的素数)分解因数(需要先特殊判断N不为1),然后将可以合并的因数合并(如2*2合并成4,)这样求得的结果位数会减少,大小肯定会小一些.具体实现见代码. ...

  7. uva 10106

    尝试一下java 的大数类 import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { ...

  8. UVA 12532-Interval Product(BIT)

    题意: 给n个数字的序列,C v p 把v位上的数字置成p , S l r代表求区间[l,r]各数字相乘得数的符号,对于每个S输出所得符号(’+‘,’-‘,’0‘) 分析: 开两个数组表示区间负数的个 ...

  9. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

随机推荐

  1. ZendStudio导入一个已有的网站

    解决方法:新建'PHP Project',选择'Create project at existiong location(from existing source)',路径指向你的网站根目录.

  2. Ambient Occlusion

    一般在光照模型中,ambient light的计算方法为:A = l * m,其中l表示表面接收到的来自光源的ambient light的总量,而m表示表面接收到ambient light后,反射和吸 ...

  3. Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  4. android开发环境搭建(for 驱动开发人员)

    前言 一.android驱动的开发流程 1: 写LINUX驱动 2: 写LINUX应用测试程序 3: 写JNI接口,用来包装第二步写的应用 (要用NDK来编译) 生成一个.SO文件,相当于CE下的DL ...

  5. Fiddler手机抓包工具如何设置过滤域名?

    fiddler手机抓包工具如何设置过滤域名?如题.fiddler抓包可以完成我们移动开发者的调试测试需求.所以说抓包尤其重要,但是多余的网页请求和手机的其他链接影响我们手机开发的需求.下面我教大家怎么 ...

  6. Java:内部类

    1.内部类的定义: 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. 2.内部类的分类: Java中的内部类共分为四种: 成员内部类member inner clas ...

  7. mysql 支持中文,防止程序乱码的方法

    1. 查看你的mysql的字符设置 mysql> show variables like 'character%'; +--------------------------+---------- ...

  8. sftp的安装和使用

    http://blog.srmklive.com/2013/04/24/how-to-setup-sftp-server-ftp-over-ssh-in-ubuntu/ In my previous ...

  9. SSL简介

    注:本文基于互联网内容整合而成,非原创.参考文章参加[7.参考资料].引用时请附上原文地址. SSL(Secure Socket Layer,安全套接字层)是位于可靠的面向连接的网络层协议和应用层协议 ...

  10. 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析   1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3   2.Fragment生命周期以及与Activ ...