Codeforces и Polygon могут быть недоступны в период с 6 декабря, 22:00 (МСК) по 7 декабря, 00:00 (МСК) в связи с проведением технических работ. ×

My JAVA Code looks like a patchwork

Правка en1, от rasinrohit, 2020-04-11 10:35:24

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

Теги #java, #newbie, #new, efficiency

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский rasinrohit 2020-04-11 10:35:24 2539 Initial revision (published)