-
JUNGOL 기초다지기 06 반복제어문1보관함 2019. 11. 19. 21:20
프로그래밍에서 가장 기초적인 내용을 다루는 기초 다지기입니다.
이번 포스팅은 기초 다지기 중 여섯 번째인 반복제어문1 문제를 풀어보도록 하겠습니다.
기초 내용인 만큼 다른 설명없이 문제와 코드만 간단하게 작성하도록 하겠습니다.
536 : 반복제어문1 - 자가진단1
#include <iostream> using namespace std; int main(void) { int num = 1; while (num <= 15) { cout << num++ << ' '; } cout << endl; }
537 : 반복제어문1 - 자가진단2
#include <iostream> using namespace std; int main(void) { int var; cin >> var; int num = 1; int sum = 0; while (num <= var) { sum += num++; } cout << sum << endl; }
538 : 반복제어문1 - 자가진단3
#include <iostream> using namespace std; int main(void) { int var; while(true) { cout << "number? "; cin >> var; if (var == 0) break; if (var > 0) cout << "positive integer" << endl; else cout << "negative number" << endl; } }
539 : 반복제어문1 - 자가진단4
이 문제는 do-while문을 사용하기 적절한 문제입니다.
#include <iostream> using namespace std; int main(void) { int var; int count = 0, sum = 0; do { cin >> var; count++; sum += var; } while (var < 100); cout.setf(ios::fixed); cout.precision(1); float avg = static_cast<float>(sum) / count; cout << sum << endl; cout << avg << endl; }
540 : 반복제어문1 - 자가진단5
#include <iostream> using namespace std; int main(void) { int var; while (true) { cin >> var; if (var == -1) break; if (var % 3 == 0) { cout << var / 3 << endl; } } }
633 : 반복제어문1 - 자가진단6
#include <iostream> using namespace std; void PrintMenu(); bool IsInMenu(int select); void PrintCityName(int select); int main(void) { int select; do { PrintMenu(); cout << "number? "; cin >> select; PrintCityName(select); } while (IsInMenu(select)); } void PrintMenu() { cout << "1. Korea" << endl; cout << "2. USA" << endl; cout << "3. Japan" << endl; cout << "4. China" << endl; } bool IsInMenu(int select) { return select >= 1 && select <= 4; } void PrintCityName(int select) { cout << endl; switch (select) { case 1: cout << "Seoul" << endl; break; case 2: cout << "Washington" << endl; break; case 3: cout << "Tokyo" << endl; break; case 4: cout << "Beijing" << endl; break; default: cout << "none" << endl; break; } cout << endl; }
125 : 반복제어문1 - 형성평가1
#include <iostream> using namespace std; int main(void) { int var; cin >> var; for (int i = 1; i <= var; ++i) { cout << i << ' '; } cout << endl; }
126 : 반복제어문1 - 형성평가2
#include <iostream> using namespace std; int main(void) { int var; int even = 0, odd = 0; while (true) { cin >> var; if (var == 0) break; if (var % 2 == 0) even++; else odd++; } cout << "odd : " << odd << endl; cout << "even : " << even << endl; }
127 : 반복제어문1 - 형성평가3
#include <iostream> using namespace std; bool IsInRange(int var, int min, int max); int main(void) { int var, sum = 0, count = 0; while(true) { cin >> var; if (!IsInRange(var, 0, 100)) break; sum += var; count++; } float avg = static_cast<float>(sum) / count; cout.setf(ios::fixed); cout.precision(1); cout << "sum : " << sum << endl; cout << "avg : " << avg << endl; } bool IsInRange(int var, int min, int max) { if (var < min) return false; if (var > max) return false; return true; }
128 : 반복제어문1 - 형성평가4
#include <iostream> using namespace std; int main(void) { int var, count = 0; while (true) { cin >> var; if (var == 0) break; if (var % 3 != 0 && var % 5 != 0) count++; } cout << count << endl; }
129 : 반복제어문1 - 형성평가5
#include <iostream> using namespace std; bool CheckContinue(); int main(void) { int base, height; cout.setf(ios::fixed); cout.precision(1); do { cout << "Base = "; cin >> base; cout << "Height = "; cin >> height; cout << "Triangle width = " << base * height * 0.5f << endl; } while (CheckContinue()); } bool CheckContinue() { char ch; cout << "Continue? "; cin >> ch; if (ch == 'Y' || ch == 'y') return true; return false; }
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=1060