網易互娛:筆試題


小學生小明剛剛學會了使用豎式計算三位數乘法,為了更熟練的使用豎式,老師給他布置了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]++; } } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM