本帖最後由 林宇翔 於 2014-8-25 15:07 編輯
我有加上註解
麻煩幫我看一下
[C++] 純文本查看 復制代碼 #include<iostream>
using namespace std;
int main()
{
int t=0,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;
}
return 0;
}
如何看程式執行時間
|