我可能没有在标题中很好地解释这一点,因为我真的不知道如何用标题的长度来解释它,所以我为此道歉。无论如何,这是我当前的代码:
// 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 而不是正相反。
在此先感谢您的帮助。