首页 > 专题系列 > Java解POJ > POJ 3332 Parsing Real Numbers [解题报告] Java
2013
11-12

POJ 3332 Parsing Real Numbers [解题报告] Java

Parsing Real Numbers

问题描述 :

Write a program that read a line of input and checks if the line contains a valid real number. Real numbers may have a decimal point, an exponent (starting with the character e or E), or both. Additionally, it has the usual collection of decimal digits. If there is a decimal point, there must be at least one digit on each side of the point. There may be a plus or minus sign in front of the number, or the exponent, or both (without any blank characters after the sign). Exponents are integers (not having decimal points). There may be blank characters before or after a number, but not inside it. Note that there is no bound on the range of the numbers in the input, but for the sake of simplicity, you may assume the input strings are not longer than 1000 characters.

输入:

The first line of the input contains a single integer T which is the number of test cases, followed by T lines each containing the input line for a test case.

输出:

The output contains T lines, each having a string which is LEGAL or ILLEGAL.

样例输入:


样例输出:

LEGAL
ILLEGAL

解题代码:

import java.util.regex.*;import java.util.*;
public class Main { 
     public static void main(String[] args){ 
         String str; 
         int n; 
         Scanner cin = new Scanner(System.in);
          n=cin.nextInt();  
          str=cin.nextLine(); 
         Pattern pattern = Pattern.compile("(\\s)*(\\+|\\-)?(\\d)+(\\.(\\d)+)?((e|E)(\\+|\\-)?(\\d)+)?");
         int i;
          for(i=0;i< n;++i){
              str=cin.nextLine(); 
             Matcher matcher = pattern.matcher(str); 
             if(matcher.matches()){ 
                 System.out.println("LEGAL"); 
             } 
             else{ 
                 System.out.println("ILLEGAL");
              } 
         } 
     }
}

  1. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。