Baekjoon - Printing Stars

Ji Kim·2020년 8월 10일
0

Algorithm

목록 보기
1/34

Link - Baekjoon Online Algorithm Judge

Before Beginning

Pattern printing is often used as a practice to get oneself used to the very basics of coding. (i.e - _for-loops, if-statement, etc.) Here are some of the solution codes for the Baekjoon's Star Printing Problem
#1 - 9.

Star Printing #1

Explnation

Given input N, print a star at 1st line, two stars at 2nd line, and n-stars at n-th line.

Input

5

Output

*
**
***
****
*****

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

Star Printing #2

Explnation

Given input N, print a right-aligned star at 1st line, two stars at 2nd line, and n-stars at n-th line.

Input

5

Output

    *
   **
  ***
 ****
*****

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

Star Printing #3

Explnation

Given input N, print N star at first line, N-1 star at second line, and a star at N-th line.

Input

5

Output

*****
****
***
**
*

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = N; i > 0; i--)
    {
        for (int j = i; j > 0; j--)
        {
            cout << "*";
        }

        cout << endl;
    }
}

Star Printing #4

Explnation

Given input N, print right-aligned N star at first line, N-1 star at second line, and a star at N-th line.

Input

5

Output

*****
 ****
  ***
   **
    *

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = N; i > 0; i--)
    {
        for (int j = 1; j <= N - i; j++)
        {
            cout << " ";
        }

        for (int k = i; k > 0; k--)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

Star Printing #5

Explnation

Given input N, print a star at 1st line, 3-stars at 2nd line, (2N) - 1 stars at N-th line.

Input

5

Output

    *
   ***
  *****
 *******
*********

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = 1; i <= N; i++)
    {
        // space
        for (int j = 1; j <= N - i; j++)
        {
            cout << " ";
        }

        for (int k = 1; k <= i * 2 - 1; k++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

Star Printing #6

Explnation

Given input N, print (2N) - 1 stars at 1st line, (2N) - 3 stars at 2nd line, and a star at N-th line.

Input

5

Output

*********
 *******
  *****
   ***
    *

Solution

#include <iostream>
using namespace std;

int N; 

int main()
{
    cin >> N; 

    for (int i = N; i > 0; i--)
    {
        for(int j = 1; j <= N - i; j++)
        {
            cout << " ";
        }

        for (int k = 1; k <= (i * 2) - 1; k++)
        {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Star Printing #7

Explnation

Analyze the given example and print the pattern below.

Input

5

Output

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= N - i; j++)
        {
            cout << " ";
        }

        for (int k = 1; k <= i * 2 - 1; k++)
        {
            cout << "*";
        }
        cout << endl;

    }

    for (int i = N - 1; i > 0; i--)
    {
        for(int j = 1; j <= N - i; j++)
        {
            cout << " ";
        }

        for (int k = 1; k <= (i * 2) - 1; k++)
        {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Star Printing #8

Explnation

Analyze the given example and print the pattern below. (restriction - print until (2N)-1th line

Input

5

Output

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = 1; i <= N; i++)
    {
        // print star
        for (int j = 1; j <= i; j++)
        {
            cout << "*";
        }

        for (int k = 1; k <= (2 * N) - (2 * i); k++)
        {
            cout << " ";
        }

        for (int n = 1; n <= i; n++)
        {
            cout << "*";
        }
        cout << endl;
    }

    for (int i = N - 1; i > 0; i--)
    {
        for (int j = i; j > 0; j--)
        {
            cout << "*";
        }
        
        for (int k = 1; k <= (2 * N) - (2 * i); k++)
        {
            cout << " ";
        }

        for (int n = 1; n <= i; n++)
        {
            cout << "*";
        }

        cout << endl;

    }
}

Star Printing #9

Explnation

Analyze the given example and print the pattern below. (restriction - print until (2N)-1th line

Input

5

Output

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

Solution

#include <iostream>
using namespace std;

int N;

int main()
{
    cin >> N;

    for (int i = N; i > 0; i--)
    {
        // space
        for (int j = 1; j <= N - i; j++)
        {
            cout << " ";
        }

        // star
        for (int k = 1; k <= (2 * i) - 1; k++)
        {
            cout << "*";
        }

        cout << endl;
    }

    for (int i = 1; i <= N - 1; i++)
    {   
        // space
        for (int j = 1; j <= (N - 1) - i; j++)
        {
            cout << " ";
        }

        // star
        for (int k = 1; k <= (2 * i) + 1; k++)
        {
            cout << "*";
        }

        cout << endl;

    }

    return 0;
}
profile
if this then that

0개의 댓글