我建立一個多頭奶牛&計劃在C和我有一個代碼,我不得不重複幾次(14確切= 14點的可能性),我想提出的我正在粘貼的代碼更短。有人可以幫我解釋一下怎麼做嗎?重複的代碼,需要把它縮短
//------------------------------0 B 0 C----------------------------------//
for (i=0; i<1296; i++)
{
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=0;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][0]=counter;
}
//------------------------------0 B 1 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=1;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][1]=counter;
}
//------------------------------0 B 2 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=2;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][2]=counter;
}
//------------------------------0 B 3 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=3;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][3]=counter;
}
//------------------------------0 B 4 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=4;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][4]=counter;
}
//------------------------------1 B 0 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=1;
s[4]=0;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][5]=counter;
}
//------------------------------1 B 1 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=1;
s[4]=1;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][6]=counter;
}
//------------------------------1 B 2 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=1;
s[4]=2;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][7]=counter;
}
//------------------------------1 B 3 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=1;
s[4]=3;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][8]=counter;
}
//------------------------------2 B 0 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=2;
s[4]=0;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][9]=counter;
}
//------------------------------2 B 1 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=2;
s[4]=1;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][10]=counter;
}
//------------------------------2 B 2 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=2;
s[4]=2;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][11]=counter;
}
//------------------------------3 B 0 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=3;
s[4]=0;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][12]=counter;
}
//------------------------------4 B 0 C----------------------------------//
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=4;
s[4]=0;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][13]=counter;
}
怎麼樣的方法包裝呢? –
我該怎麼辦呢? –
當然,你用來學習C的資源談到了如何編寫一個帶參數的函數。你的14次重複只能有不同的數字! – rodrigo