重排数列和代码碎片的ac代码,求魔幻王国的代码
1.代码碎片
package nowcoder.netease0909; import java.text.DecimalFormat; import java.util.Scanner; public class Q1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.next(); System.out.println(getAVG(str)); } } private static String getAVG(String str) { char[] c = str.toCharArray(); int sum = 0; int time = 0; int temp = 1; int p = 0, q = 0; while (q < c.length) { if (c[q] == c[p]) { q++; } else { sum += (q - p); time++; p = q; } } if (p == q) { sum++; time++; } else { sum += (q - p); time++; } DecimalFormat df = new DecimalFormat("0.00"); return df.format((sum * 1.0) / time); } }
2.重排数列
package nowcoder.netease0909; import java.util.Scanner; public class Q2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int t = sc.nextInt(); String[] strArr = new String[t]; int index = 0; while (index < t) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } strArr[index] = canDo(arr); index++; // System.out.println(canDo(arr)); } for (String s : strArr) { System.out.println(s); } } } private static String canDo(int[] arr) { int count4 = 0, count2 = 0, countOther = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] % 4 == 0) { count4++; continue; } else if (arr[i] % 2 == 0) { count2++; continue; } else countOther++; } if (countOther + 1 <= count4 || count2 == arr.length || count4 == countOther) return "Yes"; else return "No"; } }
求魔幻王国的代码, 题目都没读懂,真难受。目测已挂。
#网易##Java工程师#