现在的位置: 首页 > 综合 > 正文

POJ1016

2014年10月16日 ⁄ 综合 ⁄ 共 3092字 ⁄ 字号 评论关闭

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1016

 思路:模拟,循环比较,需要一个数组保存下以前出现的值,判断出现循环。

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. public class Main {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader read = new BufferedReader(new InputStreamReader(
  8.                 System.in));
  9.         String s;
  10.         char[][] c;
  11.         char[] next;
  12.         int step;
  13.         int[] num;
  14.         int result;
  15.         StringBuffer buff = new StringBuffer();
  16.         while (!(s = read.readLine()).startsWith("-")) {
  17.             c = new char[15][];
  18.             step = 2;
  19.             num = new int[10];
  20.             c[0] = s.toCharArray();
  21.             while (true) {
  22.                 Arrays.fill(num, 0);
  23.                 for (int i = 0; i < c[step - 2].length; i++) {
  24.                     num[c[step - 2][i] - '0']++;
  25.                 }
  26.                 buff.delete(0, buff.length());
  27.                 for (int i = 0; i < 10; i++) {
  28.                     if (num[i] != 0) {
  29.                         buff.append(num[i]);
  30.                         buff.append(i);
  31.                     }
  32.                 }
  33.                 next = buff.toString().toCharArray();
  34.                 if ((result = equal(c, next, step)) != -1) {
  35.                     if (step == 2) {
  36.                         System.out.printf("%s is self-inventorying/n", s);
  37.                     } else {
  38.                         if (step - result == 2) {
  39.                             System.out.printf(
  40.                                     "%s is self-inventorying after %d steps/n",
  41.                                     s, step - 1);
  42.                         } else {
  43.                             System.out
  44.                                     .printf(
  45.                                             "%s enters an inventory loop of length %d/n",
  46.                                             s, step - result - 1);
  47.                         }
  48.                     }
  49.                     break;
  50.                 } else {
  51.                     if (step >= 15) {
  52.                         System.out
  53.                                 .printf(
  54.                                         "%s can not be classified after 15 iterations/n",
  55.                                         s);
  56.                         break;
  57.                     }
  58.                     c[step - 1] = next;
  59.                     step++;
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     public static int equal(char[][] a, char[] b, int t) {
  65.         int[] aa = new int[10];
  66.         int[] bb = new int[10];
  67.         loop: for (int i = 0; i < t - 1; i++) {
  68.             Arrays.fill(aa, 0);
  69.             Arrays.fill(bb, 0);
  70.             if (a[i].length != b.length) {
  71.                 continue;
  72.             } else {
  73.                 for (int j = 0; j < b.length; j++) {
  74.                     aa[a[i][j] - '0']++;
  75.                     bb[b[j] - '0']++;
  76.                 }
  77.                 for (int j = 0; j < 10; j++) {
  78.                     if (aa[j] != bb[j]) {
  79.                         continue loop;
  80.                     }
  81.                 }
  82.                 return i;
  83.             }
  84.         }
  85.         return -1;
  86.     }
  87. }

抱歉!评论已关闭.