另类乘法
时间限制:3000 ms | 内存限制:65535 KB
难度:1
- 描述
-
Bessie is tired of multiplying pairs of numbers the usual way, so she invented her own style of multiplication. In her style, A*B is equal to the sum of all possible pairwise products between the digits of A and B. For example, the product 123*45 is equal to 1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54. Given two integers A and B (1 ≤ A, B ≤ 1,000,000,000), determine A*B in Bessie's style of multiplication.
- 输入
- The first Line of the input is a positive integer T,indicates the number of the test cases;
In every case,the input is in one line,contains two positive interger A,B - 输出
- For every case,output the multiplication in Bessie's style.
- 样例输入
-
1 123 45
- 样例输出
-
54
package demo1; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int caseNo = input.nextInt(); for (int i = 1;i<=caseNo;i++) { handle(input.nextInt(),input.nextInt()); } } private static void handle(int num1, int num2) { List<Integer> list1 = getList(num1); List<Integer> list2 = getList(num2); int result = solution(list1,list2); System.out.println(result); } private static int solution(List<Integer> list1, List<Integer> list2) { int sum = 0; if (list1 != null && list1.size()>0) { for (int i = list1.size()-1;i>=0;i--) { int num1 = list1.get(i); if (list2 != null && list2.size()>0) { for (int j = list2.size()-1;j>=0;j--) { int num2 = list2.get(j); sum+=num1* num2; } } } } return sum; } private static List<Integer> getList(int number) { List<Integer> list = new ArrayList<Integer>(); int temp = 0; while (number != 0) { temp = number%10; list.add(temp); number = number/10; } return list; } public static void display(List<Integer>list){ if (list != null && list.size() > 0) { for (Integer i:list) { System.out.print(i+" "); } } } }