Pages

Friday 28 December 2012

UVA - 1419 - Binary Clock

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        int cases=Integer.parseInt(br.readLine().trim());
        for(int l=1;l<cases+1;l++){
            String[] str=br.readLine().trim().split(":");
            StringBuilder hour=lengthOfWord(Integer.parseInt(str[0]));
            StringBuilder minute=lengthOfWord(Integer.parseInt(str[1]));
            StringBuilder second=lengthOfWord(Integer.parseInt(str[2]));
            char[][]ver=new char[6][3];
            char[][]hor=new char[3][6];
            for(int i=0;i<6;i++){
                ver[i][0]=hour.charAt(i);
                hor[0][i]=ver[i][0];
                ver[i][1]=minute.charAt(i);
                hor[1][i]=ver[i][1];
                ver[i][2]=second.charAt(i);
                hor[2][i]=ver[i][2];
            }
            sb.append(l).append(" ");
            for(int i=0;i<6;i++){
                for(int j=0;j<3;j++){
                    sb.append(ver[i][j]);
                }
            }
            sb.append(" ");
            for(int i=0;i<3;i++){
                for(int j=0;j<6;j++){
                    sb.append(hor[i][j]);
                }
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
   
    static StringBuilder lengthOfWord(int h){
        StringBuilder x=new StringBuilder(Integer.toString(h, 2));
        for(int i=x.length();i<6;i++){
                x.insert(0, "0");
        }
        return x;
    }
}

No comments:

Post a Comment