為甚麼執行的結果答案都錯
[C++] 純文本查看 復制代碼 #include<iostream>
#include<time.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie( NULL );
int t=0;//宣告變數
int m=0,n=0,k=0;
cin>>t;//讀入測資數量
int ans[t-1];
for(int times=0,ans_e=0;times<t;times++,ans_e++)//多測資輸入
{
cin>>m>>n>>k;//m代表y軸,n代表x軸,k代表z軸
int rice[m][n],g=0,h=0,i=0;
for(int e=0;e<m;e++)//讀入測資
{
for(int f=0;f<n;f++)
{
cin>>rice[e][f];
}
}
for(int e=0;e<m;e++)//判斷是否為1,如果是1,將上下左右都編為2
{
for(int f=0;f<n;f++)
{
if(rice[e][f] == 1)
{
rice[e][f]=2;
//cout<<"rice["<<e<<"]["<<f<<"]"<<endl;
rice[e-1][f]=2;
//cout<<"rice["<<e-1<<"]["<<f<<"]"<<endl;
rice[e][f-1]=2;
//cout<<"rice["<<e<<"]["<<f-1<<"]"<<endl;
if(rice[e+1][f] != 1)
{
rice[e+1][f]=2;
//cout<<"rice["<<e+1<<"]["<<f<<"]"<<endl;
}
if(rice[e][f+1] != 1)
{
//cout<<"rice["<<e<<"]["<<f+1<<"]"<<endl;
rice[e][f+1]=2;
}
i=k-3;
}
else //if(rice[e][f] == 0 && rice[e][f] != no)
{
rice[e][f]=0;
}
}
}
for(int e=0;e<m;e++)//判斷有哪些是2
{
for(int f=0;f<n;f++)
{
if(rice[e][f]==2)
{
h++;
}
else
{
g=(m*n)-h;
}
//cout<<"rice["<<e<<"]["<<f<<"]"<<endl;
//cout<<"g="<<g<<endl;
//cout<<"h="<<h<<endl;
}
//cout<<endl;
}
//cout<<i<<endl;
//cout<<(g*k)<<endl;
//cout<<(i*h)<<endl;
ans[ans_e]=(g*k)+(i*h);//將最後答案計算出來,儲存於陣列中
}
for(int e=0;e<t;e++)//顯示答案
{
cout<<ans[e]<<endl;
}
cout<<clock();
return 0;
}
|