Outdated/Algorithm Solution

[BOJ] 6581번 HTML

해달 2020. 4. 27. 18:00

복기

C#의 String.Split()은 공백 문자 전체를 제거하여 주지는 않으니 유의하자.


코드

  • C++ 17


#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

 

    string word;

    int lineSize = 0;

    while (cin >> word)

    {

        if (word == "<br>")

        {

            cout << '\n';

            lineSize = 0;

            continue;

        }

        else if (word == "<hr>")

        {

            if (lineSize != 0)

                cout << '\n';

            cout << "--------------------------------------------------------------------------------\n";

            lineSize = 0;

            continue;

        }

            

        if (lineSize + word.size() > 80)

        {

            cout << '\n';

            lineSize = 0;

        }

 

        lineSize += word.size() + 1;

        cout << word << ' ';

    }

}


  • C# 6.0


using System;

using System.IO;

using System.Text;

 

namespace Csharp

{

    class Program

    {

        static StringBuilder sb = new StringBuilder();

        static int lineSize = 0;

 

        static void Main(string[] args)

        {   

            while (true)

            {

                var words = Console.ReadLine()?.Split();

                if (words == null)

                    break;

 

                for (int i = 0; i < words.Length; i++)

                {

                    if (words[i] == "")

                        continue;

                    else if (words[i] == "<br>")

                    {

                        StartNewLine();

                        continue;

                    }

                    else if (words[i] == "<hr>")

                    {

                        if (lineSize != 0)

                            StartNewLine();

                        sb.Append("--------------------------------------------------------------------------------");

                        StartNewLine();

                        continue;

                    }

 

                    if (lineSize + words[i].Length > 80)

                        StartNewLine();

                    sb.Append($"{words[i]} ");

                    lineSize += words[i].Length + 1;

                }

            }

 

            Console.WriteLine(sb.ToString());

        }

 

        static void StartNewLine()

        {

            sb.Append('\n');

            lineSize = 0;

        }

    }

}


  • Python 3


from sys import stdin, stdout

print = stdout.write

 

lineSize = 0

for line in stdin.readlines():

    for word in line.split():

        if word == '<br>':

            print('\n')

            lineSize = 0

            continue

 

        if word == '<hr>':

            if lineSize != 0:

                print('\n')

            print(f"{'-' * 80}\n")

            lineSize = 0

            continue

 

        if lineSize + len(word) > 80:

            print('\n')

            lineSize = 0

 

        print(f"{word} ")

        lineSize += len(word) + 1


'Outdated > Algorithm Solution' 카테고리의 다른 글

[BOJ] 1799번 비숍  (0) 2020.05.04
[BOJ] 2661번 좋은 수열  (0) 2020.05.04
[BOJ] 10174번 팰린드롬  (0) 2020.04.27
[BOJ] 1083번 소트  (0) 2020.04.20
[BOJ] 1431번 시리얼 번호  (0) 2020.04.20