백준 10814 c++
#include <iostream>
#include <algorithm>
using namespace std;
struct profile {
int age = 0;
int num = 0;
string name;
};
int input(int lower, int upper);
bool cmp(profile a, profile b);
string input_string(int max_len);
void input_profile(profile* profile_array, int size);
void sort_struct_array(profile* profile_array, int size);
void print_struct_array(profile* profile_array, int size);
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
profile *profile_arr;
int i, N;
N = input(1, 100000);
profile_arr = new profile[N];
input_profile(profile_arr, N);
sort_struct_array(profile_arr, N);
print_struct_array(profile_arr, N);
delete[] profile_arr;
return 0;
}
int input(int lower, int upper)
{
int A;
while (1)
{
cin >> A;
if (A >= lower && A <= upper)
{
break;
}
else
{
;
}
}
return A;
}
void input_profile(profile* struct_array, int size)
{
int i;
for (i = 0; i < size; i++)
{
struct_array[i].age = input(1, 200);
struct_array[i].name = input_string(100);
struct_array[i].num = i;
}
return;
}
string input_string(int max_len)
{
string word;
cin >> word;
if (word.length() > max_len)
{
word = ' ';
}
else
{
;
}
return word;
}
void sort_struct_array(profile* struct_array, int size)
{
sort(struct_array, struct_array + size, cmp);
return;
}
bool cmp(profile a, profile b)
{
if (a.age == b.age)
{
return a.num < b.num; //입력은 빠른순
}
else
{
return a.age < b.age; //나이는 어린순
}
}
void print_struct_array(profile* struct_array, int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << struct_array[i].age << " " << struct_array[i].name << "\n";
}
return;
}