1. 程式人生 > >網易2017春招筆試真題編程題集合——分餅幹

網易2017春招筆試真題編程題集合——分餅幹

out color div nbsp -1 pre new har nco

參考:http://blog.csdn.net/wwe4023/article/details/70171648的內容

//

import java.util.*;
public class Main {
    public static void main(String[] args) {    
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        int n = Integer.parseInt(in.nextLine());
        System.out.println(combinationCount(line,n));
    }

    
public static long combinationCount(String s,int n){ int len = s.length(); long[][] dp = new long[len+1][]; for(int i = 0; i <= len; i++){ dp[i] = new long[n]; } dp[0][0] = 1; for(int i = 1; i <= len; i++){ for(int j = 0; j < n; j++){
if(s.charAt(i-1) == ‘X‘){ for(int k = 0; k <= 9; k++){ int newJ = (j*10+k) % n; dp[i][newJ] += dp[i-1][j]; } } else { int newJ = (j*10+(s.charAt(i-1)-‘0‘))% n; dp[i][newJ]
+= dp[i-1][j]; } } } /* for(int i=0;i<len+1;i++) { for(int j=0;j<n;j++) System.out.print(dp[i][j]+" "); System.out.println(); } */ return dp[len][0]; } }

網易2017春招筆試真題編程題集合——分餅幹