Download presentation

Presentation is loading. Please wait.

Published byAyla Pointer Modified about 1 year ago

1

2
Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix. The operations can be: ◦ Rotate by X degree, X = 90, 180, or 270 ◦ Reflect across the x-axis. ◦ Reflect across the y-axis.

3
Rotate by 90 degree We process all the numbers in the original matrix one by one, using a for loop. Let (j1, i1) be the coordinate of a number. #1 is at (0, 0), after rotation of 90 degree clockwise, it will be at index (2, 0) j1 i1i2 j2 i2 j2

4
#2 is at (1, 0), after rotation of 90 degree clockwise, it will be at index (2, 1) #3 is at (2, 0), after rotation of 90 degree clockwise, it will be at index (2, 2) Do you see any pattern? - Rotation 180 (clockwise)= 2 x Rotation 90 (clockwise). - Rotation 270 (clockwise)= 3 x Rotation 90 (clockwise). Hence, we only need to know 90 degree rotation clockwise

5
void rotate(int degree) { int temp[size][size]; while (degree > 0) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[j][size i] = matrix[i][j]; }

6
There is also a pattern for reflection Reflect by X-axis Reflect by Y-axis j1 i1 j2 i2

7
Do you see any pattern? j1 i1 j2 i2 void reflectX() { int temp[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[i][j] = matrix[size i][j]; }

8
Given a group of people with different heights and weights, determine the tallest and shortest person, compute their BMI You don’t really need sorting in this problem, just compare which person is the tallest and shortest, then compute their BMI.

9
tallName, tallHeight=-1, tallWeight; shortName, shortHeight=1e9, shortWeight; for(i=0; i

10
For 2 decimal points, you can use C-style printf double getBMI() { return (double)weight / (double) (height * height / ); } printf(“%.2f”, answer); Format the answer with 2 decimal places in its decimal representation

11
Given a square of NxN with trees occupy cells. Find the length of largest square with no tree Find the number of squares having length S and exactly k trees inside

12
Read & store input: ◦ Tree data: Create array NxN to store all the position of trees Exhaustive search! 12

13
Try all possible sizes of the square that we want to test (the size can be from 1 to N) Try all possible starting point of the square, i.e. by using nested loop (x, y) to fix the upper-left coordinate of the square. Check whether the square with upper-left coordinate (x, y) and fixed size has no trees.

14
14

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google