1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public class 出现K次 {
public static void main(String[] args) { int[] arr = {2, 2, 2, 9, 7, 7, 7, 3, 3, 3, 6, 6, 6, 0, 0, 0}; int len = arr.length; char[][] kRadix = new char[len][]; int k = 3; int maxlen = 0; for(int i = 0; i < len; i++) { kRadix[i] = new StringBuffer(Integer.toString(arr[i], k)).reverse().toString().toCharArray(); if(kRadix[i].length > maxlen) maxlen = kRadix[i].length; } int[] resArr = new int[maxlen]; for(int i = 0; i < len; i++) { for(int j = 0; j < maxlen; j++) { if(j >= kRadix[i].length) resArr[j] += 0; else resArr[j] += (kRadix[i][j] - '0'); } } int res = 0
; for(int i = 0; i < maxlen; i++) { res += (resArr[i] % k) * (int)(Math.pow(k, i)); } System.out.println(res); } }
|