生活随笔
收集整理的這篇文章主要介紹了
PAT_B_1012_Java(20分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個題目在最后一個測試點有一個小坑,就是判斷A2是否為零的情況有兩種可能:
(1)沒有符合條件的數,A2始終為0;
(2)進行至少兩步的交錯相加,結果為0.
一開始我沒有想到第二種情況導致最后一個測試點一直出錯,后添加了一個變量a2對上面兩種情況進行判斷,得以全部通過。
import java
.io
.BufferedReader
;
import java
.io
.IOException
;
import java
.io
.InputStreamReader
;public class Main {public static void main(String
[] args
) throws IOException
{BufferedReader bf
= new BufferedReader(new InputStreamReader(System
.in
));String
[] str
= bf
.readLine().split(" ");int[] max
= new int[str
.length
];int A1
= 0, A2
= 0, A3
= 0, A4
= 0, A5
= 0, a2
= 0;double sum
= 0;int flag
= 1;for (int i
= 1; i
< str
.length
; i
++) {if (Integer
.parseInt(str
[i
]) % 5 == 0 && Integer
.parseInt(str
[i
]) % 2 == 0) {A1
+= Integer
.parseInt(str
[i
]);} else if (Integer
.parseInt(str
[i
]) % 5 == 1) {a2
++;A2
+= flag
* Integer
.parseInt(str
[i
]);flag
= -flag
;} else if (Integer
.parseInt(str
[i
]) % 5 == 2) {A3
++;} else if (Integer
.parseInt(str
[i
]) % 5 == 3) {sum
+= Integer
.parseInt(str
[i
]);A4
++;} else if (Integer
.parseInt(str
[i
]) % 5 == 4) {max
[A5
++] = Integer
.parseInt(str
[i
]);}}if (A1
== 0) {System
.out
.print("N ");} else {System
.out
.print(A1
+ " ");}if (A2
== 0 && a2
== 0) {System
.out
.print("N ");} else {System
.out
.print(A2
+ " ");}if (A3
== 0) {System
.out
.print("N ");} else {System
.out
.print(A3
+ " ");}if (A4
== 0) {System
.out
.print("N ");} else {System
.out
.print(String
.format("%.1f ", sum
/ A4
));}if (A5
== 0) {System
.out
.print("N");} else {System
.out
.print(sort(max
));}}public static int sort(int[] max
) {int max1
= 0;for (int i
= 1; i
< max
.length
; i
++) {if (max
[i
] > max
[max1
])max1
= i
;continue;}return max
[max1
];}
}
總結
以上是生活随笔為你收集整理的PAT_B_1012_Java(20分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。