这是我编写的代码,现在为您提供示例输入和输出,只是为了进一步解决我的问题
样本输入
2(t的值,是测试用例)
3(输入数量)
2 4 2
3
0
2
3
我的输出为
1
1
我应该得到
的输出 1
0
#include<iostream>
#include<vector>
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
vector<ll int> x;
ll int n;
cin >> n;
ll int i, ent;
for (i = 0; i < n; i++)
{
cin >> ent;
x.push_back(ent);
}
vector<ll int>::iterator y,z;
for(y=(x.begin());y!=(x.end()-1);y++)
for (z = (x.begin() + 1); z != (x.end()); z++)
{
int count=0;
if (*y + *z == *y * (*z))
count++;
}
cout << count<<endl;
}
return 0;
}
更新的代码:但仍然存在相同的问题
#include<iostream>
#include<vector>
#define ll long long
int main()
{
int t;
std ::cin >> t;
while (t--)
{
std :: vector<ll int> x;
ll int n;
std :: cin >> n;
ll int i, ent;
for (i = 0; i < n; i++)
{
std :: cin >> ent;
x.push_back(ent);
}
std::vector<ll int>::iterator y,z;
int count = 0;
for(y=(x.begin());y!=(x.end()-1);y++)
for (z = (x.begin()+1); z != (x.end()); z++)
{
if (*y + *z == *y * (*z))
count++;
}
std ::cout << count<< std ::endl;
}
return 0;
}
答案 0 :(得分:0)
std::count
与本地count
之间的名称冲突。删除using namespace std;
,您将收到更有意义的错误消息:作为未知标识符count
,因为count
应该在循环外部声明:
int count=0;
for(y=(x.begin());y!=(x.end()-1);y++)
for (z = (x.begin() + 1); z != (x.end()); z++)
{
if (*y + *z == *y * (*z))
count++;
}
std::cout << count << std::endl;
注意:std::cout << function_name
输出1,并进行隐式布尔转换。
注意:代码本身的逻辑可能仍然存在问题
答案 1 :(得分:0)
您的第一个问题是您要访问变量count
的范围之外。它在内部for()
循环的范围内声明,并且您尝试在其外部打印。将其声明移至for()
循环上方
现在,回答您的问题。在while()
循环的第二次迭代中(向量0、2、3),
在for()
循环的其中之一迭代中,*x
和*z
等于2。为避免将z初始化为z = y + 1
答案 2 :(得分:0)
您编写的代码应该产生该输出(如果它甚至符合...),g ++也不会编译它,因为您在创建循环后引用了count。count的作用域是该循环,因此它不存在外。无论如何,我假设您使用的是某些编译器开关或允许这样做的编译器。
您可以有效地制作一个数字列表,并在其中循环遍历,以检查所编码的数学关系(即,两个数字加起来等于两个数字相乘的乘积。)您的第一个循环从第一个值到倒数第二个值(因为您根据end()-1)检查是否相等,所以我不确定这是否是您真正想要的。您的第二个循环从第二个值开始,然后一直到最后一个值。因此,在第二个测试用例中,第二个数字为2和2将用于参数* y,然后它是第二个循环中的第一个值,因此您要对2和2以及2 + 2 == 4进行检查== 2 * 2,因此检查通过并增加计数。尝试下面的代码以了解您的工作(请注意,我从循环内部删除了count声明,并将其移到y和z声明的上方,这是我使用系统进行编译所必需的) :
{
cout << "y:" << *y <<" z:" << *z << endl;
if (*y + *z == *y * (*z)){
cout << "match " << *y << " " << *z << endl;
count++;
}
}