如何用总和获得正产品的负面因素
how to get the negative factors of a positive product with a sum
本文关键字:何用总 更新时间:2023-10-16
我可能在标题中没有很好地解释这一点,因为我真的不知道如何用标题的长度来解释它,所以我为此道歉。无论如何,这是我当前的代码:
// Gets the product that is used to get the factors
cout << "Enter a number that you want to be factored" << endl;
cin >> y;
system("cls");
// Gets the sum from the user that the two factors need to add up to
cout << "Input the sum that is needed" << endl;
cin >> neededSum;
secondary = y;
system("cls");
// Lists the factors that add up to the specified sum
cout << "The numbers that add up to " << neededSum << " are:" << endl;
for (int i = 0; i < 100; i++)
{
x++;
increment++;
y = secondary / x;
product = x * y;
if (product == secondary)
{
sum = x + y;
if (sum == neededSum)
{
cout << x << " and " << y << endl;
}
}
}
基本上,它需要一个产品列出该产品的所有因素。然后,它将所有因素相加,直到找到加起来等于指定总和的因子。
但是,如果指定的总和是负数,例如"-11",它将不起作用,因为"28"(示例)的因数是 4 和 7,而不是 -4 和 -7。我需要找到一种方法来解决这个问题,以便它知道何时必须是 -4 和 -7 而不是正反向。
提前感谢您的帮助。
假设 x 和 y 是负数:
if (product == secondary)
{
sum = x + y;
if (sum == neededSum)
{
cout << x << " and " << y << endl;
}
else if (-sum == neededSum)
{
cout << -x << " and " << -y << endl;
}
}
相关文章:
- 用C++20 fmt限制结果的总大小
- 如何用数字处理log(0)
- 如何用转义符替换字符串中的所有特殊字符
- 如何用参数值调用函数(仅在运行时已知)
- 如何用RISC-V GD32VF103CBT6开发板卸载精确的ADC过采样
- 如何用for循环在c++中生成单词三角形
- 如何用C++编写BFS函数
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 当对话框被接受时,如何用参数调用槽
- 我不明白这段代码是如何对这个pythonlist()进行排序的,也不明白如何用C++中的向量来重现它
- 如何计算常量字符**中的总字符数?
- 如何用前导零填充数组?
- 如何用 2 个对象过度引导运算符
- 如何用C / C++编写钢琴程序
- C++:如何用单个命令替换复杂的迭代?
- 如何用文本填充 std::vector<int64_t>
- 如何用 C 编译,但如果使用任何C++,则会抛出错误?
- 如何用 gcc 或 clang 模仿_mm256_loadu_epi32?
- C++程序的总空间使用量
- 如何用正确的路径建立提升