Code Jam练习

今年Google笔试用Code Jam。今天试试怎么用。这东西很威武啊,支持各种语言,只要免费许可的都可以,还可以下载别人提交的code。做了几个非常简单的,结果就遇到坑了。

https://code.google.com/codejam/contest/189252/dashboard

第一道题很简单,将一些字母数字的组合翻译成十进制,类似IEEE754的float格式。需要注意的是,使得到的数字最小,则首位字符对应0,第2个与首位不同的字符对应0。

public long readFormat(String s) {
    int[] map = new int[256];
    int used = 0;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (map[c] == 0) {
        used++;
        map[c] = used;
        }
    }
    int radix = used;
    if (radix == 1)
        radix = 2;
    long ret = 0;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        int num = map[c];
        if (num == 1) {
        } else if (num == 2) {
        num = 0;
        } else {
        num--;
        }
        ret *= radix;
        ret += num;
    }
    return ret;
}

void run() {
    int COUNT = sc.nextInt();
    for (int c = 0; c < COUNT; c++) {
        String s = sc.next();
        System.out.println(String.format("Case #%d: %d", c + 1,
            readFormat(s)));
    }
}

第二道是几何问题,也不难。最后计算长度的时候遇到问题。这个长度可以用速度乘以时间算出来,同时这个长度也是直角三角形的一个边,也可以根据勾股定律用另两条边求出来。我按照第一个方法提交失败,按照第二个方法,就成功了。

class Pos {
    double x, y, z;

    public Pos() {
    }

    public Pos(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public void add(Pos p) {
        this.x += p.x;
        this.y += p.y;
        this.z += p.z;
    }

    public void divide(double d) {
        this.x /= d;
        this.y /= d;
        this.z /= d;
    }

    public double multiply(Pos p) {
        return this.x * p.x + this.y * p.y + this.z * p.z;
    }

    public Pos multiply(double d) {
        this.x *= d;
        this.y *= d;
        this.z *= d;
        return this;
    }

    public double getDistanceSqare() {
        return x * x + y * y + z * z;
    }

    public double getDistance() {
        debug(getDistanceSqare());
        return sqrt(getDistanceSqare());
    }
}

class Fly {
    Pos p = new Pos();
    Pos v = new Pos();

    public void add(Fly fl) {
        p.add(fl.p);
        v.add(fl.v);
    }

    public void divide(int d) {
        p.divide(d);
        v.divide(d);
    }

    @Override
    public String toString() {
        return "x: " + p.x + " y: " + p.y + " z: " + p.z + " vx: " + v.x
                + " vy: " + v.y + " vz: " + v.z;
    }
}

void run() {
    int COUNT = sc.nextInt();
    for (int c = 0; c < COUNT; c++) {
        int cc = sc.nextInt();
        Fly center = new Fly();
        for (int i = 0; i < cc; i++) {
            Fly fl = new Fly();
            fl.p.x = sc.nextInt();
            fl.p.y = sc.nextInt();
            fl.p.z = sc.nextInt();
            fl.v.x = sc.nextInt();
            fl.v.y = sc.nextInt();
            fl.v.z = sc.nextInt();
            center.add(fl);
        }
        center.divide(cc);
        debug(center);
        double dotResult = -center.p.multiply(center.v);
        double distance = 0;
        double time = 0;
        if (dotResult <= Double.MIN_VALUE) {
            distance = center.p.getDistance();
        } else {
            double vDisSqare = center.v.getDistanceSqare();
            time = dotResult / vDisSqare;
            distance = center.p.getDistanceSqare()
                    - center.v.getDistanceSqare() * time * time;
            if (distance < 0)
                distance = 0;
            else
                distance = sqrt(distance);
        }
        System.out.println(String.format("Case #%d: %.8f %.8f", c + 1,
            distance, time));
    }
}
时间: 2024-09-18 03:12:30

Code Jam练习的相关文章

Code Jam 2010 Round 1B Problem A

Problem B. Picking Up Chicks Problem A flock of chickens are running east along a straight, narrow road. Each one is running with its own constant speed. Whenever a chick catches up to the one in front of it, it has to slow down and follow at the spe

Code Jam 2010 Round 1A Problem C

Problem C. Number Game Problem Arya and Bran are playing a game. Initially, two positive integers A and B are written on a blackboard. The players take turns, starting with Arya. On his or her turn, a player can replace A with A - kB for any positive

Code Jam 2010 Round 1A Problem B

Problem B. Make it Smooth Problem You have a one-dimensional array of N pixels. Each pixel has a value, represented by a number between 0 and 255, inclusive. The distance between two pixels is the absolute difference of their numbers. You can perform

Code Jam 2010 Round 1A Problem A

Problem A. Rotate Problem In the exciting game of Join-K, red and blue pieces are dropped into an N-by-N table. The table stands up vertically so that pieces drop down to the bottom-most empty slots in their column. For example, consider the followin

怎样成为一个编程高手

在过去5年中,我面试了数百名IT工程师,我认为很值得把我的面试IT工程师的经验同大家分享.这能够: ●帮助人们为他们的下一个面试准备 ●帮助IT工程师找到合适的工作. ●帮助IT工程师创造更好的环境. 我们通过两个方面面试IT工程师: ●技术技能:包括理论和实践技能以及经验等. ●非技术技能:性格,沟通技巧,是否能够融入我们的文化环境. 第1部分中,我将主要从技能部分讲述. 1.一个IT工程师应该具有的技能 我认为实用技能包含3个方面,如图1所示:               图1 这些都是很重

SAP开发负责人:HANA担起大数据开发重任

文章讲的是SAP开发负责人:HANA担起大数据开发重任,在一年一度的开发者与技术大会上,SAP公司公布了其在SAP HANA数据平台编程领域所取得的切实进步以及深层细节.更具体地讲,开发者关系负责人Thomas Grassl宣布在最近于加利福尼亚州举办的Code Jam编程挑战赛上,已经有各种类型.各个级别的程序开发人员着手尝试基于SAP HANA的应用程序与代码流. Grassl指出,“SAP HANA为开发人员们提供全新机遇,由其带来的改变使我们能够实现大数据应用程序的设计.创建以及投付生产

中国唯一的图灵奖获得者姚期智,在清华开设的“姚班”有哪些 AI 名徒?

今日,雷锋网了解到,已放弃外国国籍成为中国公民的中国科学院外籍院士姚期智教授日前正式转为中国科学院院士. 姚期智是世界著名计算机学家,是中国唯一获得图灵奖的学者.同时也是美国科学院院士,美国科学与艺术学院院士,中国科学院院士,清华大学高等研究中心教授,香港中文大学博文讲座教授.2004 年起在清华大学任全职教授.现任清华大学交叉信息研究院院长.教授,香港中文大学博文讲座教授. 2005 年,姚期智创立了清华大学计算机科学实验班,也就是大名鼎鼎的"姚班". "姚班"已

天极网:Google启动2005年编程大赛

        Google公司近日宣布2005年全球编程大赛"Google Code Jam 2005"正式启动.Google的全球编程大赛每年都举行.电脑爱好者可以通过网上报名,选择自己喜欢的编程语言,解决一系列Google提出的挑战性问题,用他们的编程技术产生编码解决方案.报名地址是:www.google.com/codejam05.      Google的工程副总裁Alan Eustace表示:"Google编程大赛旨在聚集全球顶尖的计算机人才,并展示他们在解决问题

阿里巴巴校招内推简历筛选方案(总结篇)

        在论坛看到的,阿里巴巴HR的甄选方案,觉得值得推荐,总结下发到首页的,觉得有用希望更多人看到.        上周发了一个阿里内推的帖子,没想到短时间内就收到了成百上千封简历.   我仔仔细细地看了每一封简历,附带有Github地址的我也点进去仔细看了代码.   最终我留下了30%的简历,而且这30%中只有10%的本科生.   所有通过内推初步筛选的小伙伴会在8月30号收到邮件通知.   筛选标准   满足以下条件中的任何一个,直接通过筛选:    * ACM, Topcoder