AtCoder ABC 068 A&B&C
A問題
- 問題文上は0埋めいらないので、本当は
"ABC" + numN
でもよい
private void solveA() {
int numN = nextInt();
out.println("ABC" + String.format("%03d", numN));
}
B問題
- 2で何回割れるのか?
- 一つ一つチェックするfunction()を作ったけど、、、結局のところ「N以下の2の倍数の最大値」を出せばよい
- $N\leqq100$ なので、 ${1,2,4,8,16,32,64}$ の中でN以下の最大を探すだけではある。
private void solveB() {
int numN = nextInt();
int res = 0;
int index = 0;
for (int i = numN; i > 0; i--) {
int wk = chkB(i);
if (res <= wk) {
res = wk;
index = i;
}
}
out.println(index);
}
private int chkB(int numN) {
int cnt = 0;
while (numN > 0) {
if (numN % 2 == 0) {
numN /= 2;
cnt++;
} else {
break;
}
}
return cnt;
}
C問題
-
島1から始まり、2回で島Nにたどり着けることが条件なので
- $a_i=1$ の $b_i$ を取得して、
- $a_i=b_i$ かつ $b_i=N$ となっている組が存在するかを調べればよい
-
2回で行くという限定条件下でのコード
- $a_i=1$ の $b_i$ をsetに詰める(from)
- $b_i=N$ の $a_i$ をsetに詰める(to)
- fromの要素がtoの要素に含まれるなら2回で行ける
private void solveC() {
int numN = nextInt();
int numM = nextInt();
int[][] wk = new int[numM][2];
Set<Integer> fromSet = new HashSet<Integer>();
Set<Integer> toSet = new HashSet<Integer>();
for (int i = 0; i < numM; i++) {
wk[i][0] = nextInt();
wk[i][1] = nextInt();
if (wk[i][0] == 1) {
fromSet.add(wk[i][1]);
} else if (wk[i][1] == numN) {
toSet.add(wk[i][0]);
}
}
for (Integer integer : fromSet) {
if (toSet.contains(integer)) {
out.println("POSSIBLE");
return;
}
}
out.println("IMPOSSIBLE");
}