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


目录
相关文章
|
C++
hdoj 4288coder & cf 85d Sum of Medians
这两个题目是一样的,大概题意是有3个操作 add x, 在集合中加入x, del x 是删除x, sum 是求出由小到大排序后所有下标mod5等于3的数的和。
33 0
|
存储
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)
84 0
|
人工智能
Practical Skill Test——AT
题目描述 We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j). The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.
109 0
PAT (Advanced Level) Practice - 1055 The World‘s Richest(25 分)
PAT (Advanced Level) Practice - 1055 The World‘s Richest(25 分)
110 0
在哪儿进行 Code China 建议反馈|Code China
之前写了很多关于 Code China 的介绍文章,以及部分操作教程,但是对于如何反馈使用过程中遇到的 Bug、建议等,并未提及,导致很大一部分同学不知道去哪儿反馈建议。接下来分别介绍如何通过 Issue、微信群两种方式进行建议反馈。
203 0
成功解决Command &quot;python setup.py egg_info&quot; failed with error code 1 in C:\Users\AppData\
成功解决Command &quot;python setup.py egg_info&quot; failed with error code 1 in C:\Users\AppData\
|
PHP 开发工具
|
PHP 开发工具