Teaching how to draw

HackerMan has brought a new drawing book for his child, which consists only of geometric shapes. Its consists of lessons where the child has to make drawings using geometric shapes. The first lesson is based on how to use squares to build different objects.

You are task is to help HackerMan in teaching one part of this lesson. You have to help him in determining, given S number of squares, how many distinct rectangles you can build out of that.

Two rectangles are considered different if none of them can be rotated and moved to obtain the second one. During rectangle construction, the child can neither deform the squares nor put any squares upon any other ones.

Note: All the squares are of the same configuration.

Example:

Input:  s = 4
Output: 5

Approach

Java

public class Draw {
    public static void main(String[] args) {

        int s = 4;
        System.out.println(draw(s));

    }

    static int draw(int s) {
        int cnt = 0;
        for (int i = 1; i <= s; i++) {
            for (int j = i; j <= s; j++) {
                if (i * j <= s)
                    cnt++;
            }
        }
        return cnt;
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

int draw(int s)
{
    int cnt = 0;
    for (int i = 1i <= si++)
    {
        for (int j = ij <= sj++)
        {
            if (i * j <= s)
                cnt++;
        }
    }
    return cnt;
}
int main()
{

    int s = 4;
    cout << draw(s<< "\n";

    return 0;
}


No comments:

Post a Comment