我正在构建一个程序,我正在努力让我的代码更短一些。它只是一个for循环重复15次。 有人可以告诉我他们将如何做到并解释他们为什么这样做。 任何想法?
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;
}
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;
}
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;
}
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;
}
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=5;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=6;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=7;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=8;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=9;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=10;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=11;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=12;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=13;
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;
}
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=14;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][14]=counter;
}
答案 0 :(得分:5)
您应该执行以下操作,嵌套for循环。
for (i=0; i<1296; i++)
{
for(int k=0; k<15; k++)
{
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=k;
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;
}
}
}
答案 1 :(得分:2)
您需要考虑 循环的用途。
真的,重构是微不足道的。你有完全相同的代码重复16次。所以:
for (int k = 0; k < 16; k++) {
// your inner, repeating loop here
}
答案 2 :(得分:2)
这是较短的版本。如果你觉得它很有用,试试吧,
for (i=0; i<1296; i++)
{
for (int k=0; k<15; ++k)
{
s[0]=0;
s[4]=k;
for (j=0; j<1296; j++)
{
counter = 0;
if (remain[j][0]!=-1)
{
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][k]=counter;
}
}
}
答案 3 :(得分:2)
如果不完全理解你在做什么,你可以添加另一个循环:
for (int i=0; i<1296; i++)
{
for (int k = 0; k < 15; ++k)
{
for (int j=0; j<1296; j++)
{
if (remain[j][0]!=-1)
{
counter = 0;
s[0]=0;
s[4]=k;
feed(poss[i], remain[j], f);
if (f[0]==s[0] && f[4]==s[4])
{
counter++;
}
}
table[i][k]=counter;
}
}
}
答案 4 :(得分:1)
出于好奇,你试过这个吗,
for (i=0; i<1296; i++)
{
for (k =0; k< 15; k++)
{
for (j=0; j<1296; j++)
{
counter = 0;
s[0]=0;
s[4]=k;
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;
}
}
}
请忽略缩进。我的观点是将循环嵌套在另一个循环中。它应该失败的任何理由?