[Swift] 문자열 출력하기 / LV.0, 181952, 프로그래머스

문제: 문자열 str이 주어질 때, str을 출력하는 코드를 작성해 보세요. https://school.programmers.co.kr/learn/courses/30/lessons/181952 [프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr](https://school.programmers.co.kr/learn/courses/30/lessons/181952) 제한사항: 1 ≤ str의 길이 ≤ 1,000,000 str에는 공백이 없으며, 첫째 줄에 한 줄로만 주어집니다. 풀이: 입력 받은 문자열을 그대로 출력해주면 되는 문제입니다. print(readLine() ?? "") readLine 함수는 optional을 return 하므로 ?? 연산자(nil 병합 연산자 / coalescing operator)를 사용해 줍니다. ...

June 18, 2023 · 1 min · Sobamemil

크러스컬 알고리즘 구현 2 (붕괴법칙을 적용한 방법)

프로그램 개요 : C 언어를 사용한 크러스컬 알고리즘(Kruskal Algorithm) 구현. 붕괴법칙을 적용하지 않은 크러스컬 알고리즘. 붕괴법칙을 적용한 크러스컬 알고리즘 프로그램 구조 및 설계: 이전 글과 이어지는 포스팅입니다. 프로그램의 구조와 설계 방법은 아래 링크에서 보실 수 있습니다. 2021.04.09 - [알고리즘] - 크러스컬 알고리즘 구현 1 (붕괴법칙을 적용하지 않은 방법) [크러스컬 알고리즘 구현 1 (붕괴법칙을 적용하지 않은 방법) 프로그램 개요 : C 언어를 사용한 크러스컬 알고리즘(Kruskal Algorithm) 구현. 붕괴법칙을 적용하지 않은 크러스컬 알고리즘. 붕괴법칙을 적용한 크러스컬 알고리즘 입력 파일 : 프로그램 실행 결과 : ...

April 9, 2021 · 10 min · Sobamemil

크러스컬 알고리즘 구현 1 (붕괴법칙을 적용하지 않은 방법)

프로그램 개요 : C 언어를 사용한 크러스컬 알고리즘(Kruskal Algorithm) 구현. 붕괴법칙을 적용하지 않은 크러스컬 알고리즘. 붕괴법칙을 적용한 크러스컬 알고리즘 크러스컬 알고리즘에 대한 설명은 아래 링크의 이전 글에서 볼 수 있습니다. 2020.07.02 - [알고리즘] - 크러스컬(Kruskal) 알고리즘 [크러스컬(Kruskal) 알고리즘 크러스컬(Kruskal) 알고리즘이란? 크러스컬 알고리즘은 최소 비용 신장 그래프를 찾는 알고리즘 입니다. 변의 개수를 E, 꼭지점의 개수를 V라고 한다면 크러스컬 알고리즘은 O(ElogV)의 시간 복잡도 sobamemil.tistory.com](https://sobamemil.tistory.com/150) 이 글에서는 예로 주어진 그래프에 대해서 크러스컬 알고리즘으로 문제를 해결하는 방법과 코드를 작성하였습니다. ...

April 9, 2021 · 9 min · Sobamemil

명품 C++ programming 실습 문제 11장 12번

문제 : 커피 자판기 시뮬레이터를 C++로 작성해보자. 실행 사례는 다음과 같다. 자판기는 보통 커피, 설탕 커피, 블랙 커피의 3종류만 판매한다. 단순화를 위해 실행 살에는 총 3인분의 재료만 가지도록 하였다. 커피 메뉴에 따라 필요한 재료들이 하나씩 없어진다. 객체 지향 구조에 따라 필요한 클래스를 작성하여 프로그램을 완성하라. 실행 결과 : 목적 및 힌트 : 객체 지향 구조로 종합 응용 연습 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 #include #include using namespace std; class Material { protected: string name; int amount; public: string getName() { return name; } int getAmount() { return amount; } void setAmount(int amount) { this->amount = amount; } bool subAmount(int amount) { if(this->amount <= 0) return false; else this->amount -= amount; return true; } }; class Coffee : public Material { public: Coffee() { name = “Coffee”; amount = 3; } }; class Sugar : public Material { public: Sugar() { name = “Sugar”; amount = 3; } }; class Cream : public Material { public: Cream() { name = “Cream”; amount = 3; } }; class Water : public Material { public: Water() { name = “Water”; amount = 3; } }; class Cup : public Material { public: Cup() { name = “Cup”; amount = 3; } }; class CoffeeMachine { Material *mat[]; // Material의 객체 배열 포인터 생성 public: CoffeeMachine() { // 생성자에서 안내 멘트와 재료 상태 출력 cout « “—–명품 커피 자판기 켭니다.—–” « endl; mat[0] = new Coffee(); mat[1] = new Sugar(); mat[2] = new Cream(); mat[3] = new Water(); mat[4] = new Cup(); showCoffeeMachineState(); cout « endl; } void showCoffeeMachineState() { // 재료 상태 출력 for(int i=0; i<5; i++) { cout « setw(10) « mat[i]->getName(); for(int j=0; j<mat[i]->getAmount(); j++) cout « “*”; cout « endl; } } void start() { // 메뉴 출력 시작 int num; while(true) { showMenu(); num = selectMenu(); if(num == 3) { // 채우기 for(int i=0; i<5; i++) { mat[i]->setAmount(3); } cout « “모든 통을 채웁니다~~” « endl; showCoffeeMachineState(); cout « endl; continue; } else if(num == 4) { // 종료 cout « “프로그램을 종료합니다…” « endl; exit(0); } if(mat[0]->subAmount(1) == false) { // coffee-1 cout « “재료가 부족합니다.” « endl; showCoffeeMachineState(); continue; } if(mat[3]->subAmount(1) == false) { // water-1 cout « “재료가 부족합니다.” « endl; showCoffeeMachineState(); continue; } if(mat[4]->subAmount(1) == false) { // cup-1 cout « “재료가 부족합니다.” « endl; showCoffeeMachineState(); continue; } // 기본 재료가 부족하지 않으면 실행 switch(num) { case 0: // 보통 커피는 cream 추가 소모 if(mat[2]->subAmount(1) == false) { // cream-1 cout « “재료가 부족합니다.” « endl; showCoffeeMachineState(); continue; } cout « “맛있는 보통 커피 나왔습니다~~” « endl; showCoffeeMachineState(); cout « endl; break; case 1: // 설탕 커피는 sugar 추가 소모 if(mat[1]->subAmount(1) == false) { // sugar-1 cout « “재료가 부족합니다.” « endl; showCoffeeMachineState(); continue; } cout « “맛있는 설탕 커피 나왔습니다~~” « endl; showCoffeeMachineState(); cout « endl; break; case 2: // 블랙 커피는 추가 소모 없음 cout « “맛있는 블랙 커피 나왔습니다~~” « endl; showCoffeeMachineState(); break; default : // 잘못 입력 cout « “잘못 입력 하셨습니다.” « endl « endl; break; } } } void showMenu() { cout « “보통 커피:0, 설탕 커피:1, 블랙 커피:2, 채우기:3, 종료:4» “; } int selectMenu() { int num; cin » num; return num; } }; int main() { cout.setf(ios::left); CoffeeMachine c; c.start(); } 설명 : ...

April 2, 2020 · 4 min · Sobamemil

명품 C++ programming 실습 문제 11장 11번

문제 : 다음은 프로그램과 실행 결과를 보여준다. pos 조작자를 작성하라. 1 2 3 4 5 6 7 8 9 #include using namespace std; int main() { int x, y; cin » pos » x; cin » pos » y; cout « x « ‘,’ « y « endl; } 실행 결과 : 목적 및 힌트 : 조작자 작성 연습 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; istream& pos (istream& ins) { // pos 조작자 cout « “위치는? “; return ins; } int main() { int x, y; cin » pos » x; cin » pos » y; cout « x « ‘,’ « y « endl; } 공유하기 ...

April 2, 2020 · 1 min · Sobamemil

명품 C++ programming 실습 문제 11장 10번

문제 : 다음은 프로그램과 실행 결과를 보여준다. prompt 조작자를 작성하여 프로그램을 완성하라. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include using namespace std; int main() { string password; while(true) { cin » prompt » password; if(password == “C++") { cout « “login success!!” « endl; break; } else cout « “login fail. try again!!” « endl; } } 실행 결과 : ...

April 2, 2020 · 2 min · Sobamemil

명품 C++ programming 실습 문제 11장 9번

문제 : 다음은 Phone 클래스이다. 1 2 3 4 5 6 7 8 9 10 11 class Phone { // 전화 번호를 표현하는 클래스 string name; string telnum; string address; public: Phone(string name="", string telnum="", string address="") { this->name = name; this->telnum = telnum; this->address = address; } }; Phone 클래스의 객체를 입출력하는 아래 코드와 실행 결과를 참조하여 «, » 연산자를 작성하고 Phone 클래스를 수정하는 등 프로그램을 완성하라. ...

April 2, 2020 · 2 min · Sobamemil

명품 C++ programming 실습 문제 11장 8번

문제 : Circle 클래스는 다음과 같다. 1 2 3 4 5 6 7 8 class Circle { string name; int radius; public: Circle(int radius=1, string name="") { this->radius = radius; this->name = name; } }; Circle 클래스의 객체를 입출력하는 다음 코드와 실행 결과를 참조하여 «, » 연산자를 작성하고 Circle 클래스를 수정하는 등 프로그램을 완성하라. 1 2 3 Circle d, w; cin » d » w; // 키보드 입력을 받아 객체 d와 w를 완성 cout « d « w « endl; // 객체 d, w 출력 실행 결과 : ...

April 2, 2020 · 2 min · Sobamemil

명품 C++ programming 실습 문제 11장 7번

문제 : 0에서 127까지 ASCII 코드와 해당 문자를 다음과 같이 출력하는 프로그램을 작성하라. 화면에 출력가능하지 않는 ASCII 코드는 ‘.‘으로 출력하라. 실행 결과 : 목적 및 힌트 : cout으로 포맷 출력 응용 연습 문자가 출력 가능한지 알기 위해 bool isprint(int c); 함수를 사용하면 됩니다. 매개 변수 c는 문자 코드 값이고, 헤더 파일을 include 해야합니다. 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include #include using namespace std; void showDec(int d) { // 10진수 출력 cout « setw(10) « dec « d; } void showHexa(int h) { // 16진수 출력 cout « setw(10) « hex « h; } void showChar(int c) { // ASCII 출력 int i=0; if( (i = isprint(c)) != 0) // 출력 가능한 문자인지 확인 cout « setw(10) « (char)c; else // 출력 불가능한 문자이면 “.” 출력 cout « setw(10) « “.”; } void print() { for(int i=0; i<4; i++){ cout « setw(10) « “dec”; cout « setw(10) « “hexa”; cout « setw(10) « “char”; } cout « endl; for(int i=0; i<4; i++){ cout « setw(10) « “—”; cout « setw(10) « “—-”; cout « setw(10) « “—-”; } cout « endl; for(int i=0; i<128; i++){ // 127번 반복하여 dec, hexa, ASCII 출력 if(i%4==0 && i!=0) cout « endl; showDec(i); showHexa(i); showChar(i); } } int main() { cout.setf(ios::left); // 출력 포맷 왼쪽 정렬 print(); } 설명 : ...

April 2, 2020 · 2 min · Sobamemil

명품 C++ programming 실습 문제 11장 6번

문제 : 다음과 같이 정수, 제곱, 제곱근의 값을 형식에 맞추어 출력하는 프로그램을 작성하라. 필드의 간격은 총 15칸이고 제곱근의 유효 숫자는 총 3자리로 한다. 빈칸은 모두 underline(_) 문자로 삽입한다. 실행 결과 : 목적 및 힌트 : cout으로 포맷 출력 응용 연습 제곱근을 구하려면 헤더 파일을 include 하고 sqrt(double x) 함수를 호출하면 됩니다. 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include using namespace std; void showNumber(const double &num){ cout « setw(15) « setfill(’_’) « num; } void showSqrt(const double &num){ cout « setprecision(3) « setw(15) « setfill(’_’) « sqrt(num) « endl; } int main() { cout.setf(ios::left); cout « setw(15) « “Number”; cout « setw(15) « “Square”; cout « setw(15) « “Square Root” « endl; for(double i=0; i<=45; i+=5){ cout.precision(4); showNumber(i); // Number 출력 showNumber(i*i); // Square 출력 showSqrt(i); // Square Root 출력 } } 설명 : ...

March 27, 2020 · 2 min · Sobamemil