Giter Site home page Giter Site logo

storwell1's Introduction

Embedded Software Engineer Quiz

Please submit answers by creating a public repo on Github or Bitbucket and sharing the URL of the repo. Please do not submit answers in any other format. If you are submitting your application on our website, please use the cover letter field to include a document that contains the link to your git repo. There are two questions in this quiz; please see page 2 for the second question.

Q1) Consider that you have a rectangular piece of paper of arbitrary dimensions N by M (where N and M are positive integers). You also have a pair of scissors which can cut perfectly straight with no loss of paper. You wish to reduce the original piece of paper into a series of perfect squares of paper, making the largest possible squares, and using all of the paper provided. Write a function in C that takes the inputs N and M and returns the series of squares that can be made out of that piece of paper. No fractional squares, i.e., no square should be less than 1 in length and width.

Some examples:

  • Input: N = 6, M = 5 Output: 5x5, 1x1, 1x1, 1x1, 1x1, 1x1

  • Input: N = 1, M = 1 Output: 1x1

  • Input: N = 9, M = 9 Output: 9x9

#include <stdio.h>

void findSquares(int N, int M) {
    while (1) {
        // If N and M are equal, a square of size N or M can be cut.
        if (N == M) {
            printf("%dx%d, ", N, M);
            return;
        }

        // If either N or M is 1, the remaining paper is a series of 1x1 squares.
        if (N == 1 || M == 1) {
            int h = (N > M) ? N : M;
            for (int i = 0; i < h; i++) {
                printf("1x1, ");
            }
            return;
        }

        // Find the minimum dimension and cut a square of that size.
        int min_dim = (N < M) ? N : M;
        printf("%dx%d, ", min_dim, min_dim);

        // Update the dimensions.
        if (N < M) {
            M -= min_dim;
        } else {
            N -= min_dim;
        }
    }
}

int main() {
    int N, M;
    printf("Enter the dimensions N and M: ");
    scanf("%d%d", &N, &M);
    printf("Series of squares: ");
    findSquares(N, M);
    printf("\n");
    return 0;
}

storwell1's People

Contributors

rajatjc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.