大数乘法问题一般可以通过将大数转换为数组来解决。

解题思路

第1步

第2步

第3步

第4步

样例

输入1:

56 744

输出1:

800

输入2:

-10 678

输出2:

-6780

输入3:

1234567890 45678901234

输出3:

56393704713977776260

代码实现

#include<stdio.h>
#include<string.h>
#define MAX 1000 // 大数乘法
void Multiply(char* tempA, char* tempB, int* prod, int lenA, int lenB)
{
for (int i = 0; i < lenA; i++)
{
for (int j = 0; j < lenB; j++)
{
prod[i+j] += (tempA[i] - '0')*(tempB[j] - '0');
}
} for (int i = lenA + lenB - 1 - 1; i > 0; i--)
{
// 如果本位大于9,则保留本位数的个位,十位向左进位
if (prod[i] > 9)
{
prod[i-1] += prod[i] / 10;
prod[i] %= 10;
}
}
} int main()
{
char strA[MAX] = {0};
char strB[MAX] = {0}; while (scanf("%s%s", strA, strB) != EOF)
{
char tempA[MAX] = {0};
char tempB[MAX] = {0};
int prod[2*MAX] = {0}; int lenA = 0;
int lenB = 0;
int negNumA = 0;
int negNumB = 0; lenA = strlen(strA); // 计算字符串A的长度
lenB = strlen(strB); // 计算字符串A的长度 // 去掉字符串A和B的负号
if (strA[0] == '-')
{
negNumA++;
lenA--;
}
if (strB[0] == '-')
{
negNumB++;
lenB--;
} // 把去掉负号的字符串存储到temp数组中
for (int i = 0; i < lenA; i++)
{
tempA[i] = strA[i+negNumA];
}
for(int j = 0; j < lenB; j++)
{
tempB[j] = strB[j+negNumB];
} Multiply(tempA, tempB, prod, lenA, lenB); // 如果prod数组的第一个元素是0,则直接输出0,如100*0=000,输出0
if (prod[0] == 0)
{
printf("%d", prod[0]);
}
// 打印负号
if (negNumA + negNumB == 1)
{
printf("-");
} // 打印A*B的结果,N位数和M位数相乘的最大位数为N+M
for (int i = 0; i < lenA + lenB - 1; i++)
{
if (prod[0] != 0)
{
printf("%d", prod[i]);
}
} printf("\n"); }
return 0;
}

个人主页:

www.codeapes.cn

大数乘法(A * B Problem Plus)问题的更多相关文章

  1. (母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...

  3. Uva-oj Product 大数乘法

    Product Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Des ...

  4. hdu-5666 Segment(俄罗斯乘法or大数乘法取模)

    题目链接: Segment Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) P ...

  5. hdu1313 Round and Round We Go (大数乘法)

    Problem Description A cyclic number is an integer n digits in length which, when multiplied by any i ...

  6. 51nod 1027大数乘法

    题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...

  7. 用分治法实现大数乘法,加法,减法(java实现)

    大数乘法即多项式乘法问题,求A(x)与B(x)的乘积C(x),朴素解法的复杂度O(n^2),基本思想是把多项式A(x)与B(x)写成 A(x)=a*x^m+b B(x)=c*x^m+d 其中a,b,c ...

  8. HDOJ-1042 N!(大数乘法)

    http://acm.hdu.edu.cn/showproblem.php?pid=1042 题意清晰..简单明了开门见山的大数乘法.. 10000的阶乘有35000多位 数组有36000够了 # i ...

  9. 51 Nod 1027 大数乘法【Java大数乱搞】

    1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

随机推荐

  1. XSS的简单过滤和绕过

    XSS的简单过滤和绕过 程序猿用一些函数将构成xss代码的一些关键字符给过滤了.但是,道高一尺魔高一丈,虽然过滤了,还是可以尝试进行过滤绕过,以达到XSS攻击的目的. 最简单的是输入<scrip ...

  2. 【VS开发】【图像处理】 bayer, yuv, RGB转换方法

    因为我的STVxxx USB camera输出格式是bayer格式,手头上只有YUVTOOLS这个查看工具,没法验证STVxxx在开发板上是否正常工作. 网上找了很久也没找到格式转换工具,最后放弃了, ...

  3. vue2.X + HTML5 plus 拍照和调用设备相册 另附 图片转base64和压缩图片方法

    HTML5 部分 <button @click="tesCamera()" type="button" :disabled="isshStatu ...

  4. websocket服务器推送 (node+express+vue+socket)

    简介: 此项目需要懂一点node.express 功能: 1.前端用户登录,查看服务端推送的消息,用户只能在一个地方登录,也就是单点登录 2.服务端首先登录,上传需要推送的信息文本,后台读取文本后,存 ...

  5. python 并发编程 基于gevent模块实现并发的套接字通信

    之前线程池是通过操作系统切换线程,现在是程序自己控制,比操作系统切换效率要高 服务端 from gevent import monkey;monkey.patch_all() import geven ...

  6. Nob常用命令

    说明:此文件为常用的命令笔记 规则: .使用"[组名]"分组,如[linux] .使用"<标题一>"标示知识点,可用"<<二级 ...

  7. drop与truncate与delete的区别与联系

    在mysql和oracle数据库中delete与truncate都是可以用来对数据进行删除操作,但是二者又有些不同. 主要有以下几个区别: 区别一: 根据sql语言分类来说,delete属于DML语言 ...

  8. js 带有返回值的 匿名方法

    //可以给 permissionField返回'a,b,c'这样的以逗号分隔的字符串 permissionField:(function(){ var arr = []; $("input[ ...

  9. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install (default-cli) on project kircp-js-plan-resource: The packaging for this project did not assign a file to the bu

    结合网上的相关资料,要使用Lifecycle下的install 原因好像是Lifecycle下才会走Maven完整的phase.

  10. SIP协议 会话发起协议(一)

    会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协同工作,通过Internet控制多媒体通信会话. SIP - 概述 以下是有关SIP的几点注意事项 - S ...