AtCoder ABC 082 B&C
A問題
- 平均値を求めて切り上げる
private void solveA() {
Scanner scanner = null;
String a = "0";
String b = "0";
try {
scanner = new Scanner(System.in);
a = scanner.next();
b = scanner.next();
BigDecimal aB = new BigDecimal(a);
BigDecimal bB = new BigDecimal(b);
BigDecimal res = aB.add(bB);
System.out.println(res.divide(new BigDecimal("2"), 0, RoundingMode.UP));
} finally {
if (scanner != null) {
scanner.close();
}
}
}
B問題
- SをA to Zとなるように操作
-
yx
をxy
とする- そのために配列に変えてソートをする。その結果、
yx
という配列がxy
に並び変わる- 文字コード順にソートされるため
- そのために配列に変えてソートをする。その結果、
- TをA to Zとなるように操作
-
axy
をaxy
とする(この例だと変化なし) - Sはその文字順のまま、Tの文字について前後を入れ替える(Tはreverseする)
- Tを
axy
->yxa
に変える
- Tを
- 後は、操作した後のSとTの順序を調べる
- Listに入れてソートすると楽かなとおもった。
private void solveB() {
Scanner scanner = null;
String s = "";
String t = "";
try {
scanner = new Scanner(System.in);
s = scanner.next();
t = scanner.next();
List<String> wkList = new ArrayList<String>();
char[] sA = s.toCharArray();
char[] tA = t.toCharArray();
Arrays.sort(sA);
Arrays.sort(tA);
String wkSa = new String(sA);
String wkTa = new StringBuilder(new String(tA)).reverse().toString();
wkList.add(wkSa);
wkList.add(wkTa);
Collections.sort(wkList);
boolean res = false;
if (s.equals(t)) {
res = false;
} else {
res = wkList.get(0).equals(wkSa);
}
if (res) {
System.out.println("Yes");
} else {
System.out.println("No");
}
} finally {
if (scanner != null) {
scanner.close();
}
}
}
C問題
- 数字毎の個数をカウントする
- 数字と個数を比較し
- 同じなら何もしなくてよい
- 数字の方が個数よりも多いなら、その数字はすべて取り除く必要がある
- 個数の方が数字よりも多いなら、数字よりも多い個数を取り除く必要がある
for文versionもコメントアウトして入れてある。
private void solveC() {
Scanner scanner = null;
int n = 0;
try {
scanner = new Scanner(System.in);
n = scanner.nextInt();
int[] wk = new int[n];
for (int i = 0; i < n; i++) {
wk[i] = scanner.nextInt();
}
Map<Integer, Integer> wkMap = new HashMap<Integer, Integer>();
Arrays.stream(wk).forEach(elm -> {
wkMap.merge(elm, 1, (oldV, newV) -> oldV + newV);
});
/**
* For version
*/
// for (int i = 0; i < wk.length; i++) {
// if (!wkMap.containsKey(wk[i])) {
// wkMap.put(wk[i], 1);
// } else {
// wkMap.put(wk[i], wkMap.get(wk[i]) + 1);
// }
// }
int res = wkMap.keySet().stream().reduce(0, (sum, entry) -> {
int num = entry;
int size = wkMap.get(num);
if (num == size) {
} else if (num > size) {
sum += size;
} else if (num < size) {
sum += Math.abs(size - num);
}
return sum;
});
/**
* For version
*/
// for (Iterator<Integer> ite = wkMap.keySet().iterator(); ite.hasNext();) {
// int num = ite.next();
// int size = wkMap.get(num);
// if (num == size) {
// } else if (num > size) {
// res += size;
// } else if (num < size) {
// res += Math.abs(size - num);
// }
// }
System.out.println(res);
} finally {
if (scanner != null) {
scanner.close();
}
}
}