第四题 import java.util.Scanner; public class Main { static int[] pow10 = new int[10]; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int k = 1; for(int i = 0;i < 10;i++) { pow10[i] = k; k *= 10; } for(int i = 0;i < n;i++) System.out.print(getNum(in.nextInt(),m) + " "); } public static int getNum(int w,int m){ if(w == 0 || w%m == 0) return 0; if(m-w == 1) return 1; int min = Integer.MAX_VALUE; for(int i = 0;i < 10;i++){ int dist = w+pow10[i]; if(dist < m) dist = dist; else if(dist%m == 0) dist = m; else if(dist > m) dist = dist%m; if(dist <= w) continue; min = Math.min(min,getNum(dist,m)+1); } return min; } }
点赞 2
牛客网
牛客企业服务