前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AtCode ABC249 - A - Jogging

AtCode ABC249 - A - Jogging

作者头像
小码匠
发布2022-06-16 17:50:21
5720
发布2022-06-16 17:50:21
举报
文章被收录于专栏:小码匠和老码农

AtCode ABC249 - A - Jogging

标签

  • 分支、数学

题目地址

A - Jogging

  • https://atcoder.jp/contests/abc249/tasks/abc249_a

问题描述

Problem Statement

Takahashi and Aoki decided to jog. Takahashi repeats the following: "walk at Bmeters a second for Aseconds and take a rest for Cseconds." Aoki repeats the following: "walk at Emeters a second for Dseconds and take a rest for Fseconds." When Xseconds have passed since they simultaneously started to jog, which of Takahashi and Aoki goes ahead?

Constraints

  • 1≤A,B,C,D,E,F,X≤100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

代码语言:javascript
复制
A B C D E F X

Output

When XX seconds have passed since they simultaneously started to jog, if Takahashi goes ahead of Aoki, print Takahashi; if Aoki goes ahead of Takahashi, print Aoki; if they have advanced the same distance, print Draw.

Sample Input 1

代码语言:javascript
复制
4 3 3 6 2 5 10

Sample Output 1

代码语言:javascript
复制
Takahashi

During the first 10 seconds after they started to jog, they move as follows.

  • Takahashi walks for 4seconds, takes a rest for 3 seconds, and walks again for 3 seconds. As a result, he advances a total of (4+3)×3=21 meters.
  • Aoki walks for 6 seconds and takes a rest for 4 seconds. As a result, he advances a total of 6×2=12 meters.

Since Takahashi goes ahead, Takahashi should be printed.

题意

  • 比较简单:一个人走A秒,歇C秒,然后再走,X秒后,看谁走的远?

思路

这道题不难,不过相对以往比赛第一题,稍有难度。

我在线上参加比赛时编写的代码如下,是不是写的比较绕了。

主要是当时心态有些急,如果稍加分析,理清思路,很快就能秒掉该题。

题解

小码匠

代码语言:javascript
复制
void coder_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int a, b, c, d, e, f, x, g, h;
    cin >> a >> b >> c >> d >> e >> f >> x;
    if (x <= a + c) {
        if (x >= a && x <= a + c) {
            g = a * b;
        } else {
            g = x * b;
        }
    }
    if (x <= d + f) {
        if (x >= d && x <= d + f) {
            h = d * e;
        } else {
            h = x * e;
        }
    }
    if (x > a + c) {
        g = x / ( a + c) * a * b;
        if (x % ( a + c) >= a) {
            g += a * b;
        } else {
            g += x % ( a + c) * b;
        }
    }
    if (x > d + f) {
        h = x / ( d + f) * d * e;
        if (x % ( d + f) > d) {
            h += d * e;
        } else {
            h += x % (d + f) * e;
        }
    }
    if ( g > h) {
        cout << "Takahashi";
    } else if ( g == h) {
        cout << "Draw";
    } else {
        cout << "Aoki";
    }
}

官方题解

  • 这个是官方给的题解,利用循环做处理,感觉也有些绕。
代码语言:javascript
复制
void best_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int a, b, c, d, e, f, x;
    cin >> a >> b >> c >> d >> e >> f >> x;
    int takahashi = 0, aoki = 0;
    for (int k = 0; k < x; ++k) {
        if (k % (a + c) < a) {
            takahashi += b;
        }
        if (k % (d + f) < d) {
            aoki += e;
        }
    }
    if (takahashi > aoki) {
        cout << "Takahashi\n";
    } else if (takahashi < aoki) {
        cout << "Aoki\n";
    } else {
        cout << "Draw\n";
    }
}

小码匠二次解

这个是今天晚上做复盘时,重写的代码,比之前线上比赛时简洁多了。

主要是还学习到了新知识。(呵呵...)

代码语言:javascript
复制
void best_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    // 输入
    int a, b, c, d, e, f, x;
    cin >> a >> b >> c >> d >> e >> f >> x;

    // 定义函数
    auto calc = [&](int a, int b, int c, int x) {
        int m = x / (a + c) * a * b;
        if (x % (a + c) >= a) {
            m += a * b;
        } else {
            m += x % (a + c) * b;
        }
        return m;
    };

    // 分别计算
    int takahashi = calc(a, b, c, x);
    int aoki = calc(d, e, f, x);
    
    // 输出
    if (takahashi > aoki) {
        cout << "Takahashi";
    } else if (takahashi < aoki) {
        cout << "Aoki";
    } else {
        cout << "Draw";
    }
}

复盘

心态:

  • 每次做第一题时,心态都有些急,难免思路就会不清晰。其实精心梳理下,这道题很快就能解决。
  • 函数:重复的处理要编写函数,提高程序的扩展性。
    • 老码农叮嘱我:不要为了写代码而写代码,每一次动手敲都要有提高,可以是技术,可以是思维,总之要有提升

待补知识点

  • 关于函数需要深入学习
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小码匠和老码农 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AtCode ABC249 - A - Jogging
  • 标签
  • 题目地址
  • 问题描述
    • Problem Statement
      • Constraints
        • Input
          • Output
            • Sample Input 1
              • Sample Output 1
              • 题意
              • 思路
              • 题解
                • 小码匠
                  • 官方题解
                    • 小码匠二次解
                    • 复盘
                      • 心态:
                        • 待补知识点
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档