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


目录
相关文章
|
消息中间件 Java Linux
rocketmq linux注册服务开机启动配置
rocketmq linux注册服务开机启动配置
797 1
|
2月前
|
人工智能 安全 Ubuntu
保姆级教程 | 在Ubuntu上部署Claude CodeUI全过程
Claude Code Plan Mode 是 Anthropic 推出的智能编程助手功能,采用只读分析模式,保障代码安全的同时提供AI驱动的项目规划与风险评估。该模式平均每周为开发者节省27小时,显著提升开发效率与项目成功率,是AI编程领域的重要创新。
770 10
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的学生学籍管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的学生学籍管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
237 1
|
SQL 存储 关系型数据库
关系型数据库SQLserver添加新列
【8月更文挑战第4天】
421 9
|
人工智能 安全 机器人
AppFlow:将Kimi大模型加入钉钉群聊
使用AppFlow将Kimi大模型(基于Moonshot)集成到钉钉的步骤概览: 1. 在AppFlow控制台创建连接流,选择钉钉机器人,触发事件为收到文本消息。 2. 添加智谱AI的“使用prompt对话大模型”动作,配置API KEY。 3. 配置模型推理后消息的发送回钉钉的动作,插入变量表示模型结果。 4. 配置Webhook地址和加签(或IP白名单),在钉钉群聊中添加机器人,启用Outgoing机制,填写AppFlow的Webhook地址。 5. 通过@机器人在群聊中与Kimi模型进行交互。 这个教程展示了如何通过AppFlow将AI模型无缝接入钉钉,提升办公效率。
718 5
|
人工智能 关系型数据库 分布式数据库
AI与云数据库的深度结合:黄铭钧院士点赞PolarDB,引领云数据库2.0时代
最近,阿里云PolarDB开发者大会的举办引起了广泛关注,中国科学院外籍院士、世界级数据库专家黄铭钧在阿里云PolarDB开发者大会上表示,AI与云数据库的深度结合是数据库发展的必然趋势。他点赞以PolarDB为代表的中国数据库正在引领全球云原生数据库的发展。那么本文就来简单聊聊AI与云数据库的深度结合,引领云数据库2.0时代,以及院士点赞国产数据库的意义和数据库产业突破的重要性和前景。
699 2
AI与云数据库的深度结合:黄铭钧院士点赞PolarDB,引领云数据库2.0时代
|
编译器 C语言 C++
从C语言到C++_23(多态)抽象类+虚函数表VTBL+多态的面试题(上)
从C语言到C++_23(多态)抽象类+虚函数表VTBL+多态的面试题
196 0
|
机器学习/深度学习 索引 Python
【Python·问题解决】IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
【Python·问题解决】IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
【Python·问题解决】IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
|
Java 数据挖掘 开发工具
34-内存泄露MAT工具看本文就够了
通过本篇文章的讲解,大家对于MAT工具的使用有了一定的理解和参考,后续遇到内存泄露、内存卡顿、大对象数据分析都可以直接通过MAT精准确定,更好的优化我们的项目。
1736 0
|
小程序 开发者
微信小程序如何跳转到外部小程序
微信小程序如何跳转到外部小程序
849 0