Multiplication Dilemma(乘法技巧模拟)

本文介绍了一种将任意两数相乘的问题转化为特殊数(首位非零,其余位为零)乘法表达式的求解方法。通过将输入的两个整数分解为特殊数的加减组合,可以更直观地理解和计算它们的乘积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Multiplication operation is not always easy! For example, it is hard to calculate 27 × 20 using your mind, but it is easier to find the answer using the following methods: 30 × 20 - 3 × 20. It turns out that people can calculate the multiplication of two special numbers very easily.

A number is called special if it contains exactly one non-zero digit at the beginning (i.e. the most significant digit), followed by a non-negative number of zeros. For example, 30, 7, 5000 are special numbers, while 0, 11, 8070 are not.

In this problem, you are given two numbers a and b. Your task is to calculate the multiplication of a and b (a × b), by writing the multiplication expression as a summation or subtraction of multiplication of special numbers. Can you?
Input

The first line contains an integer T (1 ≤ T ≤ 10^4) specifying the number of test cases.

Each test case consists of a single line containing two integers a and b ( - 10^9 ≤ a, b ≤ 10^9, a ≠ 0, b ≠ 0), as described in the problem statement above.
Output

For each test case, print a single line containing the multiplication expression of a and b as a summation or subtraction of multiplication of special numbers. All special numbers must be between  - 10^9 and 10^9 (inclusive). If there are multiple solutions, print any of them. It is guaranteed that an answer always exists for the given input.

The multiplication expression must be printed in exactly one line. A single term must be printed as in which z and w are both special numbers, # represents a single space, and x represents the multiplication operation. Two consecutive terms must be printed as in which z and w are both special numbers, # represents a single space, represents the multiplication operation, and represents either the addition operation  +  or the subtraction operation  - . (Check the sample output for more clarification).
Example
Input

2
55 20
70 17

Output

60 x 20 - 5 x 20
-3 x 70 + 70 x 20

特殊数:最高位非零,其他位都是0 (1~9也算,0不算)
注意 pow函数返回值是浮点数

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char a[15],b[15];
int pa[15],pb[15];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%s%s",a+1,b+1);
		int la=strlen(a+1);
		int lb=strlen(b+1);
		int fa=(a[1]=='-'?-1:1);//记录是正数还是负数 
		int fb=(b[1]=='-'?-1:1);
		int ta=0,tb=0;
		for(int i=(fa==1?1:2);i<=la;i++)
		if(a[i]!='0') pa[++ta]=i;//记录非0数的位置 
		for(int i=(fb==1?1:2);i<=lb;i++)
		if(b[i]!='0') pb[++tb]=i;
		int f=fa*fb;//结果是正数还是负数 
		for(int i=1;i<=ta;i++){
			for(int j=1;j<=tb;j++){
				if(i==1&&j==1){
					if(f==1) printf("%.f x %.f",(a[pa[i]]-'0')*pow(10,la-pa[i]),(b[pb[j]]-'0')*pow(10,lb-pb[j]));
					else printf("-%.f x %.f",(a[pa[i]]-'0')*pow(10,la-pa[i]),(b[pb[j]]-'0')*pow(10,lb-pb[j]));
				}
				else
				    printf(" %c %.f x %.f",f==1?'+':'-',(a[pa[i]]-'0')*pow(10,la-pa[i]),(b[pb[j]]-'0')*pow(10,lb-pb[j]));
			}
		} 
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值