忌み数とか(AOJ 0208)
久しぶりに何か書かないとなーなんて思って。
内容は殆どないのでお察し。
AOJ 0208 - Room Numbers of a Hospital
"4と6を使わない" というのを "0 1 2 3 5 7 8 9で表される8進数"と捉えれば簡単な問題。
import java.util.Scanner; public class Main { static final Scanner s = new Scanner(System.in); public static void main(String[] args) { while(true) { int n = s.nextInt(); if(n==0) return; System.out.println(Integer.toOctalString(n) .replace('7', '9').replace('6', '8') .replace('5', '7').replace('4', '5')); } } }
クソコード感溢れてる…
もうちょっとまともにやってみると
import java.util.Scanner; public class Main { static final Scanner s = new Scanner(System.in); public static void main(String[] args) { while(true) { int n = s.nextInt(); if(n==0) return; byte[] buf = new byte[10]; int ind = 10; for(;n>0;n>>>=3) { int x = n & 7; if((x&4)==0) buf[--ind] = (byte)(x + '0'); else if(x==4) buf[--ind] = '5'; else buf[--ind] = (byte)(x + '2'); } System.out.write(buf, ind, 10-ind); System.out.println(); } } }
長くはなったけど実行速度は少し速くなったかな…
最近AOJやってないし解けてないので少しずつやっていきたいと思います。おわり。