小学生小明刚刚学会了使用竖式计算三位数乘法,为了更熟练的使用竖式,老师给他布置了N道算术题。但是这个N很多,小明感觉非常不高兴:为什么我不能用计算机去计算这些结果呢?但是作业要求把竖式过程写出来,不能直接写一个结果糊弄老师。
没办法,小明在写作业的时候开始了他的苦衷做乐,,他打算算出今天写到的最多的数字(1,2,3,4,5,6,78,9,小明不喜欢数字0)是哪一个,作为今天的幸运数字,但是小明发现数数字也是一件很麻烦的事情,他想为什么我不能写一个程序去帮我计算这个幸运数字呢?于是小明动起手来。但当他写完识别数字图像识别程序后发现已经很晚了,而作业只写到一半,所以他必须去写乘法竖式。但是眼看12点的钟声就要敲响,小明是在非常想知道今天的幸运数字是什么,于是他拜托你帮忙计算幸运数字,对于小明的竖式乘法,下方说明会有更详细的定义。
输入描述:
每个输入数据包含一个测试点
第一行为一个正整数N(0<N<10000),表示老师布置的题目数
接下来N行,每行两个非负整数a,b(0<a,b<1000),表示这一题需要小明使用竖式计算a*b
输出描述:
输出N+1行,前N行每行包括9个使用空格隔开的数字,第i行表示输入中的第i题需要写的数字个数分别是多少。该行第一个数字是数字1写的次数,改行第二个数字是数字2写的次数,一次类推
示例1:
3
123 456
15 20
20 15
输出:
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
说明:
对于123 * 456,竖式乘法如图:
对于两个数字的计算顺序,小明会严格按照先写a在写b,不会点到顺序
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]++; } } } }