缩短代码

我正在构建一个程序,我正在尝试使我的代码更短。 它只是一个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; } 

你应该做类似下面的事情,嵌套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; } } } 

你需要考虑循环的用途。

真的,这对于重构来说是微不足道的。 你有完全相同的代码重复16次。 所以:

 for (int k = 0; k < 16; k++) { // your inner, repeating loop here } 

这是较短的版本。 如果你发现有用,试试吧

 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; } } } 

如果不完全理解你在做什么,你可以添加另一个循环:

 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; } } } 

出于好奇,你试过这个,

  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; } } } 

请忽略缩进。 我的观点是将循环嵌套在另一个循环中。 它应该失败的任何理由?