• 洛谷算法记录-P1013


    题目:P1013 [NOIP1998 提高组] 进制

    解题思路:

            1:根据题干要求:9>=N>=3 , 且字母不重复,所以是2进制到8进制之间的数据

            2:假设N=5,即输入四进制数据,则S(i,0)列必是:0,1,2,3;并作为每行的开头数字(行顺序可先不在意),每行简单的和每列数字做下加法如下:

           1开头的行(10)                     2开头的行(10,11)                3开头的行(10,11,12)

            1+1 = 02        ​​​​​​​                         2+1 = 03        ​​​​​​​                   3+0 = 03          ​​​​​​​
            1+2 = 03                                 2+2 = 10                           3+1 = 10                                 
          
      1+3 = 10     ​​​​​​​                            2+3 = 11           ​​​​​​​                3+2 = 11                      
                                                                                                    3+3 = 12     

            识别行开头的逻辑:根据上面规律发现行开头是数字n,则行内出现双位数的数量即n;反之出现了几个双位数就是几开头。

             3:题干其他判断逻辑:

                    一:斜对角对称

                    二:第一列或第一行内容不重复

                    三:S[i,0]值 + S[0,j]值 =  S[i,j]   //S[i,j]字符根据位数做切割,并把十位字符数乘以进制数,再加上个位数值

    编码解决:

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. run();
    5. }
    6. public static void run() {
    7. try { //初始化输入
    8. Scanner scanner = initScanner();
    9. //读取整数
    10. Integer n = readNumnber(scanner, 3, 9);
    11. //根据整数构建二维数组
    12. String[][] datas = read2Array(scanner, n);
    13. //统计 - 每个字符出现次数
    14. Map rs = new LinkedHashMap<>();
    15. //校验数据行
    16. for (int i = 0; i < n; i++) {
    17. if (!datas[i][0].equals(datas[0][i])) {
    18. throw new RuntimeException("不对称");
    19. }
    20. if (datas[i][0].length() != 1) {
    21. throw new RuntimeException("字母长度不对");
    22. }
    23. //统计每个字母行内出现2位长度的字符数量,即当前行字符的数值
    24. int cnt = 0;
    25. for (int j = 0; j < n; j++) {
    26. if (datas[i][j].length() <= 1) {//2位数
    27. continue;
    28. }
    29. cnt++;
    30. }
    31. rs.put(datas[i][0], cnt + "");
    32. }
    33. //判断字符是否重复
    34. if (rs.keySet().size() != n) {
    35. throw new RuntimeException("数组高度重复");
    36. }
    37. //移除加号统计
    38. rs.remove("+");
    39. //替换二维数组字符 转 数字
    40. for (int i = 0; i < n; i++) {
    41. for (int j = 0; j < n; j++) {
    42. if ("+".equals(datas[i][j])) {
    43. continue;
    44. }
    45. for (Map.Entry entry : rs.entrySet()) {
    46. datas[i][j] = datas[i][j].replaceAll("[" + entry.getKey() + "]", entry.getValue());
    47. }
    48. }
    49. if ("+".equals(datas[i][0])) {
    50. continue;
    51. }
    52. //数值不能超过进制最大值
    53. if (Integer.parseInt(datas[i][0]) > n - 2) {
    54. throw new RuntimeException("超出进制范围");
    55. }
    56. }
    57. //校验数据行
    58. for (int i = 1; i < n; i++) {
    59. for (int j = 1; j < n; j++) {
    60. //加法校验(n-1进制)
    61. if (datas[i][j].length() <= 1) {//单字符值校验
    62. if (Integer.parseInt(datas[i][0]) + Integer.parseInt(datas[0][j]) != Integer.parseInt(datas[i][j])) {
    63. throw new RuntimeException("进制加法不通过 n=" + (n - 1) + " i=" + i + ",j=" + j + " -> " + datas[i][j]);
    64. }
    65. } else {//双位字符值校验
    66. String[] ds = datas[i][j].split("");
    67. if (Integer.parseInt(datas[i][0]) + Integer.parseInt(datas[0][j]) != Integer.parseInt(ds[0]) * (n - 1) + Integer.parseInt(ds[1])) {
    68. throw new RuntimeException("进制加法不通过 n=" + (n - 1) + " i=" + i + ",j=" + j + " -> " + datas[i][j]);
    69. }
    70. }
    71. }
    72. }
    73. String text = rs.toString().replaceAll("[{}\"]", "").replaceAll(":", "=");
    74. text = text.replaceAll(",", "");
    75. System.out.println(text);
    76. System.out.print(n - 1);
    77. } catch (RuntimeException e) {
    78. //e.printStackTrace();
    79. System.out.print("ERROR!");
    80. }
    81. }
    82. public static Scanner initScanner() {
    83. return new Scanner(System.in);
    84. }
    85. //读取整数行
    86. public static int readNumnber(Scanner scanner, Integer min, Integer max) {
    87. int val = Integer.parseInt(scanner.nextLine());
    88. if (min != null && val < min) {
    89. throw new RuntimeException("数值" + val + "不能小于" + min);
    90. }
    91. if (max != null && val > max) {
    92. throw new RuntimeException("数值" + val + "不能大于" + max);
    93. }
    94. return val;
    95. }
    96. //输入读取二维数组
    97. public static String[][] read2Array(Scanner scanner, int arraySize) {
    98. String[][] datas = new String[arraySize][arraySize];
    99. for (int i = 0; i < arraySize; i++) {
    100. String line = scanner.nextLine();
    101. if (line == null || line.equals("")) {//如果是最后一行,使用next
    102. break;
    103. }
    104. String[] l = line.split(" ");
    105. if (l.length != arraySize) {
    106. throw new RuntimeException("数组长度不对");
    107. }
    108. datas[i] = l;
    109. }
    110. for (int i = 0; i < datas.length; i++) {
    111. for (int j = 0; j < datas[i].length; j++) {
    112. //System.out.print(datas[i][j] + " ");
    113. }
    114. //System.out.println("");
    115. }
    116. return datas;
    117. }
    118. }

      
        
        
     

        
      
        
     

  • 相关阅读:
    自幂数的统计
    win10 自带虚拟机软件 虚拟CentOS系统
    正则表达式
    案例分享:某汽车企业通过龙智拓展Jira功能,实现高效项目管理
    数据类型(面向对象)
    顶刊TPAMI 2022!基于不同数据模态的行为识别:最新综述
    OpenJDK16 ZGC 源码分析
    CSS学习笔记
    延时队列java
    【人脸识别】face_recognition 库的使用
  • 原文地址:https://blog.csdn.net/WJL_MGQS/article/details/126974522