'Array'에 해당되는 글 2건

  1. 2008/02/17 귀차니스트 The Skyline Problem (2)
  2. 2008/02/17 귀차니스트 LCD Display (3)

The Skyline Problem

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=41
  2.  의   견   : 일일이 하나의 경우에 대해서 앞의 모든 경우를 검사하는 것이 아님을 가르쳐주는 문제. 방법은 배열을 선언, 입력이 들어올 때마다 배열을 범위로 선택 값을 갱신합니다. 출력시에는 다른 지점의 인덱스만 출력하면 됩니다.
  3. 소스
    105.cpp (Language : cpp)
    1. #include <iostream>
    2. short Skyline[ 10001 ] = { 0, };
    3. void InsertHeight( int Left, int Right, int Height )
    4. {
    5.     for( int i = Left; i < Right; ++i ) {
    6.         if( Skyline[ i ] < Height )
    7.             Skyline[ i ] = Height;
    8.     }
    9. }
    10. void PrintHeight()
    11. {
    12.     int LastHeight = 0;
    13.     int CurrentHeight;
    14.     bool IsFirst = true;
    15.     for( int i = 0; i < 10001; ++i ) {
    16.         CurrentHeight = Skyline[ i ];
    17.         if( CurrentHeight != LastHeight ) {
    18.             if( IsFirst )
    19.                 IsFirst = false;
    20.             else {
    21.                 std::cout << " ";
    22.             }
    23.             std::cout << i << " " << CurrentHeight;
    24.             LastHeight = CurrentHeight;
    25.         }
    26.     }
    27.     std::cout << std::endl;
    28. }
    29. int main( int argc, char **argv )
    30. {
    31.     int Left, Height, Right;
    32.     while( std::cin >> Left >> Height >> Right ) {
    33.         InsertHeight( Left, Right, Height );
    34.     }
    35.     PrintHeight();
    36.     return 0;
    37. }



크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
3n+1 Problem (0)2008/02/17
Ecological Bin Packing (0)2008/02/17
Maximum Sum (0)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30

댓글을 달아 주세요

  1. alex 2008/08/19 23:39  댓글주소  수정/삭제  댓글쓰기

    그냥 using namespace std 쓰시지 ...
    큰 작업도 아니고 uva 문제 푸는 정도면
    유도리 있게 코딩하시는거도 좋다고 생각합니다.

    • 귀차니스트 2008/09/21 21:21  댓글주소  수정/삭제

      아 ㅋㅋ 물론 진짜 ACM 대회를 나가게 된다면 시간이 중요하니 그럴수 있겠지만.. 평상시 일 때는 원칙을 되도록 지키고 싶어서 그렇습니다^^.

LCD Display

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=9&page=show_problem&problem=647
  2.  의   견   : 배열을 이용 참조하여 문자열을 출력하는 식으로 구현해봤습니다. 문제는 Commit 시 공백의 문제점 때문에 Wrong Answer가 뜨더군요. 하지만, 외관상 문자열 출력형식은 같기에 굳이 Accept까지 진행하지는 않았습니다. 가로와 세로의 출력을 분리하여 구현한다면 쉬운 문제일 것입니다.
  3. 소스
    706.cpp (Language : cpp)
    1. #include <iostream>
    2. #include <string>
    3. std::string NumberList;
    4. unsigned char CharacterTable[ 10 ][ 5 ][ 3 ] = { { { ' ', '-', ' ' }, { '|', ' ', '|' }, { ' ', ' ', ' ' }, { '|', ' ', '|' }, { ' ', '-', ' ' } }, // 0
    5. { { ' ', ' ', ' ' }, { ' ', ' ', '|' }, { ' ', ' ', ' ' }, { ' ', ' ', '|' }, { ' ', ' ', ' ' } }, // 1
    6. { { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', '-', ' ' }, { '|', ' ', ' ' }, { ' ', '-', ' ' } }, // 2
    7. { { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', '-', ' ' } }, // 3
    8. { { ' ', ' ', ' ' }, { '|', ' ', '|' }, { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', ' ', ' ' } }, // 4
    9. { { ' ', '-', ' ' }, { '|', ' ', ' ' }, { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', '-', ' ' } }, // 5
    10. { { ' ', '-', ' ' }, { '|', ' ', ' ' }, { ' ', '-', ' ' }, { '|', ' ', '|' }, { ' ', '-', ' ' } }, // 6
    11. { { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', ' ', ' ' }, { ' ', ' ', '|' }, { ' ', ' ', ' ' } }, // 7
    12. { { ' ', '-', ' ' }, { '|', ' ', '|' }, { ' ', '-', ' ' }, { '|', ' ', '|' }, { ' ', '-', ' ' } }, // 8
    13. { { ' ', '-', ' ' }, { '|', ' ', '|' }, { ' ', '-', ' ' }, { ' ', ' ', '|' }, { ' ', '-', ' ' } } }; // 9
    14. void HorizonalPrint( int Width, int Seq )
    15. {
    16.     bool IsFirst = true;
    17.     char PrintChar;
    18.     size_t VectorCount = NumberList.length();
    19.     for( unsigned int i = 0; i < VectorCount; ++i ) {
    20.         PrintChar = CharacterTable[ NumberList[ i ] - '0' ][ Seq ][ 1 ];
    21.         if( i != 0 )
    22.             std::cout << " ";
    23.         std::cout << " ";
    24.         for( int j = 0; j < Width; ++j ) {
    25.             std::cout << PrintChar;
    26.         }
    27.         std::cout << " ";
    28.     }
    29. }
    30. void VerticalPrint( int Width, int Seq )
    31. {
    32.     bool IsFirst = true;
    33.     size_t VectorCount = NumberList.length();
    34.     std::string PrintStr = "";
    35.     for( unsigned int i = 0; i < VectorCount; ++i ) {
    36.         if( i != 0 )
    37.             PrintStr += " ";
    38.         PrintStr += CharacterTable[ NumberList[ i ] - '0' ][ Seq ][ 0 ];
    39.         for( int j = 0; j < Width; ++j ) {
    40.             PrintStr += " ";
    41.         }
    42.         PrintStr += CharacterTable[ NumberList[ i ] - '0' ][ Seq ][ 2 ];
    43.     }
    44.     for( int Cnt = 0; Cnt < Width; ++Cnt )
    45.         std::cout << PrintStr.c_str() << std::endl;
    46. }
    47. void PrintNumber( int Width )
    48. {
    49.     for( int i = 0; i < 5; ++i ) {
    50.         if( i & 1 )
    51.             VerticalPrint( Width, i );
    52.         else {
    53.             HorizonalPrint( Width, i );
    54.             std::cout << std::endl;
    55.         }
    56.     }
    57. }
    58. int main( int argc, char **argv )
    59. {
    60.     int CharacterWidth;
    61.     while( true ) {
    62.         std::cin >> CharacterWidth >> NumberList;
    63.         if( CharacterWidth == 0 ) {
    64.             if( NumberList == "0" )
    65.                 break;
    66.             else
    67.                 continue;
    68.         }
    69.         PrintNumber( CharacterWidth );
    70.     }
    71.     return 0;
    72. }

크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
3n+1 Problem (0)2008/02/17
Ecological Bin Packing (0)2008/02/17
The Skyline Problem (2)2008/02/17
Maximum Sum (0)2008/02/17
2008/02/17 08:30 2008/02/17 08:30

댓글을 달아 주세요

  1. 최익필 2009/03/02 14:04  댓글주소  수정/삭제  댓글쓰기

    잘 배워 갑니다.

  2. 최익필 2009/03/02 14:10  댓글주소  수정/삭제  댓글쓰기

    궁금한 것을 여쭙습니다. int a = '1' - '0'; 처럼 문자열 빼기가 어떻게 정수가 될 수 있는지 궁금합니다.

  3. 최익필 2009/03/02 16:30  댓글주소  수정/삭제  댓글쓰기

    왜 나오는지 알았습니다 : ) 재미있었습니다. ㅋ