码迷,mamicode.com
首页 > 编程语言 > 详细

2018/12/05 PAT刷题 L1-017. 到底有多二 Java

时间:2018-12-05 12:27:42      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:parse   ++   NPU   ret   puts   har   new   string   stream   

题目麻烦一点, 但是考察的还是简单的for循环和if...else...语句的使用, 代码如下:

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 
 4 public class Main {
 5 
 6     public static void main(String[] args) throws Exception{
 7         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 8         String s = br.readLine();
 9         System.out.printf("%.2f%%", Count(s));
10     }
11     
12     public static double Count(String s) {
13         double count = 0;
14         if((s.charAt(0) + "").equals("-")) {
15             for(int i=1; i<s.length(); i++) {
16                 if ((s.charAt(i) + "").equals("2")) {
17                     count++;
18                 }
19             }
20             if ((Integer.parseInt(s.charAt(s.length() - 1) + "")) % 2 == 0) {
21                 return count / (s.length() - 1)*1.5*2*100;
22             } else {
23                 return count / (s.length() - 1)*1.5*100;
24             }
25         } else {
26             for (int i=0; i<s.length(); i++) {
27                 if((s.charAt(i) + "").equals("2")) {
28                     count++;
29                 }
30             }
31             
32             if( (Integer.parseInt(s.charAt(s.length()-1)+"")) %2 == 0) {
33                 return count / (s.length())*2*100;
34             } else {
35                 return count / (s.length())*100; 
36             }
37         }
38     }
39 }

 

2018/12/05 PAT刷题 L1-017. 到底有多二 Java

标签:parse   ++   NPU   ret   puts   har   new   string   stream   

原文地址:https://www.cnblogs.com/huangZ-H/p/10069246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!