库特<< " " ;正在改变我的答案,C++



我试图解决一些ds问题,所有的事情在算法方面都很好,但在答案方面(错误(,然后为了调试,我放了cout来检查哪里出错了。当我放cout时,答案是正确的,但没有它,答案是错误的另外,不同的在线编译器正在显示正确的答案

没有cout和Is 1:7:4(错误的ans(答案是1:7:3(正确答案(输入为2 91 0 1 0 1 1 1 10 0 1 0 1 0 0 1

#include <bits/stdc++.h>
using namespace std; 
int main()
{
int n,m;
int a,b;
int ans=0; 
int ansij=INT_MAX;
cin>>n>>m;
int arr[n+10][m+10]; 
for(int i=0;i<n;i++)
{
{ for(int j=0;j<m;j++) 
cin>>arr[i][j];
}
}

for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++) 
{ if(i==0&&j==0)
continue; 
int score=0;
if(arr[i][j]==1)
{ //cout<<arr[i][j]<<endl ;
//1
if(arr[i+1][j]==1)      
score++;
//2

if(arr[i][j+1]==1)
score++;
//3
if(arr[i-1][j]==1)      
score++;
//4
if(arr[i][j-1]==1)
score++;
//5
if(arr[i+1][j+1]==1)      
score++;
//6
if(arr[i+1][j-1]==1)
score++;
//7
if(arr[i-1][j+1]==1)      
score++;
//8
if(arr[i-1][j-1]==1)
score++;
}
//cout<<"";
//cout<<"score of i and j is ("<<i+1<<","<<j+1<<") "<<score<<endl;
if(ans<score)
{ ans=score; 
ansij=i+j;
a=i;
b=j;
//cout<<" answer update from gretest"<<endl;

}
else if(ans==score)
{ if(ansij>=(i+j))
{ ans=score;
ansij=i+j;
a=i;
b=j;
//cout<<" answer update from qual"<<endl;
}
}
//cout<<" answer in i j is"<<ans<<endl;
}
}  
cout<<a+1<<":"<<b+1<<":"<<ans;   

return 0;
}

"cout";不是问题所在。

我已经用你给定的值初始化了一个矩阵,我已经从键盘上读取了值n=2m=9

int arr[n+10][m+10]{1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1};

和cout打印的1:7:3(你说,应该是正确的答案(。

这意味着你的问题在于阅读矩阵。

相关内容

最新更新