zoukankan      html  css  js  c++  java
  • 另类乘法

    另类乘法

    时间限制: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+" ");
    			}
    		}
    	}
    	
    	
    }
    

      

  • 相关阅读:
    关于CSS自文档的思考_css声明式语言式代码注释
    html5中contenteditable属性如果过滤标签,过滤富文本样式
    web前端工程化/构建自动化
    Python连载19-装饰器
    Java连载1-概述&常用的dos命令
    HTML连载18-id选择器与class区别&class选择器使用思路&后代选择器
    Python连载18-closure闭包解释及其注意点
    HTML连载17-id选择器&类选择器
    Python连载17-排序函数&返回函数的函数
    HTML连载16-颜色控制属性2&标签选择器
  • 原文地址:https://www.cnblogs.com/airycode/p/6591356.html
Copyright © 2011-2022 走看看