栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

AtCode ABC249 - A - Jogging

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 << "Takahashin";
    } else if (takahashi < aoki) {
        cout << "Aokin";
    } else {
        cout << "Drawn";
    }
}
小码匠二次解

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

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

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";
    }
}
复盘 心态:
  • 每次做第一题时,心态都有些急,难免思路就会不清晰。其实精心梳理下,这道题很快就能解决。
  • 函数:重复的处理要编写函数,提高程序的扩展性。
    • 老码农叮嘱我:不要为了写代码而写代码,每一次动手敲都要有提高,可以是技术,可以是思维,总之要有提升。
待补知识点
  • 关于函数需要深入学习
    • 给老码农安排个活,帮我整理下知识点和学习资料
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873154.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号