C++ 代码查找谁无法提供足够的糖果
假设我们有两个数字 a 和 b。阿玛尔和比马尔手中有 a 和 b 颗糖果。阿玛尔给了比马尔一颗糖果,比马尔给了阿玛尔两颗糖果,下一轮阿玛尔给了 3 颗糖果,比马尔给了 4 颗,以此类推。一直这样下去,直到他们中的一人无法给对手适当数量的糖果时为止。他们不会将从对手那里得到的糖果视为自己的。我们必须找出谁是第一个无法给对手适当数量的糖果的人。
所以,如果输入类似于 a = 7;b = 6,那么输出将是 Arnal,因为最初 Arnal 给了一颗,Bimal 给了两颗,然后 Arnal 给的三颗,Bimal 给的 4 颗,现在在这个回合中 Arnal 必须给 5 颗糖果,但只有 4 颗糖果。
步骤
要解决这个问题,我们将遵循以下步骤 -
x := square root of a if x * (x + 1) > b, then: return "Bimal" Otherwise return "Amal"
例子
让我们看看以下实现以获得更好的理解 -
#include <bits/stdc++.h> using namespace std; string solve(int a, int b){ int x = sqrt(a); if (x * (x + 1) > b) return "Bimal"; else return "Amal"; } int main(){ int a = 7; int b = 6; cout << solve(a, b) << endl; }
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输入
7, 6
输出
Amal
广告