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));
    }
}


目录
相关文章
SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
how to create BRF application via code
使用如下代码自动创建新的BRF application:
how to create BRF application via code
|
Java 容器
Clean Code Notes
《代码整洁之道》笔记
1332 0
|
PHP 开发工具
|
PHP 开发工具
|
Android开发 Spring
An internal error occurred during: &quot;Building UI model&quot;. com/google/common/base/Function
An internal error occurred during: "Building UI model". com/google/common/base/FunctionEclipse Neon.2 Release (4.6.2) 安装了 STS(Spring Tool Suite) 后,创建项目时遇到些问题。
2734 0
|
PHP API 机器学习/深度学习
如何在 PhpStorm 使用 Code Generation?
實務上開發專案時,有一些程式碼會不斷的出現,這時可靠 PhpStorm 的 Code Generation 幫我們產生這些 code snippet,除此之外,我們也可以將自己的 code snippet 加入 Live Template,可加快發開速度,並減少 typo。
1884 0
|
架构师 程序员 测试技术