LoginSignup
1
0

More than 5 years have passed since last update.

第8回オフラインリアルタイムどう書くの参考問題をJAVAで解きました

Posted at

イベントページは http://atnd.org/events/36783
問題は http://nabetani.sakura.ne.jp/hena/ord8entco/

まだ慣れていないけど書いてみました。

qiita.java

package java_test8;
public class java_test {
    public static void main(String[] args) {
        ent();
    }
    static int test(String input, String output){
        int i,t;
        String bin;
        String ans="";

        /*入力した16進数を2進数に変換*/
        bin=hextobin(input);
        i=bin.length();
        t=0;

        while(t<=i){
            if((bin.indexOf("000",t)==t)){
                ans=ans+"t";
                t=t+3;;
            }
            else if((bin.indexOf("0010",t)==t)){
                ans=ans+"s";
                t=t+4;
            }
            else if((bin.indexOf("0011",t)==t)){
                ans=ans+"n";
                t=t+4;
            }
            else if((bin.indexOf("0100",t)==t)){
                ans=ans+"i";
                t=t+4;
            }
            else if((bin.indexOf("01010",t)==t)){
                ans=ans+"d";
                t=t+5;
            }
            else if((bin.indexOf("0101101",t)==t)){
                ans=ans+"c";
                t=t+7;
            }
            else if((bin.indexOf("010111",t)==t)){
                ans=ans+"l";
                t=t+6;
            }
            else if((bin.indexOf("0110",t)==t)){
                ans=ans+"o";
                t=t+4;
            }
            else if((bin.indexOf("0111",t)==t)){
                ans=ans+"a";
                t=t+4;
            }
            else if((bin.indexOf("10",t)==t)){
                ans=ans+"e";
                t=t+2;
            }
            else if((bin.indexOf("1100",t)==t)){
                ans=ans+"r";
                t=t+4;
            }
            else if((bin.indexOf("1101",t)==t)){
                ans=ans+"h";
                t=t+4;
            }
            else if((bin.indexOf("111",t)==t)){
                /*終端符号があった場合、終了*/
                t=t+3;
                ans=ans+":";
                System.out.println(ans+t);
                return 0;
            }
            else{
                /*上記に該当しない符号があった場合は終了*/
                System.out.println("*invalid*");
                return 0;
            }
        }
        System.out.println("*invalid*");
        return 0;

    }
    static void ent(){
        /*0*/ test( "16d9d4fbd", "ethanol:30" );
        /*1*/ test( "df", "e:5" );
        /*2*/ test( "ad7", "c:10" );
        /*3*/ test( "870dcb", "t:6" );
        /*4*/ test( "880f63d", "test:15" );
        /*5*/ test( "a57cbe56", "cat:17" );
        /*6*/ test( "36abef2", "roll:23" );
        /*7*/ test( "ad576cd8", "chant:25" );
        /*8*/ test( "3e2a3db4fb9", "rails:25" );
        /*9*/ test( "51aa3b4c2", "eeeteee:18" );
        /*10*/ test( "ad5f1a07affe", "charset:31" );
        /*11*/ test( "4ab8a86d7afb0f", "slideshare:42" );
        /*12*/ test( "ac4b0b9faef", "doctor:30" );
        /*13*/ test( "cafebabe", "nlh:17" );
        /*14*/ test( "43e7", "sra:15" );
        /*15*/ test( "53e7", "eera:15" );
        /*16*/ test( "86cf", "tera:16" );
        /*17*/ test( "b6cf", "hon:15" );
        /*18*/ test( "0", "*invalid*" );
        /*19*/ test( "c", "*invalid*" );
        /*20*/ test( "d", "*invalid*" );
        /*21*/ test( "e", "*invalid*" );
        /*22*/ test( "babecafe", "*invalid*" );
        /*23*/ test( "8d", "*invalid*" );
        /*24*/ test( "ad", "*invalid*" );
        /*25*/ test( "af", "*invalid*" );
        /*26*/ test( "ab6e0", "*invalid*" );
        /*27*/ test( "a4371", "*invalid*" );
        /*28*/ test( "a4371", "*invalid*" );
        /*29*/ test( "96e3", "*invalid*" );
        /*30*/ test( "0dc71", "*invalid*" );
        /*31*/ test( "2a9f51", "*invalid*" );
        /*32*/ test( "a43fb2", "*invalid*" );
        /*33*/ test( "ab6e75", "*invalid*" );
        /*34*/ test( "a5dcfa", "*invalid*" );
        /*35*/ test( "ca97", "*invalid*" );
        /*36*/ test( "6822dcb", "*invalid*" );
    }

    static String hextobin(String hex){
        String bin="";
        int i,j;
        j=hex.length();
        i=0;
        for(i=0;i<j;i++){
            if((hex.substring(i,i+1).equals("0"))){
                bin=bin+"0000";
            }
            else if((hex.substring(i,i+1).equals("1"))){
                bin=bin+"1000";
            }
            else if((hex.substring(i,i+1).equals("2"))){
                bin=bin+"0100";
            }
            else if((hex.substring(i,i+1).equals("3"))){
                bin=bin+"1100";
            }
            else if((hex.substring(i,i+1).equals("4"))){
                bin=bin+"0010";
            }
            else if((hex.substring(i,i+1).equals("5"))){
                bin=bin+"1010";
            }
            else if((hex.substring(i,i+1).equals("6"))){
                bin=bin+"0110";
            }
            else if((hex.substring(i,i+1).equals("7"))){
                bin=bin+"1110";
            }
            else if((hex.substring(i,i+1).equals("8"))){
                bin=bin+"0001";
            }
            else if((hex.substring(i,i+1).equals("9"))){
                bin=bin+"1001";
            }
            else if((hex.substring(i,i+1).equals("a"))){
                bin=bin+"0101";
            }
            else if((hex.substring(i,i+1).equals("b"))){
                bin=bin+"1101";
            }
            else if((hex.substring(i,i+1).equals("c"))){
                bin=bin+"0011";
            }
            else if((hex.substring(i,i+1).equals("d"))){
                bin=bin+"1011";
            }
            else if((hex.substring(i,i+1).equals("e"))){
                bin=bin+"0111";
            }
            else if((hex.substring(i,i+1).equals("f"))){
                bin=bin+"1111";
            }
        }
        return bin;
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0