Making code shorter

后端 未结 5 509
感动是毒
感动是毒 2021-01-26 16:28

I\'m currently building a program and I\'m trying to make my code a bit shorter. It\'s just a for-loop repeating itself 15 times. Can someone show me how they would do it and ex

相关标签:
5条回答
  • 2021-01-26 16:46

    You need to think about what loops are for.

    This is trivial to refactor, really. You have the exact same code repeating 16 times. So:

    for (int k = 0; k < 16; k++) {
        // your inner, repeating loop here
    }
    
    0 讨论(0)
  • 2021-01-26 16:48

    Here is the shorter version. Try it if you find it useful,

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-26 16:50

    Without fully understanding what you're doing you can add another loop:

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-26 16:54

    You should do something like below, nesting the for loops.

    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;
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-26 17:07

    Out of curiosity, have you tried this,

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

    Please, ignore the indentation. My point is nesting the loop in another one. Any reason that it should fail?

    0 讨论(0)
提交回复
热议问题