Note that matrices multiplication is associative: X times Y times Z = X times (Y times Z) = (X
times Y) times Z. Suppose we want to calculate the product of a series of matrices
multiplication: .A_1 times A_2 times A_3 times ... times A_l. The dimension of these matrices
are already given in an array dim, = (dim_1, dim_2, ..., dim_l+i), such that A_1 has dimensions
dim_1 times dim_2, A_2 has dimensions dim_2 times dim_3, etc. Describe a polynomial time
algorithm that determines the best way to associate A_1. A_2, ..., A_l such that the computation
cost is minimized? (The proof of correctness and time complexity are also required.)
Solution
matrix multiplication algo
. n = length[p] - 1
for i = 1 to n
do m[i, i] =0
for L =2 to n
do for i = 1 to n - L+1
do j ¬ i + L - 1
m[i, j] ¬ ¥
for k ¬ i to j - 1
do q ¬ m[i, k] + m[k+1, j] + pi-1 pk pj
if q < m[i, j]
then m[i, j] ¬ q
s[i, j] ¬ k
return m and s.
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
Objectives Create a Java program using programming fundamentals (fi.pdf
1. Objectives: Create a Java program using programming fundamentals (file I/O, loops, conditional
statements, arrays, functions) Problem: In preparation for the release of Guardians of the Galaxy
2, you have been hired by the owner of a small movie theater to develop the backend for an
online ticket reservation system. Patrons will be able to reserve seats in one of three auditoriums.
Once the patron has selected an auditorium, the program should display the current seating
arrangement and allow the patron to select seats. A report should be generated at the end of the
program to specify for each individual auditorium and overall for all auditoriums how many
seats were sold/unsold and how much money was earned.
Details
The seating arrangement for each auditorium will be stored in separate files. These files will be
named A1.txt, A2.txt, and A3.txt for auditorium 1, 2 and 3 respectively.
Each line in the file will represent a row in the auditorium. The number of rows in each
auditorium is unknown to you.
The number of seats in each row of a single auditorium will be the same. For example, if the
first line of the file has 15 seats, then every subsequent row in the theater will also have 15 seats.
This does not mean that each auditorium has the same number of seats in each row. One
auditorium may have 15 seats per row and another may have 20 seats.
Each auditorium will be held in a two-dimensional array.
Empty seats are represented by a pound sign (#).
Reserved seats are represented by a period (.).
Tickets can only be reserved the day of the screening and all screenings are at 7 PM that night.
There is no need to worry about multiple screenings or reserving seats on a specific day.
All tickets are $7 regardless of patron age or auditorium. User Interface and Input: Present a
user-friendly menu system for the user to select the auditorium. First, ask for the auditorium:
1. Auditorium 1
2. Auditorium 2
3. Auditorium 3
4. Exit
Although in reality the user would likely only make one purchase, for testing purposes, assume
the user will repeat the ticket buying process until they decide to quit.
Once the auditorium has been selected, display the current seating availability for that
auditorium. An example seating chart is provided below for an auditorium with 5 rows and 20
seats per row.
The seats are numbered sequentially from left to right and only the one's digit is displayed above
each column to make it easier to display the chart. It is understood that the second set of digits
2. from 1-0 are for the numbers 11- 20 in the above example. After the user has selected the
auditorium and the seating chart has been displayed, prompt the user for the following
information in the order below:
Row number
Starting seat number
Number of tickets
Assume that the user wants to reserve sequential seats to the right of the first seat entered. If the
desired seats are not available, offer the user the best available seats that meet their criteria on
that row only. The best available seats are the seats closest to the middle of the row.
Prompt the user to enter a Y to reserve the best available or N to refuse the best available. Once
the selection has been processed, return to the main menu. All input will be of the valid data
type. You do not have to worry about the user entering a letter when a number is expected or a
floating point number when an integer is expected. You are responsible for validating that the
data falls within the proper range and that a user does not try to reserve a seat that is already
reserved. Output: At the end of the program, write the current status of each auditorium to the
respective file. Also, display a formatted report to the console. The report should consist of 4
columns:
Column 1 – labels
o Auditorium 1
o Auditorium 2
o Auditorium 3
o Total
Column 2 - number of seats reserved for each label
Column 3 - the number of open seats for each label
Column 4 - the total of the ticket sales for each label
Here is the Pseudocode I have made and verified by the professor.
Read Auditorium
Arguments: auditorium array
Returns: auditorium array
Open file
While not EOF
o Read line
o For 0 to line length
Copy character in string to array
Close file
Display Auditorium
3. Arguments: auditorium array
Returns: nothing
Display column header
For each row
o Display row number
o For each column
Display array[row][col]
o Display newline
Reserve Seats
Arguments: auditorium array, row, seat, quantity
Returns: nothing
For 1 to quantity
o Array[row][seat+i] = #
Check Availability
Arguments: auditorium array, row, seat, quantity
Returns: Boolean
For 1 to quantity
o If array[row][seat+i] is a period
Return false
Return true
Count Seats
Arguments: auditorium array
Returns: open seats and reserved seats
Open file based on auditorium
For each row o For each column
If array[row][col] = #
Increment open seats
Else increment reserved seats
Print Report
Arguments: none
Returns: none
For 1 to 3
o Call Get Seat Info function
o Print out open seats, reserved seats, money earned
o Increment total open seats, total reserved seats, total money earned
Print out total open seats, total reserved seats, total money earned
4. Best Available
Arguments: auditorium array, row, quantity
Returns: left endpoint of best available section
Determine middle of row
From mid to 1 (i) o If array[row][i] = #
For 1 to quantity (j)
Check if array[row][i-j] = #
If all available
Left side = Hold i
From mid to last column (i) o If array[row][i] = #
For 1 to quantity (j)
Check if array[row][i+j] = #
If all available
Right side = Hold i
Return min (right side, left side - quantity)
Main
Loop (do…while not exit)
o Print main menu
o Get user input
o If user does not want to exit
Print auditorium
Get auditorium number
If user wants to reserve seats
Ask user for row, seat, and quantity
Check if seats are available
If available reserve seats
If not available search for best available
o If best available, offer to user
If user wants to see auditorium
Call display auditorium function
Print report
9 *# *## 5 .# .# 4 .# --# 3 .# -## 2# ---# # -### 0# -# -# 8## . .# 7 .# -## 6 -# --# 5## -## 4## . .#
3 .# -## 2 -# --# 1 .# -## 12345
Solution
5. Here is the full solution to the problem. Note that it has two classes: Audotorium and
AuditoriumTester(class with main method). Create two different classes in eclipse with the same
name as mentioned here.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Auditorium {
public static final String path = "D:Chegg";
//audiName should be same as the text file name.
public String[][] readAuditorium(String audiName){
String[][] auditorium = null;
BufferedReader br = null;
FileReader fr = null;
try{
fr = new FileReader(path+""+audiName);
br = new BufferedReader(fr);
String currentLine;
List rowList = new ArrayList();
while((currentLine=br.readLine())!=null){
char[] row = currentLine.toCharArray();
rowList.add(row);
}
int col = rowList.get(0).length;
auditorium = new String[rowList.size()][col];
for(int i=0;i9){
String num = i+"";
System.out.print(num.charAt(num.length()-1));
}
6. else{
System.out.print(i);
}
}
System.out.println();
for(int i=0;i countSeats(String[][] auditorium){
List countOpenReserve = new ArrayList();
int open =0;
int reserve =0;
for(int i=0;i seatsAudi1 = countSeats(audi1);
List seatsAudi2 = countSeats(audi2);
List seatsAudi3 = countSeats(audi3);
int totalOpenSeats = seatsAudi1.get(0)+seatsAudi2.get(0)+seatsAudi3.get(0);
int totalReserveSeats = seatsAudi1.get(1)+seatsAudi2.get(1)+seatsAudi3.get(1);
int totalSale = totalReserveSeats*7;
System.out.println("Auditorium1"+"tt"+seatsAudi1.get(1)+"tt"+seatsAudi1.get(0)+"t
"+"$"+seatsAudi1.get(1)*7);
System.out.println("Auditorium2"+"tt"+seatsAudi2.get(1)+"tt"+seatsAudi2.get(0)+"t
"+"$"+seatsAudi2.get(1)*7);
System.out.println("Auditorium3"+"tt"+seatsAudi3.get(1)+"tt"+seatsAudi3.get(0)+"t
"+"$"+seatsAudi3.get(1)*7);
System.out.println("Total"+"ttt"+totalReserveSeats+"tt"+totalOpenSeats+"t"+"$"
+totalSale);
}
}
Main Class
import java.util.Scanner;
public class AuditoriumTester {
public static void main(String[] args) {
String ch = "";
String audiName;
7. Scanner sc = new Scanner(System.in);;
String[][] audi =null;
Auditorium auditorium = new Auditorium();
do{
System.out.println("*******Select an Auditorium**********");
System.out.println("Auditorium 1: Enter 1");
System.out.println("Auditorium 2: Enter 2");
System.out.println("Auditorium 3: Enter 3");
System.out.println("Exit: Enter 4");
System.out.println("**************************************");
System.out.print("Your Input: ");
String choice = sc.next();
if(choice.equals("1")){
audi = auditorium.readAuditorium("audi1.txt");
audiName="audi1.txt";
}
else if(choice.equals("2")){
audi = auditorium.readAuditorium("audi2.txt");
audiName="audi2.txt";
}else if(choice.equals("3")){
audi = auditorium.readAuditorium("audi3.txt");
audiName="audi3.txt";
}else
break;
//Display the selected auditorium.
auditorium.displayAuditorium(audi);
System.out.println("Select your seats");
System.out.print("Row Number: ");
int row = sc.nextInt();
System.out.println();
System.out.print("Starting Seat Number: ");
int seat = sc.nextInt();
System.out.println();
System.out.print("Number of tickets: ");
8. int quantity = sc.nextInt();
if(auditorium.checkAvailability(audi, row, seat, quantity)){
System.out.println("Do you want to reserve seat(Y/N):");
String reserve = sc.next();
if(reserve.equalsIgnoreCase("y")){
auditorium.reserveSeats(audi, row, seat, quantity);
}else{
continue;
}
}else{
int bestSeat = auditorium.bestAvailable(audi, row, quantity);
if(bestSeat==0){
System.out.println("No seat available for your selection in this row. Please try
another row or change auditorium");
System.out.println("Do you want to continue(Y/N):");
String reserve = sc.next();
if(reserve.equalsIgnoreCase("y")){
continue;
}else{
break;
}
}
System.out.println("Your current choice is not available. However, you can choose
seat: "+bestSeat);
System.out.print("Do you want to reserve seat(Y/N):");
String reserve = sc.next();
if(reserve.equalsIgnoreCase("y")){
auditorium.reserveSeats(audi, row, bestSeat, quantity);
}else{
continue;
}
}
auditorium.printReport(audi, audiName);
System.out.print("Do you want to continue(y/n): ");
ch = sc.next();