Netease mutual entertainment: pen questions

Pupils Xiaoming just learned three-digit multiplication calculation using a vertical, for skillful use of the vertical, the teacher arranged for him N math problems. But this much N, Xiao Ming feel very unhappy: Why can not I use the computer to calculate the results of it? But the job requires vertical process to write out, can not write directly a result fool teacher.

No way, Xiao Ming homework when difficulties began his music ,, he intends to do most of the figures calculated today wrote (1,2,3,4,5,6,78,9, Xiao Ming did not like the number 0 ) which is, as today's lucky number, but found that the number of digital Xiao Ming is a very troublesome thing, he thought why can not I write a program to help me calculate this lucky number it? Thus Xiaoming moving hands. But when he finished identifying digital image recognition program found that it was late, but the job is only half written, so he had to write multiplication vertical. But seeing the 12 o'clock bell is about to ring, Xiao Ming is very much like to know what today is a lucky number, so I want you to help him calculate lucky numbers, Xiao Ming for the vertical multiplication, there will be explained below in more detail definition.

Enter a description:

Each input data comprising a test point

Conduct a first positive integer N (0 <N <10000), represents the number of teacher assigned topic

Next N lines of two non-negative integers a, b (0 <a, b <1000), this indicates that the need to use vertical Bob computing a * b

Output Description:

N + 1 output lines, each line comprising a first N rows separated by spaces 9 numbers, the i-th row indicates the number of digits of input i need to write questions are how much. The first number is the number of row number 1 written diverted to the second number is the number of write number 2, and so once

Example 1:

3

123 456

15 20

20 15

Output:

2 2 2 2 3 3 1 3 1

1 1 2 0 1 0 0 0 0 

2 2 1 0 1 0 0 0 0

1

Description:

To 123 * 456, as shown in FIG vertical multiplication:

              

 

For the calculation order of the two numbers, Bob will be written in strict accordance with a first write B, does not point to the order

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 网易游戏
 */
public class Problem11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        List<int[]> result = new ArrayList<>();
        List<Integer> params = new ArrayList<>(total*2);
        for (int i = 0; i < total; i++) {
            params.add(sc.nextInt());
            params.add(sc.nextInt());
        }
        result = numList(params);
        for (int i = 0; i < result.size() ; i++) {
            for (int j = 0; j < result.get(i).length; j++) {
                System.out.print(result.get(i)[j] + " ");
            }
            System.out.println();
        }
        int max = 0;
        int n = 1;
        for (int j = 0; j < total; j++) {
            max += result.get(j)[0];
        }
        for (int i = 1; i < 9; i++) {
            int another = 0;
            for (int j = 0; j < total; j++) {
                 another += result.get(j)[i];
            }
            if (another > max) {
                n = i + 1;
            }
        }
        System.out.println(n);
    }
    // 返回两个数乘积后的数字的出现次数集合
    public  static List<int[]> numList(List<Integer> params) {
        List<int[]> temp = new ArrayList<>();
        for (int i = 0; i < params.size()-1; i += 2) {
            int[] rowResult = new int[9];
            for (int j = 0; j < rowResult.length; j++) {
                rowResult[j] = 0;
            }
            int multiFirst = params.get(i);
            int multiSec = params.get(i+1);
            int m = 0;
            int result = 0;
            while ((multiSec/10) != 0) {
                int i1 = result(multiFirst, multiSec, rowResult);
                if (i1 == -1) {
                    multiSec = multiSec/10;
                    m ++;
                    continue;
                }
                if (m==0) {
                    result += i1;
                } else {
                    result += i1 * Math.pow(10,m);
                }
                multiSec = multiSec/10;
                m++;
            }
            int i1 = result(multiFirst, multiSec, rowResult);
            result += i1 * Math.pow(10,m);
            xunhuan(result, rowResult);
            xunhuan(multiFirst,rowResult);
            temp.add(rowResult);
        }
        return temp;
    }

    public static int result (int multiFirst, int multiSec, int[] rowResult) {
        int yushu = multiSec % 10;
        if (yushu == 0){
            return -1;
        }
        rowResult[yushu-1]++;
        int i1 = yushu * multiFirst;
        xunhuan(i1, rowResult);
        return i1;
    }

   //计算整数中单个数字出现的次数
    public static void xunhuan (int i1, int[] rowResult) {
        String i2 = String.valueOf(i1);
        char[] array = i2.toCharArray();
        for (int j = 0; j < array.length; j++) {
            if (array[j] - '0' == 0) {
                continue;
            } else {
                int index = array[j]-'0';
                rowResult[index - 1]++;
            }

        }
    }
}

 

Guess you like

Origin www.cnblogs.com/yanglanwan/p/11600723.html