-
JUNGOL 기초다지기 16 구조체보관함 2020. 1. 6. 21:39
프로그래밍에서 가장 기초적인 내용을 다루는 기초 다지기입니다.
이번 포스팅은 기초 다지기 중 열여섯 번째인 구조체 문제를 풀어보도록 하겠습니다.
기초 내용인 만큼 다른 설명없이 문제와 코드만 간단하게 작성하도록 하겠습니다.
613 : 구조체 - 자가진단1
#include <iostream> #include <string> using namespace std; struct Person { string name; string school; int grade; }; int main() { Person p; cin >> p.name >> p.school >> p.grade; cout << "Name : " << p.name << endl; cout << "School : " << p.school << endl; cout << "Grade : " << p.grade << endl; }
614 : 구조체 - 자가진단2
#include <iostream> #include <string> using namespace std; struct Person { string school; int grade; }; int main() { Person p1{ "Jejuelementary", 6 }, p2; cin >> p2.school >> p2.grade; cout << p1.grade << " grade in " << p1.school << " School\n"; cout << p2.grade << " grade in " << p2.school << " School\n"; }
615 : 구조체 - 자가진단3
#include <iostream> #include <string> using namespace std; struct Person { string name; int ko; int en; }; int main() { Person people[2]; for (int i = 0; i < 2; ++i) { cin >> people[i].name >> people[i].ko >> people[i].en; } for (int i = 0; i < 2; ++i) { cout << people[i].name << ' ' << people[i].ko << ' ' << people[i].en << endl; } int avg[2]{ (people[0].ko + people[1].ko) / 2, (people[0].en + people[1].en) / 2 }; cout << "avg " << avg[0] << ' ' << avg[1] << endl; }
616 : 구조체 - 자가진단4
#include <iostream> using namespace std; template <typename T> struct Point { T x; T y; }; int main() { Point<int> points[3]; Point<float> c{ 0, 0 }; for (int i = 0; i < 3; ++i) { cin >> points[i].x >> points[i].y; c.x += points[i].x; c.y += points[i].y; } cout.setf(ios::fixed); cout.precision(1); c.x /= 3.f; c.y /= 3.f; cout << '(' << c.x << ", " << c.y << ")\n"; }
617 : 구조체 - 자가진단5
#include <iostream> #include <string> using namespace std; struct Person { string name; int height; }; int main() { Person shortestPerson{ "", 9999 }; Person temp; for (int i = 0; i < 5; ++i) { cin >> temp.name >> temp.height; if (temp.height < shortestPerson.height) { shortestPerson.name = temp.name; shortestPerson.height = temp.height; } } cout << shortestPerson.name << ' ' << shortestPerson.height << endl; }
618 : 구조체 - 자가진단6
#include <iostream> #include <string> using namespace std; struct Person { string name; int height; float weight; }; int main() { Person people[5]; for (int i = 0; i < 5; ++i) { cin >> people[i].name >> people[i].height >> people[i].weight; } cout.setf(ios::fixed); cout.precision(1); for (int i = 0; i < 5; ++i) { for (int j = 0; j < 4 - i; ++j) { if (people[j].name > people[j + 1].name) { swap(people[j], people[j + 1]); } } } cout << "name" << endl; for (int i = 0; i < 5; ++i) { cout << people[i].name << ' ' << people[i].height << ' ' << people[i].weight << endl; } cout << endl; for (int i = 0; i < 5; ++i) { for (int j = 0; j < 4 - i; ++j) { if (people[j].weight < people[j + 1].weight) { swap(people[j], people[j + 1]); } } } cout << "weight" << endl; for (int i = 0; i < 5; ++i) { cout << people[i].name << ' ' << people[i].height << ' ' << people[i].weight << endl; } }
195 : 구조체 - 형성평가1
#include <iostream> #include <string> using namespace std; struct Person { string name; string tel; string addr; }; int main() { Person people; cin >> people.name >> people.tel >> people.addr; cout << "name : " << people.name << endl; cout << "tel : " << people.tel << endl; cout << "addr : " << people.addr << endl; }
196 : 구조체 - 형성평가2
#include <iostream> #include <string> using namespace std; struct Person { string name; string tel; string addr; }; int main() { Person people[3]; for (int i = 0; i < 3; ++i) { cin >> people[i].name >> people[i].tel >> people[i].addr; } for (int i = 0; i < 3; ++i) { int fast{ i }; for (int j = i + 1; j < 3; ++j) { if (people[fast].name > people[j].name) fast = j; } swap(people[fast], people[i]); } cout << "name : " << people[0].name << endl; cout << "tel : " << people[0].tel << endl; cout << "addr : " << people[0].addr << endl; }
197 : 구조체 - 형성평가3
그림이 나왔지만 사실 간단하게 생각하면 왼쪽 아래 좌표는 더 작은 값을 선택하고 오른쪽 위 값은 더 큰 값을 선택하면 됩니다.
#include <iostream> using namespace std; struct Point { int x; int y; }; struct Box { Point lb; Point ru; }; int main() { Box a, b; cin >> a.lb.x >> a.lb.y >> a.ru.x >> a.ru.y; cin >> b.lb.x >> b.lb.y >> b.ru.x >> b.ru.y; Box c; c.lb.x = a.lb.x < b.lb.x ? a.lb.x : b.lb.x; c.lb.y = a.lb.y < b.lb.y ? a.lb.y : b.lb.y; c.ru.x = a.ru.x > b.ru.x ? a.ru.x : b.ru.x; c.ru.y = a.ru.y > b.ru.y ? a.ru.y : b.ru.y; cout << c.lb.x << ' ' << c.lb.y << ' ' << c.ru.x << ' ' << c.ru.y << endl; }
198 : 구조체 - 형성평가4
#include <iostream> using namespace std; struct Person { int height; float weight; }; int main() { Person f, m; cin >> f.height >> f.weight; cin >> m.height >> m.weight; Person c; c.height = (f.height + m.height) / 2 + 5; c.weight = (f.weight + m.weight) / 2 - 4.5f; cout.setf(ios::fixed); cout.precision(1); cout << "height : " << c.height << "cm\n"; cout << "weight : " << c.weight << "kg\n"; }
199 : 구조체 - 형성평가5
#include <iostream> #include <string> using namespace std; struct Person { string name; int score[3]; int total{ 0 }; }; int main() { int n; cin >> n; Person* people = new Person[n]; for (int i = 0; i < n; ++i) { cin >> people[i].name; for (int j = 0; j < 3; ++j) { cin >> people[i].score[j]; people[i].total += people[i].score[j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (people[j].total < people[j + 1].total) { swap(people[j], people[j + 1]); } } } for (int i = 0; i < n; ++i) { cout << people[i].name << ' '; for (int j = 0; j < 3; ++j) { cout << people[i].score[j] << ' '; } cout << people[i].total << endl; } delete[] people; }
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=10g0