Skip to content

Instantly share code, notes, and snippets.

@QuiltMeow
Last active December 25, 2022 13:40
Show Gist options
  • Save QuiltMeow/21a27e09c51c799a3e145f3a4edbde96 to your computer and use it in GitHub Desktop.
Save QuiltMeow/21a27e09c51c799a3e145f3a4edbde96 to your computer and use it in GitHub Desktop.
M、N Star Print Practice
#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