zoukankan      html  css  js  c++  java
  • UVa 1593

    题目描述 : 输入若干行代码,按照要求格式输出,。各列单词尽量靠左,单词之间至少要一个空格。

    思路 : 利用字符串数组找规律。  只要控制好边界其他的都很简单。  连测试用例都没有用,因为UVa网页老刷不出来。直接交代码QuickSubmit,只是担心会超时,但结果令人意外,竟然是AC。再来两道吧。      对了 我又不好意思的用了正则表达式。

    代码 :

    <p>import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;</p><p>public class Main1593 {</p><p> public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      Pattern p = Pattern.compile("<a target=_blank href="file://\S">\S</a>+");
      String[][] str = new String[1005][1000];
      int rows = 0;
      int[] rowcnt = new int[1000];
      Arrays.fill(rowcnt, 0);
      while(scan.hasNextLine()) {
       String line = scan.nextLine();
       Matcher m = p.matcher(line);
       int cols = 0;
       while(m.find()) {
        rowcnt[rows] ++;
        str[rows][cols++] = m.group();
       }
       rows ++;
      }
      //System.out.println(rows);
      int[] maxlen = new int[850];
      Arrays.fill(maxlen, 0);
      for(int i=0; i<rows; i++) {
       for(int j=0; j<rowcnt[i]; j++) {
        maxlen[j] = max(maxlen[j], str[i][j].length());
       }
      }
      for(int i=0; i<rows; i++) {
       for(int j=0; j<rowcnt[i]; j++) {
        System.out.print(str[i][j]);
        if(j < rowcnt[i]-1)
        for(int k=0; k<=maxlen[j]-str[i][j].length(); k++) 
         System.out.print(" ");
       }
       System.out.println();
      }
     }
     public static int max(int a, int b) {
      if(a >= b)
       return a;
      else
       return b;
     }
    }
    </p>
  • 相关阅读:
    Palindrome Linked List 解答
    Word Break II 解答
    Array vs Linked List
    Reverse Linked List II 解答
    Calculate Number Of Islands And Lakes 解答
    Sqrt(x) 解答
    Find Median from Data Stream 解答
    Majority Element II 解答
    Binary Search Tree DFS Template
    188. Best Time to Buy and Sell Stock IV
  • 原文地址:https://www.cnblogs.com/wxisme/p/4363737.html
Copyright © 2011-2022 走看看