Академический Документы
Профессиональный Документы
Культура Документы
struct PersonRecord {
string first_name;
string last_name;
int date_of_birth;
};
/*
The input will be two lists that are already sorted based on date of birth.
In most modern programming languages, a sorted list is usually represented
as an array or a vector where each element can be referenced by an index.
The algorithm below is a straightforward way to combine the two sorted lists.
*/
int i = 0; // index to track Dr A's list
int j = 0; // index to track Dr B's list
int k = 0; // index to track combined list
PersonRecord combined_list[drbest.length + drcushing.length];
while (i < drbest.length and j<drcushing.length)
{
if (drbest[i].date_of_birth < drcushing[j].date_of_birth)
{
combined_list[k] = drbest[i];
i = i + 1;
}
else
{
combined_list[k] = drcushing[j];
j = j + 1;
}
k = k + 1;
}
This loop will add the remaining records from Dr. Best's list:
This loop will add the remaining records from Dr. Cushing's list:
At this point both list were merged into one. To output the records,
just iterate on every record and print the names and date of birth:
*/
for (i = 0; i < combined_list.length; i=i+1)
{
print first_name, last_name, date_of_birth
}
count = 1; // count of people date of birth
date_of_birth = combined_list[0].date_of_birth
for (i = 1; i < combined_list.length; i=i+1)
{
if (combined_list[i].date_of_birth != date_of_birth)
{
print date_of_birth, count
count = 1 // reset the count
}
else
{
count = count + 1 // same date of birth as previous record
}
}
print date_of_birth, count