PAT 1073

简单题,字符串处理,写的有点乱

 1 import java.util.*;
 2 import java.io.*;
 3 
 4 class FastReader{
 5     BufferedReader reader;
 6     StringTokenizer tokenizer;
 7     
 8     public FastReader(InputStream stream){
 9         reader = new BufferedReader(new InputStreamReader(stream), 1 << 22);
10         tokenizer = null;
11     }
12     
13     public String next(){
14         while (tokenizer == null || !tokenizer.hasMoreTokens()){
15             try { 
16                 tokenizer = new StringTokenizer(reader.readLine());
17             } catch (Exception e){
18                 //System.out.println(-1);
19                 throw new RuntimeException(e);
20             }
21         }
22         
23         return tokenizer.nextToken();
24     }
25     
26     public int next_int(){
27         return Integer.parseInt(next());
28     }
29     
30     public float next_float(){
31         return Float.parseFloat(next());
32     }
33 }
34 
35 public class Main {
36     public static void main(String[] args){
37         FastReader reader = new FastReader(System.in);
38         String input = reader.next();
39         
40         int num_sig = (input.charAt(0) == '+') ? 1 : -1;
41         String int_part = input.substring(1, 2);
42         
43         String dec_part;
44         int cur_idx = 3;
45         while (Character.isDigit(input.charAt(cur_idx)))
46             cur_idx++;
47         
48         dec_part = input.substring(3, cur_idx);
49         
50         cur_idx++;
51         int exp_num = Integer.parseInt(input.substring(cur_idx));
52         
53         
54         int dec_part_cnt = dec_part.length();
55         
56         StringBuilder res_sb = new StringBuilder();
57         
58         if (exp_num < 0){
59             exp_num = -exp_num;
60             
61             if (num_sig == -1)
62                 res_sb.append('-');
63             
64             res_sb.append("0.");
65             exp_num--;
66             while (exp_num != 0){
67                 res_sb.append('0');
68                 exp_num--;
69             }
70             
71             res_sb.append(int_part);
72             res_sb.append(dec_part);
73         } else {
74             if (num_sig == -1)
75                 res_sb.append('-');
76             
77             if (exp_num >= dec_part_cnt){
78                 res_sb.append(int_part);
79                 res_sb.append(dec_part);
80                 
81                 int zero_cnt = exp_num - dec_part_cnt;
82                 for (int i = 0; i < zero_cnt; i++)
83                     res_sb.append('0');
84             } else {
85                 res_sb.append(int_part);
86                 res_sb.append(dec_part.substring(0, exp_num));
87                 res_sb.append('.');
88                 res_sb.append(dec_part.substring(exp_num));
89             }
90         }
91         
92         System.out.println(res_sb.toString());
93     }
94 }

 

posted @ 2014-11-03 20:57  EpisodeXI  阅读(179)  评论(0编辑  收藏  举报