英文:
How to make a method that finds non-Pythagorean-triples from vector?
问题
我正在尝试学习C++并完成一个学习任务:
这是任务:
勾股定理三元组是一组三个整数a、b和c,满足a^2 + b^2 = c^2。编写一个函数,它接受一个std::vector作为输入,并返回一个std::vector,其中移除了所有的勾股定理三元组。例如,给定包含{6, 25, 5, 3, 7, 24, 4, 23}的输入,函数应返回{6, 23},因为3^2 + 4^2 = 5^2和7^2 + 24^2 = 25^2是勾股定理三元组。
我在尝试获得输出{6, 23}。
这是我目前的代码。
```cpp
#include <iostream>
#include <vector>
#include <cmath>
bool isPythagorasTriplet(int a, int b, int c)
{
if (pow(a, 2) + pow(b, 2) == pow(c, 2)){
return true;
} else return false;
}
std::vector<int> pythagorasTriplet(std::vector<int> input)
{
std::vector<int> notTriplets;
bool isTriplets = false;
for (int i = 0; i < input.size(); i++){
isTriplets = false;
for (int j = 0; j < input.size(); j++){
for (int k = 0; k < input.size(); k++){
if (i == j || i == k || j == k){
continue;}
if (isPythagorasTriplet(input[i], input[j], input[k])){
isTriplets = true;
// notTriplets.push_back(input[i]);
// notTriplets.push_back(input[j]);
// notTriplets.push_back(input[k]);
}
}
}
if (isTriplets == false) notTriplets.push_back(input[i]);
}
return notTriplets;
}
int main()
{
std::vector<int> input = {6, 25, 5, 3, 7, 24, 4, 23};
std::vector<int> noTriplets = pythagorasTriplet(input);
for (int i = 0; i < noTriplets.size(); i++){
std::cout << noTriplets[i] << std::endl;
}
return 0;
}
英文:
Im trying to learn c++ and complete a learning task:
This is the task:
A Pythagorean triplet is a set of three integers, a, b, and c, for which it holds true that a^2 + b^2 = c^2. Write a function that takes an std::vector as input and returns an std::vector where all the Pythagorean triplets are removed. For example, given an input consisting of {6, 25, 5, 3, 7, 24, 4, 23}, the function should return {6, 23} because 3^2 + 4^2 = 5^2 and 7^2 + 24^2 = 25^2 are Pythagorean triplets.
Im having trouble getting the output {6 ,23}
This is my code so far.
#include <iostream>
#include <vector>
#include <cmath>
bool isPythagorasTriplet(int a, int b, int c)
{
if (pow(a, 2) + pow(b, 2) == pow(c, 2)){
return true;
} else return false;
}
std::vector<int> pythagorasTriplet(std::vector<int> input)
{
std::vector<int> notTriplets;
bool isTriplets = false;
for (int i = 0; i < input.size(); i++){
isTriplets = false;
for (int j = 0; j < input.size(); j++){
for (int k = 0; k < input.size(); k++){
if (i == j || i == k || j == k){
continue;}
if (isPythagorasTriplet(input[i], input[j], input[k])){
isTriplets = true;
// notTriplets.push_back(input[i]);
// notTriplets.push_back(input[j]);
// notTriplets.push_back(input[k]);
}
}
}
if (isTriplets == false) notTriplets.push_back(input[i]);
}
return notTriplets;
}
int main()
{
std::vector<int> input = {6, 25, 5, 3, 7, 24, 4, 23};
std::vector<int> noTriplets = pythagorasTriplet(input);
for (int i = 0; i < noTriplets.size(); i++){
std::cout << noTriplets[i] << std::endl;
}
return 0;
}
答案1
得分: 0
你的代码应该找到列表中的每个数字,它们与列表中的其他数字不构成毕达哥拉斯三元组。然而,当评估是否将 input[i]
包含在列表中时,它仅评估 input[i]
是否在传递给 isPythagorusTriplet
的两个其他数字的第一个位置上形成毕达哥拉斯三元组。因此,它会错过 input[i]
是毕达哥拉斯三元组的斜边的情况。
将 isPythagorusTriplet
的主体更改为 return a*a + b*b == c*c || b*b + c*c == a*a || c*c + a*a == b*b;
可以解决这个问题。(还有一些可以优化的地方,以避免冗余检查。)
英文:
Your code is supposed to find each number in the list that does not form a Pythagorean triple with any other numbers in the list. However, when evaluating whether or not to include input[i]
in the list, it evaluates only whether input[i]
forms a Pythagorean triple in the first position as passed to isPythagorusTriplet
with two other numbers. It therefore misses cases where input[i]
is the hypotenuse of a Pythagorean triple.
Changing the body of isPythagorusTriplet
to return a*a + b*b == c*c || b*b + c*c == a*a || c*c + a*a == b*b;
remedies this. (There are also optimizations that can be made to avoid redundant checks.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论