도형만들기1

*썸네일은 Thumbnail-Maker by oneook으로 만들었습니다.

도형만들기1 구현 내용

첫 시작은 "도형만들기1" 문제 구현 내용이다.
시작이니만큼 특별히 어려운 문제는 없었고, 필요한 부분에 대해서만 간단하게 설명해 보았다.

1291 구구단

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int s;
    int e;

    cin >> s >> e;
    while (s < 2 || s > 9 || e < 2 || e > 9)
    {
        cout << "INPUT ERROR!" << endl;
        cin >> s >> e;
    }

    if (s <= e)
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = s; j <= e; j++)
            {
                cout << j << " * " << i << " = " << setw(2) << j * i << "   ";
            }
            cout << endl;
        }
    }
    else
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = s; j >= e; j--)
            {
                cout << j << " * " << i << " = " << setw(2) << j * i << "   ";
            }
            cout << endl;
        }
    }

    return 0;
}
  • INPUT ERROR! 출력 후 재입력 받는 내용이 있어 이를 추가했다.
  • C++ 문법을 사용하고 싶어 printf("%2d")를 사용하지 않고 #include <iomanip>setw(2)를 사용했다.

1341- 구구단2

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int s;
    int e;

    cin >> s >> e;

    if (s <= e)
    {
        for (int i = s; i <= e; i++)
        {
            for (int j = 1; j <= 9; j++)
            {
                cout << i << " * " << j << " = " << setw(2) << i * j << "   ";

                if (j % 3 == 0)
                {
                    cout << endl;
                }
            }
            cout << endl;
        }
    }
    else
    {
        for (int i = s; i >= e; i--)
        {
            for (int j = 1; j <= 9; j++)
            {
                cout << i << " * " << j << " = " << setw(2) << i * j << "   ";

                if (j % 3 == 0)
                {
                    cout << endl;
                }
            }
            cout << endl;
        }
    }

    return 0;
}

1303 - 숫자사각형1

#include <iostream>
using namespace std;

int main()
{
    int n;
    int m;

    cin >> n >> m;

    int num = 1;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << num++ << " ";
        }
        cout << endl;
    }

    return 0;
}

1856 - 숫자사각형2

#include <iostream>
using namespace std;

int main()
{
    int n;
    int m;

    cin >> n >> m;

    int num = 1;
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < m; j++)
            {
                cout << num++ << " ";
            }
            cout << endl;

            num += m - 1;
        }
        else
        {
            for (int j = 0; j < m; j++)
            {
                cout << num-- << " ";
            }
            cout << endl;

            num += m + 1;
        }
    }

    return 0;
}
  • 정올 사이트 array에 저장했다 출력하는 Hint가 있었지만 크게 어려운 부분은 없어서 바로 출력하도록 했다.

1304 - 숫자사각형3

#include <iostream>
using namespace std;

int main()
{
    int n;

    cin >> n;

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

    return 0;
}

2046 - 숫자사각형4

#include <iostream>
using namespace std;

int main()
{
    int n;
    int m;

    cin >> n >> m;

    if (m == 1)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << i << " ";
            }
            cout << endl;
        }
    }
    else if (m == 2)
    {
        for (int i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
                for (int j = 1; j <= n; j++)
                {
                    cout << j << " ";
                }
            }
            else
            {
                for (int j = n; j >= 1; j--)
                {
                    cout << j << " ";
                }
            }
            cout << endl;
        }
    }
    else if (m == 3)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << i + (i * j) << " ";
            }
            cout << endl;
        }
    }

    return 0;
}

1307 - 문자사각형1

#include <iostream>
using namespace std;

int main()
{
    int n;
    char arr[100][100];

    cin >> n;

    char c = 'A';
    for (int i = n - 1; i >= 0; i--)
    {
        for (int j = n - 1; j >= 0; j--)
        {
            arr[j][i] = c++;
            
            if (c > 'Z')
            {
                c = 'A';
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
  • n의 입력 범위가 최대 100이기 때문에 최대값 기준으로 array 정의했다.
  • Z에서 다시 A로 돌아가는 걸 고려해 array 없이 구현할 수 있지만 쉽게 구현할 수 있게 array를 사용했다.

1314 - 문자사각형2

#include <iostream>
using namespace std;

int main()
{
    int n;
    char arr[100][100];

    cin >> n;

    char c = 'A';
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < n; j++)
            {
                arr[j][i] = c++;

                if (c > 'Z')
                {
                    c = 'A';
                }
            }
        }
        else
        {
            for (int j = n - 1; j >= 0; j--)
            {
                arr[j][i] = c++;

                if (c > 'Z')
                {
                    c = 'A';
                }
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
  • if (c > 'Z') { c = 'A'; } 를 함수로 정의하여 중복 코드를 제거할 수 있지만 큰 차이는 없을 것 같아 그대로 중복해서 사용했다.

1338 - 문자삼각형1

#include <iostream>
using namespace std;

int main()
{
    int N;
    char arr[100][100] = { 0 };

    cin >> N;

    char c = 'A';
    for (int i = 0; i < N; i++)
    {
        for (int j = i, k = N - 1; j < N; j++, k--)
        {
            arr[j][k] = c++;

            if (c > 'Z')
            {
                c = 'A';
            }
        }
    } 

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (arr[i][j] == 0)
            {
                cout << "  ";
            }
            else
            {
                cout << arr[i][j] << " ";
            }
        }
        cout << endl;
    }

    return 0;
}
  • char arr[100][100] = { 0 }; 으로 초기화하여 0인 경우는 공백을 출력한다.
  • for (int j = i, k = N - 1; j < N; j++, k--) 처럼 for문 안에 변수를 여러 개 넣어서 복잡한 순서를 좀 더 쉽게 구현했다.
    정올 사이트 Hint에도 for문에서 초기값과 증감에 여러개의 연산을 처리하려면 위와 같이 ','로 구분하여 작성하면 된다. 라는 내용이 있다.

1339 - 문자삼각형2

#include <iostream>
using namespace std;

int main()
{
    int N;
    char arr[99][99] = { 0 };

    cin >> N;
    if (N < 1 || N > 99 || N % 2 == 0)
    {
        cout << "INPUT ERROR" << endl;

        return 1;
    }

    char c = 'A';
    for (int i = N / 2; i >= 0; i--)
    {
        for (int j = i; j < N - i; j++)
        {
            arr[j][i] = c++;

            if (c > 'Z')
            {
                c = 'A';
            }
        }
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j <= N / 2; j++)
        {
            if (arr[i][j] == 0)
            {
                cout << "  ";
            }
            else
            {
                cout << arr[i][j] << " ";
            }
        }
        cout << endl;
    }

    return 0;
}
  • N의 범위가 1 이상 100 이하의 홀수이므로 arr의 크기를 99 X 99로 정의했다.
  • N이 입력범위를 벗어나면 INPUT ERROR 출력 후 return 1; 으로 종료시켰다.
  • 열이 N / 2 이상인 경우는 출력할 필요가 없기 때문에 열에 대한 for문을 for (int j = 0; j <= N / 2; j++) 로 정의했다.
  • 정올 사이트에는 열 번호를 i라 하면 시작위치는 i와 같고 끝위치는 (n / 2) * 2 - i 와 같다. 라는 Hint가 있었지만 위의 조건이 더 쉬워보인다.

좋은 웹페이지 즐겨찾기