-
JUNGOL 기초다지기 09 배열1보관함 2019. 11. 28. 21:03
프로그래밍에서 가장 기초적인 내용을 다루는 기초 다지기입니다.
이번 포스팅은 기초 다지기 중 아홉 번째인 배열1 문제를 풀어보도록 하겠습니다.
기초 내용인 만큼 다른 설명없이 문제와 코드만 간단하게 작성하도록 하겠습니다.
555 : 배열1 - 자가진단1
#include <iostream> using namespace std; int main(void) { char arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } for (int i = 0; i < 10; ++i) { cout << arr[i]; } cout << endl; }
556 : 배열1 - 자가진단2
#include <iostream> using namespace std; int main(void) { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; for (int i = 0; i < 10; ++i) { cout << arr[i] << ' '; } cout << endl; }
557 : 배열1 - 자가진단3
#include <iostream> using namespace std; int main(void) { char arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } for (int i = 0, cnt = 0; cnt < 3; i += 3, ++cnt) { cout << arr[i] << ' '; } cout << endl; }
558 : 배열1 - 자가진단4
#include <iostream> using namespace std; int main(void) { int arr[100]; int cnt = 0; for (int temp, i = 0; i < 100; ++i, ++cnt) { cin >> temp; if (temp == 0) break; arr[i] = temp; } for (int i = cnt - 1; i >= 0; --i) { cout << arr[i] << ' '; } cout << endl; }
559 : 배열1 - 자가진단5
#include <iostream> using namespace std; int main(void) { float arr[7]{ 0, 85.6, 79.5, 83.1, 80.0, 78.2, 75.0 }; int var1, var2; cin >> var1 >> var2; float sum = arr[var1] + arr[var2]; cout.setf(ios::fixed); cout.precision(1); cout << sum << endl; }
560 : 배열1 - 자가진단6
#include <iostream> using namespace std; int main(void) { int arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } int min = 1000; for (int i = 0; i < 10; ++i) { if (min > arr[i]) min = arr[i]; } cout << min << endl; }
561 : 배열1 - 자가진단7
#include <iostream> using namespace std; bool IsInRange(int min, int max, int num); int main(void) { int arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } int min = 10000, max = 0; for (int i = 0; i < 10; ++i) { if (min > arr[i] && IsInRange(100, 9999, arr[i])) min = arr[i]; if (max < arr[i] && IsInRange(1, 99, arr[i])) max = arr[i]; } if (min == 10000) min = 100; if (max == 0) max = 100; cout << max << ' ' << min << endl; } bool IsInRange(int min, int max, int num) { if (num < min) return false; if (num > max) return false; return true; }
562 : 배열1 - 자가진단8
#include <iostream> using namespace std; int main(void) { int arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } int evenSum = 0, oddSum = 0; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) oddSum += arr[i]; else evenSum += arr[i]; } float oddAvg = oddSum / 5.f; cout.setf(ios::fixed); cout.precision(1); cout << "sum : " << evenSum << endl; cout << "avg : " << oddAvg << endl; }
563 : 배열1 - 자가진단9
#include <iostream> using namespace std; void Swap(int& num1, int& num2); int main(void) { int arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } for (int i = 0; i < 10; ++i) { for (int j = i + 1; j < 10; ++j) { if (arr[i] < arr[j]) Swap(arr[i], arr[j]); } } for (int i = 0; i < 10; ++i) { cout << arr[i] << ' '; } cout << endl; } void Swap(int& num1, int& num2) { int temp = num1; num1 = num2; num2 = temp; }
150 : 배열1 - 형성평가1
#include <iostream> using namespace std; int main(void) { char arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } for (int i = 9; i >= 0; --i) { cout << arr[i] << ' '; } cout << endl; }
151 : 배열1 - 형성평가2
#include <iostream> using namespace std; int main(void) { int arr[5]; for (int i = 0; i < 5; ++i) { cin >> arr[i]; } int sum = 0; for (int i = 0; i < 5; i += 2) { sum += arr[i]; } cout << sum << endl; }
152 : 배열1 - 형성평가3
#include <iostream> using namespace std; int main(void) { int arr[10]; for (int i = 0; i < 10; ++i) { cin >> arr[i]; } int oddSum = 0; for (int i = 0; i < 10; i += 2) { oddSum += arr[i]; } cout << "odd : " << oddSum << endl; int evenSum = 0; for (int i = 1; i < 10; i += 2) { evenSum += arr[i]; } cout << "even : " << evenSum << endl; }
153 : 배열1 - 형성평가4
#include <iostream> using namespace std; int GetCollectNum(int num); int main(void) { int arr[100]; int count = 0; for (int i = 0; i < 100; ++i, ++count) { cin >> arr[i]; if (arr[i] == -1) break; } for (int i = GetCollectNum(count - 3); i < count; ++i) { cout << arr[i] << ' '; } cout << endl; } int GetCollectNum(int num) { if (num <= 0) return 0; return num; }
154 : 배열1 - 형성평가5
#include <iostream> using namespace std; int main(void) { float arr[6]; for (int i = 0; i < 6; ++i) { cin >> arr[i]; } float sum = 0; for(int i=0; i<6; ++i) { sum += arr[i]; } float avg = sum / 6.f; cout.setf(ios::fixed); cout.precision(1); cout << avg << endl; }
155 : 배열1 - 형성평가6
#include <iostream> using namespace std; int main(void) { char arr[6] = { 'J', 'U', 'N', 'G', 'O', 'L' }; char var; cin >> var; int idx = -1; for (int i = 0; i < 6; ++i) { if (var == arr[i]) { idx = i; break; } } if (idx != -1) cout << idx << endl; else cout << "none" << endl; }
156 : 배열1 - 형성평가7
#include <iostream> using namespace std; int main(void) { int arr[100]; int count = 0; for (int i = 0; i < 100; ++i, ++count) { cin >> arr[i]; if (arr[i] == 999) break; } int min = 1000, max = -1000; for (int i = 0; i < count; ++i) { if (min > arr[i]) min = arr[i]; if (max < arr[i]) max = arr[i]; } cout << "max : " << max << endl; cout << "min : " << min << endl; }
157 : 배열1 - 형성평가8
#include <iostream> using namespace std; int main(void) { int arr[100]; int count = 0; for (int i = 0; i < 100; ++i, ++count) { cin >> arr[i]; if (arr[i] == 0) break; } int countMul5 = 0; int sum = 0; for (int i = 0; i < count; ++i) { if (arr[i] % 5 == 0) { countMul5++; sum += arr[i]; } } float avg = static_cast<float>(sum) / countMul5; cout.setf(ios::fixed); cout.precision(1); cout << "Multiples of 5 : " << countMul5 << endl; cout << "sum : " << sum << endl; cout << "avg : " << avg << endl; }
158 : 배열1 - 형성평가9
#include <iostream> using namespace std; int main(void) { int arr[100]; int count = 0; for (int i = 0; i < 100; ++i, ++count) { cin >> arr[i]; if (arr[i] == 0) break; } cout << count << endl; for (int i = 0; i < count; ++i) { if (arr[i] % 2 == 0) cout << arr[i] / 2 << ' '; else cout << arr[i] * 2 << ' '; } cout << endl; }
159 : 배열1 - 형성평가A
#include <iostream> using namespace std; void Swap(int& a, int& b); int main(void) { int arr[20]; int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = 0; i < n; ++i) { int max = i; for (int j = i + 1; j < n; ++j) { if (arr[max] < arr[j]) max = j; } Swap(arr[i], arr[max]); cout << arr[i] << endl; } } void Swap(int& a, int& b) { int temp = a; a = b; b = temp; }
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=1090