我正在尝试比较两个输入文件中的数据,看看一组是否小于另一组
我正在尝试将一个文件中的数据与另一个文件中的数据进行比较,看看它是否“适合”另一个文件。我认为一组是“洞”,另一组是“块”。我想看看所有圆块是否都适合至少一个圆孔。如果它们都适合,我想输出“YES”。如果有一个不适合,我想输出“NO,所有圆形块至少不适合一个孔”。
这是我的代码:
bool fits = false;
for (int i=0; i<M; i++)
{
if (btype[i] == Circles)
{
CA[i] = (3.14 * (D[i] * D[i]));
}
for (int j=0; j<N; j++)
{
if (htype[j] == Circle)
{
CA[j] = (3.14 * (A[j] * A[j]));
}
if (CA[i] < CA[j])
{
fits = true;
break;
}
}
if (!fits)
{
break;
}
}
if (fits)
{
cout << "5. YES, all circle blocks fit a hole. "
<< endl;
}
else
{
cout << "5. NO, all circle blocks don't fit a hole. "
<< endl;
}
代码运行不正确。当我将一个圆圈值放入输入文件中时,程序肯定会输出“否”,但它却给出“是”。我想不通。
I'm trying to compare data from one file to another to see if it "fits" inside the other. I'm thinking of one set as "Holes" and the other as "Blocks." I'm trying to see if all circle blocks fit inside at least one circle hole. If they all fit, I want to output "YES." If one does not fit, I want to output "NO, all circle blocks do not fit into at least one hole."
Heres my code:
bool fits = false;
for (int i=0; i<M; i++)
{
if (btype[i] == Circles)
{
CA[i] = (3.14 * (D[i] * D[i]));
}
for (int j=0; j<N; j++)
{
if (htype[j] == Circle)
{
CA[j] = (3.14 * (A[j] * A[j]));
}
if (CA[i] < CA[j])
{
fits = true;
break;
}
}
if (!fits)
{
break;
}
}
if (fits)
{
cout << "5. YES, all circle blocks fit a hole. "
<< endl;
}
else
{
cout << "5. NO, all circle blocks don't fit a hole. "
<< endl;
}
The code is not acting right. When I put a circle value into the input file that should definitely make the program cout a NO, it give me a YES. I can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有 if 语句在最后一个 for 循环内进行打印。
如果您想根据一条是否不适合来打印消息,如果全部适合则打印另一条消息,您可以这样做。
You have the if statement that does the printing inside the last for loop.
If you want to print the message depending on if one doesn't fit, and if all fit then print another message you can do it like this.
由于带有 cout 的行位于具有 N 次迭代的 for 循环内,该循环位于另一个具有 for 循环内strong>M 次迭代,看起来应该打印 M * N 行
As the lines with cout are inside the for loop with N iterations, which is inside another for loop with M iterations, looks like it should print M * N lines