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:

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

4 3 3 6 2 5 10

Sample Output 1

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秒后,看谁走的远?

思路

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

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

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

题解

小码匠

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

官方题解

  • 这个是官方给的题解,利用循环做处理,感觉也有些绕。
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";
    }
}

小码匠二次解

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

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

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

复盘

心态:

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

待补知识点

  • 关于函数需要深入学习
    • 给老码农安排个活,帮我整理下知识点和学习资料
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码匠和老码农

喜欢作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值