Last active
December 25, 2022 13:40
-
-
Save QuiltMeow/21a27e09c51c799a3e145f3a4edbde96 to your computer and use it in GitHub Desktop.
M、N Star Print Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class StarPrintHelper { | |
private: | |
static bool shouldPrintStar(int current, int line, int direction, int j) { | |
for (int i = 1; i <= direction; ++i) { | |
if (i % 2 == 0) { | |
if (j == line * i - current - i + 2) { | |
return true; | |
} | |
} | |
else { | |
int convert = i - 1; | |
if (j == line * convert + current - convert) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
static bool shouldBreak(int current, int line, int direction, int count) { | |
if (current == 1 && count > direction / 2) { | |
return true; | |
} | |
else if (current >= line) { | |
if (direction % 2 == 0 && count >= direction / 2) { | |
return true; | |
} | |
else if (count > direction / 2) { | |
return true; | |
} | |
} | |
else if (count >= direction) { | |
return true; | |
} | |
return false; | |
} | |
protected: | |
public: | |
static void printUniversalStar(int line, int direction, bool type) { | |
if (direction <= 0) { | |
return; | |
} | |
if (line <= 0) { | |
return; | |
} | |
if (line == 1) { | |
for (int i = 1; i <= direction; ++i) { | |
std::cout << "*"; | |
} | |
std::cout << std::endl; | |
return; | |
} | |
if (type) { | |
for (int i = 1; i <= line; ++i) { | |
for (int j = 1, count = 0; ; ++j) { | |
if (shouldPrintStar(i, line, direction, j)) { | |
std::cout << "*"; | |
if (shouldBreak(i, line, direction, ++count)) { | |
break; | |
} | |
} | |
else { | |
std::cout << " "; | |
} | |
} | |
std::cout << std::endl; | |
} | |
} | |
else { | |
for (int i = line; i >= 1; --i) { | |
for (int j = 1, count = 0; ; ++j) { | |
if (shouldPrintStar(i, line, direction, j)) { | |
std::cout << "*"; | |
if (shouldBreak(i, line, direction, ++count)) { | |
break; | |
} | |
} | |
else { | |
std::cout << " "; | |
} | |
} | |
std::cout << std::endl; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment