πŸ’» Dev & CSiOS & Others

[Swift] Swapping Upper and Lowercase - Programmers Lv.0 (181949)

Problem: μ˜μ–΄ μ•ŒνŒŒλ²³μœΌλ‘œ 이루어진 λ¬Έμžμ—΄ str이 μ£Όμ–΄μ§‘λ‹ˆλ‹€. 각 μ•ŒνŒŒλ²³μ„ λŒ€λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ μ†Œλ¬ΈμžλŠ” λŒ€λ¬Έμžλ‘œ λ³€ν™˜ν•΄μ„œ 좜λ ₯ν•˜λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•΄ λ³΄μ„Έμš”. https://school.programmers.co.kr/learn/courses/30/lessons/181949 Constraints: 1 ≀ str의 길이 ≀ 20 str은 μ•ŒνŒŒλ²³μœΌλ‘œ 이루어진 λ¬Έμžμ—΄μž…λ‹ˆλ‹€. Solution: let line = readLine()! let resultString = line.map { character in if character.isLowercase { return character.uppercased() } else { return character.lowercased() } }.joined() print(resultString) // λ˜λŠ” // print(readLine()?.map { $0.isLowercase ? $0.uppercased() : $0.lowercased() }.joined() ?? "") μž…λ ₯ 받은 각 문자 ν•˜λ‚˜ν•˜λ‚˜κ°€ λŒ€λ¬ΈμžμΈμ§€ μ†Œλ¬ΈμžμΈμ§€ νŒλ‹¨ν•΄μ„œ λ°˜λŒ€λ‘œ 좜λ ₯ν•˜λ©΄ λ˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€. ...

June 19, 2023 Β· 1 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 16 Solution

Problem: vector<Shape*> v;λ₯Ό μ΄μš©ν•˜μ—¬ κ°„λ‹¨ν•œ κ·Έλž˜ν”½ νŽΈμ§‘κΈ°λ₯Ό μ½˜μ†” λ°”νƒ•μœΌλ‘œ λ§Œλ“€μ–΄λ³΄μž. μƒμ„±λœ λ„ν˜• 객체λ₯Ό v에 μ‚½μž…ν•˜κ³  κ΄€λ¦¬ν•˜λΌ. 9μž₯ Exercise Problem 10번의 힌트λ₯Ό μ°Έκ³ ν•˜λΌ. Shapeκ³Ό Circle, Line, Rect ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. Execution Result: Objective & Hints: vectorλ₯Ό ν™œμš©ν•˜λŠ” μ’…ν•© μ‘μš© Code: 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 #include #include using namespace std; class Shape { protected: virtual void draw() = 0; public: void paint() { draw(); } }; class Circle : public Shape { protected: virtual void draw(){ cout << "Circle" << endl; } }; class Rect : public Shape { protected: virtual void draw() { cout << "Rectangle" << endl; } }; class Line : public Shape { protected: virtual void draw() { cout << "Line" << endl; } }; class UI { public: static int seleteMenu() { int n; cout << "μ‚½μž…:1, μ‚­μ œ:2, λͺ¨λ‘λ³΄κΈ°:3, μ’…λ£Œ:4 >> "; cin >> n; return n; } static int seleteShape() { int n; cout << "μ„ :1, 원:2, μ‚¬κ°ν˜•:3 >> "; cin >> n; return n; } static int seleteDelIndex() { int n; cout << "μ‚­μ œν•˜κ³ μž ν•˜λŠ” λ„ν˜•μ˜ 인덱슀 >> "; cin >> n; return n; } static void showAll(vector<Shape*> &v, vector<Shape*>::iterator &it) { int i=0; for(it = v.begin();it!=v.end(); it++, i++){ // vector v의 첫 μ›μ†ŒλΆ€ν„° 끝 μ›μ†ŒκΉŒμ§€ 탐색 및 좜λ ₯ cout << i << ": "; v.at(i)->paint(); } } }; class GraphicEditor { vector<Shape*> v; vector<Shape*>::iterator it; public: GraphicEditor() { cout << "κ·Έλž˜ν”½ μ—λ””ν„°μž…λ‹ˆλ‹€.\n"; start(); } void start() { while(true){ int n; n = UI::seleteMenu(); switch(n){ case 1: //μ‚½μž…μ„ μ„ νƒν•œ 경우 n = UI::seleteShape(); switch(n){ case 1: //선을 μ„ νƒν•œ 경우 v.push_back(new Line()); break; case 2: //원을 μ„ νƒν•œ 경우 v.push_back(new Circle()); break; case 3: //μ‚¬κ°ν˜•μ„ μ„ νƒν•œ 경우 v.push_back(new Rect()); break; default: cout << "잘λͺ» μ„ νƒν•˜μ…¨μŠ΅λ‹ˆλ‹€.\n"; break; } break; case 2:{ //μ‚­μ œλ₯Ό μ„ νƒν•œ 경우 n = UI::seleteDelIndex(); if(n >= v.size()

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 14 Solution

Problem: μ•”ν˜Έ 관리 μ‘μš©ν”„λ‘œκ·Έλž¨μ„ map을 μ΄μš©ν•˜μ—¬ μž‘μ„±ν•˜λΌ. μ‹€ν–‰ 과정은 λ‹€μŒκ³Ό κ°™λ‹€. Execution Result: Objective & Hints: map μ»¨ν…Œμ΄λ„ˆμ— μ‚½μž… 및 쑰회 μ‘μš© 이름과 점수λ₯Ό 쌍으둜 μ €μž₯ν•  λ§΅ μ»¨ν…Œμ΄λ„ˆλ‘œ map<string, string>을 μ΄μš©ν•˜λ©΄ λ©λ‹ˆλ‹€. μ•„λž˜ 링크에 μžˆλŠ” Exercise Problem 10μž₯ 13λ²ˆμ„ μ°Έκ³ ν•˜μ„Έμš”. Code: 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 using namespace std; void insert(map<string, string> &pwManager){ string name, pw; cout << "이름 μ•”ν˜Έ>> "; cin >> name >> pw; pwManager.insert(make_pair(name, pw)); } void checkNamePw(map<string, string> &pwManager){ string name, pw; cout << "이름? "; cin >> name; while(true){ cout << "μ•”ν˜Έ? "; cin >> pw; if(pwManager[name] == pw){ cout << "톡과!!\n"; break; } else cout << "μ‹€νŒ¨~~\n"; // 틀리면 좜λ ₯ ν›„ λ‹€μ‹œ μ•”ν˜Έ 질문 } } int main() { map<string, string> pwManager; cout << "***** μ•”ν˜Έ 관리 ν”„λ‘œκ·Έλž¨ WHOλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€ *****\n"; while(true){ cout << "μ‚½μž…:1, 검사:2, μ’…λ£Œ3>> "; int n; cin >> n; switch(n){ case 1: insert(pwManager); break; case 2: checkNamePw(pwManager); break; case 3: cout << "ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€..."; return 0; } } } Explanation: ...

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 13 Solution

Problem: map μ»¨ν…Œμ΄λ„ˆλ₯Ό μ΄μš©ν•˜μ—¬ (이름, 성적)을 μ €μž₯ν•˜κ³  μ΄λ¦„μœΌλ‘œ 성적을 μ‘°νšŒν•˜λŠ” 점수 관리 ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€μ–΄λΌ. 이름은 빈칸 없이 μž…λ ₯ν•˜λŠ” 것을 μ›μΉ™μœΌλ‘œ ν•œλ‹€. Execution Result: Objective & Hints: map μ»¨ν…Œμ΄λ„ˆ ν™œμš© 이름과 점수λ₯Ό 쌍으둜 μ €μž₯ν•  λ§΅ μ»¨ν…Œμ΄λ„ˆλ‘œ map<string, int>λ₯Ό μ΄μš©ν•˜λ©΄ λœλ‹€. Code: 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 #include #include #include using namespace std; int main() { map<string, int> Score; // map μ»¨ν…Œμ΄λ„ˆ 생성. ν‚€λŠ” ν•œκΈ€ 이름, 값은 μ •μˆ˜ 점수 cout << "***** μ μˆ˜κ΄€λ¦¬ ν”„λ‘œκ·Έλž¨ HIGH SCORE을 μ‹œμž‘ν•©λ‹ˆλ‹€ *****\n"; while(true){ int num; int score; string name; cout << "μž…λ ₯:1, 쑰회:2, μ’…λ£Œ:3 >> "; cin >> num; switch (num){ case 1: cout << "이름과 점수>> "; cin >> name >> score; Score.insert(make_pair(name, score)); // map에 μ €μž₯ break; case 2: cout << "이름 >> "; cin >> name; if(Score.find(name) == Score.end()) // name 'ν‚€'λ₯Ό λκΉŒμ§€ μ°Ύμ•˜λŠ”λ° μ—†μŒ cout << "μ—†μŒ" << endl; else cout << name << "의 μ μˆ˜λŠ” " <<Score[name] << endl; // Scoreμ—μ„œ name의 값을 μ°Ύμ•„ 좜λ ₯ break; case 3: cout << "ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€...\n"; return 0; default : cout << "μ œλŒ€λ‘œ μž…λ ₯ν•΄\n"; break; } } } Explanation: ...

March 11, 2020 Β· 2 min Β· Sobamemil