E
E
EvgenySManko2016-01-25 15:25:15
Programming
EvgenySManko, 2016-01-25 15:25:15

Rectangle problem (acmp). Why doesn't it pass?

I solved problems on the site acmp.ru, and I came across a seemingly easy problem, but my solution does not pass the test.
The bottom line is that it is required to determine the number of possible rectangles with area from A to B and perimeter from C to D. The full text of the task is acmp.ru/asp/do/index.asp?main=task&id_problem=682.
My solution is to iterate over all possible areas (from A to B) and find two factors whose product equals the current area. Next, we check these sides (multipliers) for compliance with the perimeter.

int count = 0; //счетчик
      
      for(int s = a; s <= b; s++){ //перебираем площади
        for(int x = 1; x < s; x++){ 
          if(s%x==0){ //перебираем множители
            int y = s/x;
            int p = 2*(x+y);
            if(p>=c&p<=d&x<=y){ //проверяем периметр и запрещаем повтор(x*y=y*x)
              count++;
              System.out.println(x+" "+y);
            }
          }
        }
      }

Moreover, the very first test that was given in the condition works correctly.
UPD: I solved the problem, it turns out you need to sort through sqrt (b), because the maximum size of the sides will be when the rectangle is a square

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question