Sie sind auf Seite 1von 5

10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems

UAB – 2006: Problem 1: Merging


at the Brick Yard

SEPTEMBER 16, 2009AUGUST 11, 2013 / SHAHAB

i
1 Vote

The Indianapolis 500 race is just a few weeks away. They need your
help in creating a program that will provide the order in which the
cars are to appear at the starting line (ordering is according to the
number placed on the car).

There are two garages where the race cars are stored. Each garage
will form a line of 5 cars that are sorted by the number on the car.
As the cars from the two garages merge to form a single start line,
your program must maintain the ordering such that the smallest
numbered cars are earlier in the line than those with larger
numbers.

Note: Some cars may share the same number, and the numbers
range from 1 to 10. You may assume that there are always 5 cars in
each garage prior to the merging and each garage is already sorted.

Example 1:
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 1/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems

Example 1:
Enter the order for garage 1: 1 1 2 3 4
Enter the order for garage 2: 2 2 3 4 5

The final merged order is:


1122233445

Example 2:
Enter the order for garage 1: 1 2 3 4 5
Enter the order for garage 2: 5 6 7 8 9

The final merged order is:


1234556789
?
Critical TestCases
1 Enter the order for garage 1: 1 1 1 1 1
2 Enter the order for garage 2: 1 1 1 1 1
3
4 The final merged order is:
5 1 1 1 1 1 1 1 1 1 1
6
7 Enter the order for garage 1: 5 4 3 2 1
8 Enter the order for garage 2: 1 2 3 4 5
9
10 The final merged order is:
11 1 1 2 2 3 3 4 4 5 5
12
13 Enter the order for garage 1: 9 8 7 6 5
14 Enter the order for garage 2: 4 3 2 1 0
15
16 The final merged order is:
17 0 1 2 3 4 5 6 7 8 9
18
19 Enter the order for garage 1: 1 2 3 4 5
20 Enter the order for garage 2: 9 8 7 6 5
21
22 The final merged order is:
23 1 2 3 4 5 5 6 7 8 9
24
25 Enter the order for garage 1: 2 3 2 3 3
26 Enter the order for garage 2: 2 3 3 2 2
27
28 The final merged order is:
29 2 2 2 2 2 3 3 3 3 3
?
Solution in C/C++

1 // @BEGIN_OF_SOURCE_CODE
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 2/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems

1 // @BEGIN_OF_SOURCE_CODE
2
3 #include <cstdio>
4 #include <cstdlib>
5
6 #define F(i, a, b) for ( int i = (a); i < (b); i++ )
7
8 int compare (const void *a, const void *b)
9 {
10 int *x = (int *) a;
11 int *y = (int *) b;
12
13 return *x - *y;
14 }
15
16 int main (int argc, char *argv [])
17 {
18 int array [10];
19
20 printf ("Enter the order for garage 1: ");
21 F (i, 0, 5) scanf ("%d", &array [i]);
22
23 printf ("Enter the order for garage 2: ");
24 F (i, 5, 10) scanf ("%d", &array [i] );
25
26 qsort (array, 10, sizeof (int), compare);
27
28 printf ("\nThe final merged order is:\n");
29
30 F (i, 0, 10) {
31 printf ("%d", array [i]);
32 if ( i < 9 ) printf (" ");
33 }
34
35 printf ("\n");
36
37
38 return 0;
39 }
40
41 // @END_OF_SOURCE_CODE

https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 3/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems
Advertisements

Easy Problems

2 thoughts on “UAB – 2006: Problem


1: Merging at the Brick Yard”

1. fokoruddin
DECEMBER 31, 2011 AT 6:09 PM
well

i
Rate This

2. haughki
SEPTEMBER 10, 2016 AT 6:55 AM
class CarMerge {
private static int LIST_LENGTH = 5;
String mergeCars(List list1, List list2){
String result = “”;
int index1 = 0;
int index2 = 0;
while (index1 < LIST_LENGTH || index2 = LIST_LENGTH) {
result += getRest(list2, index2);
break;
}
if (index2 >= LIST_LENGTH) {
result += getRest(list1, index1);
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 4/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems

result += getRest(list1, index1);


break;
}
Integer current1 = list1.get(index1);
Integer current2 = list2.get(index2);
if (Objects.equals(current1, current2)) {
result += current1.toString();
result += current2.toString();
index1++;
index2++;
} else if (current1 < current2) {
result += current1;
index1++;
} else {
result += current2;
index2++;
}
}
System.out.println(result);
return result;
}

private String getRest(List list, int index) {


String result = “”;
while (index < LIST_LENGTH) {
result += list.get(index);
index++;
}
return result;
}
}

i
Rate This

https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 5/5

Das könnte Ihnen auch gefallen