43 - Multiply Strings

本文介绍了一种使用字符串表示的大数乘法算法实现方法,通过将字符串转换为字符数组进行逐位相乘并处理进位,最终得到两个大数相乘的结果。

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note: The numbers can be arbitrarily large and are non-negative.

Subscribe to see which companies asked this question

思路分析:

乘法原理,感觉如果用数组来计算会简单很多,用字符串必须再每次循环后处理一下高位的进位问题,比较麻烦。

1、当个位数相乘时,digit无用。

即当123 x 54 时,当 4 x 123 digit 始终为0,此后 digit位表示每一位的进位值。

/*
*/

#include "stdafx.h"
#include <iostream>
#include <algorithm>

using namespace std;

class Solution_043_MultiplyStringsArray
{
public:
	string multiply(string num1, string num2) 
	{
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		string s(num1.size() + num2.size(), '0');

		reverse(num1.begin(), num1.end());
		reverse(num2.begin(), num2.end());

		for (int i = 0; i < num1.size(); i++)
		{
			int flag = 0;
			for (int j = 0; j < num2.size(); j++)
			{
				int digit = s[i + j] - '0';
				int num = (num1[i] - '0') * (num2[j] - '0');
				int res = digit + num + flag;
				s[i + j] = (res % 10) + '0';
				flag = res / 10;//进位
			}
			int index = i + num2.size();
			while (flag)
			{
				int digit = s[index] - '0';
				int res = digit + flag;
				s[index] = (res % 10) + '0';
				flag = res / 10;
			}
		}

		while (true)
		{
			if (s[s.size() - 1] == '0' && s.size() > 1)
				s.erase(s.size() - 1, 1);
			else
				break;
		}

		reverse(s.begin(), s.end());

		return s;
	}
};


The Industrial Computer Processor Company offers very fast, special purpose processing units tailored to customer needs. Processors of the a-C-m family (such as the 1-C-2 and the 5-C-3) have an instruction set with only two different operations: A add a M multiply by m The processor receives an integer, executes a sequence of A and M operations (the program) that modifies the input, and outputs the result. For example, the 1-C-2 processor executing the program AAAM with the input 2 yields the output 10 (the computation is 2 ! 3 ! 4 ! 5 ! 10), while the 5-C-3 processor yields 51 with the same program and input (2 ! 7 ! 12 ! 17 ! 51). You are an a-C-m programmer assigned to a top secret project. This means that you have not been told the precise computation your program should perform. But you are given particular values p, q, r, and s and the following conditions: The input is guaranteed to be a number between p and q. The output must be some number between r and s. Given an a-C-m processor and the numbers p, q, r, and s, your job is to construct the shortest a-C-m program which, for every input x such that p x q, yields some output y such that r y s. If there is more than one program of minimum length, choose the one that come first lexicographically, treating each program as a string of As and Ms The input contains several test cases. Each test case is given by a line with the six integers a, m, p, q, r, and s as described above (1 a; m; p; q; r; s 10 9 , p q and r s). The last test case is followed by a line with six zeros For each test case, display its case number followed by the best program as described above. Display the word “empty” if the best program uses no operations. Display the word “impossible” if there is no program meeting the specifications. Display the program as a sequence of space-separated strings, alternating between strings of the form “nA” and strings of the form “nM”, where n > 0. Strings of the former type indicate n consecutive A operations, and strings of the latter type indicate n consecutive M operations. Follow the format of the sample output. 输入样例 1 2 2 3 10 20 1 3 2 3 22 33 3 2 2 3 4 5 5 3 2 3 2 3 0 0 0 0 0 0 输出样例 Case 1: 1A 2M Case 2: 1M 2A 1M Case 3: impossible Case 4: empty
最新发布
12-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值