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 ≤ 104) specifying the number of test cases.
Each test case consists of a single line containing two integers aand b ( - 109 ≤ a, b ≤ 109, 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 - 109 and 109(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
方法一:
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
int s1,s2,s3,s4;
s1=s2=s3=s4=0;
if(a%10!=0)
{
s1=(a/10+1)*10;
s2=(10-a%10);
}
if(b%10!=0)
{
s3=(b/10+1)*10;
s4=(10-b%10);
}
if(s1==0&&s2==0&&s3==0&&s4==0)
{
printf("%d x %d \n",a,b);
}
else if(s3==0&&s4==0)
{
printf("%d x %d - %d x %d\n",s1,b,s2,b);
}
else if(s1==0&&s2==0)
{
printf("%d x %d - %d x %d\n",a,s3,a,s4);
}
else printf("%d x %d + %d x %d - %d x %d - %d x %d\n",s1,s3,s2,s4,s1,s4,s2,s3);
}
return 0;
}
本文介绍了一种利用特殊数进行快速乘法运算的方法,通过将普通乘法表达式转化为特殊数的加减乘法组合,简化了计算过程。特殊数定义为开头只有一个非零数字,后跟任意数量零的整数。文章提供了算法实现,展示了如何将任意两数相乘的问题转换为特殊数的乘法,从而提高计算效率。
1130

被折叠的 条评论
为什么被折叠?



