【发布时间】:2019-09-10 11:00:41
【问题描述】:
检查二维矩阵是否对称 任务是如果矩阵对称则输出YES,否则输出NO。
我没有得到预期的结果。有人可以帮助我,请让我知道这段代码有什么问题
#include<iostream>
#include<vector>
using namespace std;
bool rev(int n)
{
int n1,d,rn=0;
n1=n;
while(n>0)
{
d=n%10;
rn=(rn*10)+d;
n/=10;
}
if(n1==rn)
{return true;}
else
return false;
}
bool XAxisSymCheck(vector<int> vect)
{
// Declaring iterator to a vector
vector<int>::iterator ptr;
for (ptr = vect.begin(); ptr < vect.end(); ptr++)
{ if(!rev(*ptr)) // reversing the elements in each element of vector to check whether its symmetric or not .. similar to palindrome
{
return false;
}
}
}
int main()
{int testcase;
cin>>testcase;
for(int k=0;k<testcase;++k)
{vector<int> rows;
bool IsSymmetric=true;
int row;
cin >> row;
// read each row and append to the "rows" vector
for (int r = 0; r < row; r++)
{
int line;
cin >> line;
rows.push_back(line);
}
if(XAxisSymCheck(rows))
{int i,j;
i=0;
j=row-1;
while(i<j) // looping through the elements of vector and checking the first element with last element , second element with the second last element and so on.
{
if(rows[i]!=rows[j])
{
IsSymmetric=false;
break;
}
i++;
j--;
}
}
else
{
IsSymmetric=false;
}
cout << (IsSymmetric ? "Yes" : "No") << endl;
}
return 0;
}
输入: 第一行包含 T - 测试用例数。 T 测试用例如下。 每个测试用例的第一行包含 N - 大小的矩阵。 接下来的 N 行包含长度为 N 的二进制字符串。
输出: 在每个测试用例的新行中打印 YES 或 NO
SAMPLE INPUT
5
2
11
11
4
0101
0110
0110
0101
4
1001
0000
0000
1001
5
01110
01010
10001
01010
01110
5
00100
01010
10001
01010
01110
SAMPLE OUTPUT
YES
NO
YES
YES
NO
Test Case #1: Symmetric about both axes, so YES.
Test Case #2: Symmetric about X-axis but not symmetric about Y-axis, so NO.
Test Case #3: Symmetric about both axes, so YES.
Test Case #4 and #5 are explained in statement.
【问题讨论】:
-
那么,你的问题是什么?
-
我没有得到预期的结果。有人可以帮助我,请让我知道这段代码有什么问题。
-
那么,哪个测试用例返回了错误的结果?
-
对于所有的测试用例,它的打印是的,我不知道为什么?你能帮帮我吗?
标签: c++ algorithm vector data-structures