Big Bug Ban

兴趣 践行 创新

Archive for 四月, 2011

codejam!!!

 

最近很郁闷啊..

清理htdocs的时候吧无数小代码给删掉了..

包括只有一份的pafetion1.3…

有空再重写吧..

然后今天看到google的codejam了..

来测试下去年的题~~

总共3道题

T 1:

没什么难度..不说了…

T 2:

没什么难度..不说了…

T 3:

手机拨号问题

image

就是说比如给你一个串

hello world   对应的按键应该是4433555 555666096667775553

其中空格代表停顿一下  因为连续的两个键相同 然后0代表的才是空格

然后我们分析下这个键盘.用1-27固然可以,,但是要写的代码太多了..分析下.其实2-6都是3个一组的,所以可以考虑用ascii码来解决问题

A = 97   (97-97) /3 + 1

B = 98   (98-97) /3 + 1  = 2

然后重复的次数就是余数加一

然后7 9上面有四个..比较麻烦..所以得单独判断一下..

代码如下..

/*  Copyright 2010 princehaku
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  Created on : 2011-4-19, 12:00:01
 *  Author     : princehaku
 */

package codejam3;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStreamReader;

/**
 *
 * @author princehaku
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        FileInputStream fr=new  FileInputStream("C:\\Users\\princehaku\\Desktop\\C-large-practice.in");
        BufferedReader br = new BufferedReader(new InputStreamReader(fr));
        int rr=Integer.parseInt(br.readLine());
        String content=" ";
        for(int i=1;i<=rr;i++){
            String source=br.readLine();

            String line="";

            for(int y=0;y<source.length();y++){
                line=line+ToChar(source.substring(y,y+1));
            }
            if(!line.equals("sss")){
                content=content+"Case #"+i+": "+line+"\n";
            }
        }
        System.out.println(content);
        FileWriter fw=new FileWriter("C:\\Users\\princehaku\\Desktop\\C-large-practice.out");
        fw.write(content);
        fw.close();
    }

    static int last=-99;

    private static String ToChar(String s) {
        String pre="";
        int r=(s.getBytes()[0]);
        int base=0;
        if(s.equals(" ")){
            if(last==-8){
                pre=" ";
            }
            last=-8;
            return pre+"0";
        }
        if(s.equals("s")||s.equals("z")||s.equals("y")){
            base++;
        }
        if(r>=115){
            r=r-1;
        }
        if(r>=120){
            r=r-1;
        }
        int y=(r-97)/3+2;
        if(y==last){
            pre=" ";
        }
        int rep=base+(r-97)%3+1;
        String string="";
        for(int a=0;a<rep;a++){
            string=string+y;
        }
        string=pre+string;
        last=y;
        return string;
    }

}

[download id=”18″]

 

Written by princehaku

四月 19th, 2011 at 10:46 下午