zoukankan      html  css  js  c++  java
  • 机试题

    上午做了个机试题,没通过零分。总结了下,是AR没分解好,代码然成一坨调试很浪费时间。

    中午吃饭想了下,20分钟编了出来,代码如下:

     1 package test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 import java.util.Scanner;
     6 
     7 public class Main
     8 {
     9 
    10     private static final int MAX_LEN = 50;
    11 
    12     private static int num = 0; // 2
    13     private static int len = 0; // 8
    14     private static String[] cont = null;    // {"abc", "123456789"}
    15 
    16     public static void main(String[] args)
    17     {
    18         processInput();
    19         List<String> result = new ArrayList<String>();
    20         for (String s : cont)
    21         {
    22             List<String> list = new ArrayList<String>();
    23             formatStr(s, list);
    24             result.addAll(list);
    25         }
    26         for (String s : result)
    27         {
    28             System.out.println(s);
    29         }
    30     }
    31 
    32     /**
    33      * process input
    34      */
    35     private static void processInput()
    36     {
    37         Scanner cin = new Scanner(System.in);
    38         int idx = 0;
    39         if (cin.hasNext())
    40         {
    41             String s1 = cin.next();
    42             String[] numAndLen = s1.split(",");
    43             num = Integer.parseInt(numAndLen[0]);
    44             len = Integer.parseInt(numAndLen[1]);
    45             len = Math.min(len, MAX_LEN);
    46         }
    47         cont = new String[num];
    48         while (idx < num)
    49         {
    50             cont[idx++] = cin.next();
    51         }
    52     }
    53 
    54     /**
    55      * add zero to string tail
    56      * @param str
    57      * @return
    58      */
    59     private static String addZero(String str)
    60     {
    61         int sub = len - str.length();
    62         for (int i = 0; i < sub; i++)
    63         {
    64             str += "0";
    65         }
    66         return str;
    67     }
    68 
    69     /**
    70      * process original string
    71      * @param str
    72      * @param list
    73      */
    74     private static void formatStr(String str, List<String> list)
    75     {
    76         if (str.length() <= len)
    77         {
    78             list.add(addZero(str));
    79         }
    80         else
    81         {
    82             list.add(str.substring(0, len));
    83             formatStr(str.substring(len), list);
    84         }
    85     }
    86 
    87 }

    呵呵,想想应该遵循此原则。

    1. 处理输入部分,先打桩数据,不做处理。  // 调试太麻烦

    2. 按照思路,先写出方法名,内容随后补齐。

    3. 必然涉及字符串补齐,先编并测。

    4. 既然一开始就想到要递归,干脆将递归先写出来。

  • 相关阅读:
    datagrid
    IntelliJ IDEA for mac 引入js注意事项
    centos7安装并配置svn
    yum使用总结
    安装php
    类视图
    django里面添加静态变量
    Ubuntu16.04安装&创建虚拟环境
    制作dockerfile, 天眼查的镜像、并运行
    dockerfile
  • 原文地址:https://www.cnblogs.com/xzs603/p/3447525.html
Copyright © 2011-2022 走看看