我的逻辑对于“Ebony and Ivory - CodeForces 633A”问题是否正确?

问题描述 投票:0回答:1

**问题是: 但丁正在与“救世主”进行战斗。在他用剑与它战斗之前,他需要打破它的盾牌。他有两把枪,乌木枪和象牙枪,每把都能够进行任意非负数的射击。

每击中护盾的子弹,乌木都会造成a单位的伤害,而象牙会造成b单位的伤害。为了打破护盾,但丁必须造成恰好 c 个单位的伤害。看看这是否可行。**

    #include <bits/stdc++.h>
    using namespace std;

    int main() {
    int a, b, c;
    cin >> a >> b >> c;
    int remaining_damage = c - a;
    
    if (remaining_damage % b == 0 ) {
    cout << "Yes\n";
    } else {
    cout << "No\n";
    }
    
    return 0;
    }

在 vs code 中,它显示正确,但在代码中强制显示输出:

c++ if-statement conditional-statements logic
1个回答
0
投票

你的逻辑不正确。您只需检查乌木(

a
单位)的一发射击,然后检查剩余的伤害(
c - a
)是否可以由象牙(
b
)造成。这并没有涵盖两把枪的所有可能的射击组合。

您可以使用循环迭代一把枪的可能射击次数来解决此问题,然后检查另一把枪是否可以造成剩余伤害。

尝试这样的事情:

#include <bits/stdc++.h> // "Okay" since saves some typing during competitive programming competitions. 

using namespace std; // "Okay" since saves some typing during competitive programming competitions.

int main() {
    ios::sync_with_stdio(false);  // See: https://usaco.guide/general/fast-io?lang=cpp
    cin.tie(nullptr); // See: https://usaco.guide/general/fast-io?lang=cpp
    int a, b, c;
    cin >> a >> b >> c;
    for (int i = 0; i * a <= c; ++i) {
        if ((c - i * a) % b == 0) {
            cout << "Yes" << '\n';
            return 0;
        }
    }
    cout << "No" << '\n';
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.