Блог пользователя rasinrohit

Автор rasinrohit, история, 5 лет назад, По-английски

I am relatively new to Competitive Programming as you can tell by my Rating.
I have started feeling that my code is more of a patchwork.
I run the program, see the answer is off by some digits and change that by hard coding.
I don't focus much on the logic behind the wrong answer.
Also , even simple programs sometimes extend to over 75-80 lines of code which seems very inefficient .
Below mentioned is an example of my code.
It's the second problem from Educational Codeforces Round 85.
I was not able to solve it within the 2:00 hours, so I don't know if it is correct or not.But, that doesn't matter. I just want to know what coding practices I should use inorder to clean this mess.,
Am I the only one who is facing this issue or it's common with new programmers ?

I am using JAVA 14

~~~~~

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Collections;

public class practice
{
    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);
       int sizeOfArray=0,lotOfMoney;
       ArrayList<Long> savings = new ArrayList<Long>();
       int t = input.nextInt();
       Long sum=(long)0;
       double avg=0.0;
       int loopControler;
       for(int i=0;i<t;i++)
       {
         savings.clear();
         sizeOfArray = 0;
         lotOfMoney = 0;
         sizeOfArray =  input.nextInt();
         loopControler = sizeOfArray;
         lotOfMoney  = input.nextInt();
         for(int j=0;j<sizeOfArray;j++)
         {
          savings.add(input.nextLong());
         }
         Collections.sort(savings , Collections.reverseOrder());
         //System.out.println(savings);

         for(int o=0;o<sizeOfArray;o++)
         {
          for(int l=0;l<loopControler;l++)
          {
              sum = savings.get(l) + sum;
          }
          avg = (double)sum/loopControler;
          //System.out.println("averag is : "+avg);
          if(avg>=lotOfMoney)
          {
              System.out.println(loopControler);
              avg=0;
              sum =(long)0;
              break;
          }
          else if(savings.get(0) < lotOfMoney)
          {
              System.out.println("0");
              break;
          }

          else
          {
              avg =0 ;
              sum =(long)0 ;
              loopControler--;
          }
         }
       }
    }
}

~~~~~

PS : If, these types of questions are not supposed to be asked on Code Forces and are against it's policies , please let me know, I'll remove/change it
Thanks

  • Проголосовать: нравится
  • +10
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

At this point, it might help to try the following approach. When you are solving an algorithmic problem, first, come up with the idea of the solution. Write a short version in natural language or pseudocode. Importantly, prove that it works. Only then, start implementing it in your programming language.

If the implementation strays too far from your short version, it means either the short version is missing critical details, or you are about to introduce a bug in the implementation. So, fix or expand that part in your short version, and prove again that it works after the change. The goal is to ever be able to explain what every line of your program should do and why. When you can't really say why a part of the code is there, why would that code help solve your problem?

If the implemented solution produces a different answer than expected, it is almost never a good idea to hard code the right answer. This way, most of the time, you just conceal the problem from yourself, making it necessary to come up with another, possibly harder, test case to find it again. Instead, take the example and follow the logic of your algorithm manually: first with the short version, then with the implementation. If the short version produces an incorrect answer, it's probably a bug in the solution idea. In that case, the patch should again produce a general solution that covers all cases, not only the example, unless you can prove the example is indeed a special case. Otherwise, if the implementation produces an incorrect answer, check the correspondence between the short version and the implementation. Trace the program state in a debugger or use debug output if necessary.

Lastly, after the contest, you can open the solutions for a particular problem, filter them by your favorite language, and see how others implement it. Not every solution will be clean and readable, but you will surely find some excellent implementations at the top.

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    Thanks, that's a great tip !
    I have already started following this and I feel much more confident in writing code.

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +10 Проголосовать: не нравится

Just read others solutions which you think are shorter. Do about 50 relatively easy implementation problems by looking at other's code. You'll get better; if you don't, do another 50. Remember, only look for code in others submissions, do get the sketch of solution yourself.