Διάπκεια 3 ώπερ. Να απανηηθούν όλερ οι επωηήζειρ Ππόβλημα 1: Ανηιζηποθή 2 (Μονάδερ: 20) Να γξάςεηε έλα πξόγξακκα ην νπνίν δηαβάδεη από ην αξρείν input1.txt κηα ζεηξά ραξαθηήξσλ κήθνπο ην πνιύ 4,000,000,000 ζηνηρείσλ θαη ηελ ηππώλεη αληεζηξακκέλε ζην αξρείν output1.txt. Μεηά ην ηέινο ηεο εθηύπσζεο, ην πξόγξακκα ζαο πξέπεη λα ηππώζεη ζηελ νζόλε ηελ ζπρλόηεηα εκθάληζεο ησλ ραξαθηήξσλ ζε θζίλνπζα ζεηξά Υπόδεημε: Ππέπει λα ρξεζηκνπνηήζεη κηα αλαδξνκηθή δηαδηθαζία ηελ νπνία ζα νλνκάζεηε PushTheCharacters. Είζοδορ Μηα γξακκή πνπ απνηειείηαη από Ν ραξαθηήξεο (όπνπ, 1 Ν 4,000,000,000) θαη θάζε ραξαθηήξαο είλαη πεδόο ραξαθηήξαο ηνπ ιαηηληθνύ αιθαβήηνπ (a-z). Έξοδορ Στο αρχείο: Μηα γξακκή πνπ πεξηέρεη ηελ γξακκή εηζόδνπ αληεζηξακκέλε. Στην οθόνη: Μ γξακκέο πνπ ζα πεξηέρνπλ έλα ραξαθηήξα θαη ηελ ζπρλόηεηα εκθάληζεο ηνπ ρσξηζκέλα κε έλα θελό ραξαθηήξα (όπου, 1 Μ 26). Φαξαθηήξεο κε ηελ ίδηα ζπρλόηεηα εκθάληζεο κπνξνύλ λα εθηππώλνληε κε νπνηαδήπνηε ζεηξά. Παπάδειγμα Ειζόδορ qwertyqerr Εξόδορ Στο αρχείο: rreqytrewq Στην οθόνη: r 3 e 2 q 2 t 1 y 1 w 1 1 P a g e http://www.pspo.org.cy/
Duration 3 hours Answer all questions Problem 1: Reverse 2 (Points: 20) Write a program that reads from the file input1.txt, a series of characters with maximum length 4,000,000,000 elements and prints it in reverse order to the file output1.txt. After the end of the printing, your program should print on the screen the appearance frequency of the characters in a descending sequence. Note: You must use a recursive function to achieve this which will be named PushTheCharacters. A line that is composed from N characters (where, 1 N 4,000,000,000) and each character is a small caps Latin character (a-z). To the file: A line that contains the input series reversed. To the screen: M lines that contain a character and its appearance frequency separated by an empty character (where, 1 M 26). Characters with the same appearance frequency can be print in any order. Sample test data qwertyqerr To the file: rreqytrewq To the screen: r 3 e 2 q 2 t 1 y 1 w 1 2 P a g e http://www.pspo.org.cy/
Ππόβλημα 2: Κύβοι (Μονάδερ: 35) Σηελ επηθάλεηα ελόο ηξαπεδηνύ ππάξρνπλ N θύβνη αξηζκεκέλνη από ην 1 κέρξη ην Ν (όπνπ 1 N 100). Μηα ζεηξά από εληνιέο κεηαθηλεί ηνπο θύβνπο. Κάζε εληνιή απνηειείηαη από δύν αθέξαηνπο αξηζκνύο, A θαη B, θαη πξνζδηνξίδεη όηη ν θύβνο A κεηαθηλείηαη πάλσ ζηνλ θύβν B. Γηα παξάδεηγκα, ε εληνιή 1 3 ππνδεηθλύεη όηη ν θύβνο κε αξηζκό 1 κεηαθηλείηαη πάλσ ζηνλ θύβν κε αξηζκό 3. Η επηθάλεηα ηνπ ηξαπεδηνύ ππνδεηθλύεηαη κε ηνλ αξηζκό 0. Καηά ζπλέπεηα, ε εληνιή 7 0 ππνδεηθλύεη όηη ν θύβνο κε αξηζκό 7 πξέπεη λα κεηαθηλεζεί ζηελ επηθάλεηα ηνπ ηξαπεδηνύ. Οδεγίεο: Αλ ν θύβνο A έρεη κηα ζηνίβα από θύβνπο πάλσ ηνπ, ηόηε ε ζηνίβα παξακέλεη πάλσ ζηνλ A θαη κεηαθηλείηαη καδί κε ηνλ A. Αλ ν θύβνο B έρεη κηα ζηνίβα από θύβνπο πάλσ ηνπ ηόηε εθείλε ε ζηνίβα κεηαθηλείηαη πάλσ ζηελ επηθάλεηα ηνπ ηξαπεδηνύ σο έρεη, ώζηε λα κελ ππάξρεη θύβνο πάλσ ζηνλ B, θαη κόλν ηόηε ν A (κε νπνηνπζδήπνηε θύβνπο πάλσ ηνπ) κεηαθηλείηαη ζην πάλσ ζηνλ B. Να γξάςεηε έλα πξόγξακκα ην νπνίν ζα δέρεηαη από ην αξρείν input2.txt ην πιήζνο ησλ θύβσλ θαη ηηο εληνιέο θαη ζα εθηππώλεη ηελ ηειηθή θαηάζηαζε ησλ θύβσλ κεηά ην πέξαο ησλ εληνιώλ. Δεδομένα Ειζόδος Η πξώηε γξακκή είλαη έλαο αθέξαηνο αξηζκόο Ν πνπ ππνδεηθλύεη ηνλ αξηζκό ησλ θύβσλ πνπ βξίζθνληαη πάλσ ζηελ επηθάλεηα ηνπ ηξαπεδηνύ (όπνπ 1 N 100). Κάζε γξακκή πνπ αθνινπζεί είλαη κηα εληνιή. Κάζε εληνιή απνηειείηαη από δύν αθέξαηνπο αξηζκνύο (A θαη B) ρσξηζκέλνπο κε έλα θελό δηάζηεκα (0 A N και 0 B N). Η ηειεπηαία γξακκή ηνπ αξρείνπ εηζόδνπ ζα είλαη πάληα ε εληνιή 0 0, γηα ηελ νπνία θακηά ελέξγεηα δελ πξέπεη λα γίλεη. Θεσξείζηε όηη δελ ζα ππάξρνπλ κε έγθπξεο εληνιέο ζην αξρείν εηζόδνπ. Γηα παξάδεηγκα, ε εληνιή 0 3 είλαη κε έγθπξε θαη δελ ζα ππάξμεη πνηέ ζην αξρείν εηζόδνπ. Δεδομένα εξόδος Τν πξόγξακκα ζα πξέπεη λα ηππώλεη N γξακκέο ζην αξρείν εμόδνπ. Η πξώηε γξακκή ζα πξέπεη λα δείρλεη ηε ζηνίβα ησλ θύβσλ πάλσ από ηνλ θύβν κε αξηζκό 1. Η δεύηεξε λα δείρλεη ηε ζηνίβα ησλ θύβσλ πάλσ από ηνλ θύβν κε αξηζκό 2. Η n-ηνζηή γξακκή δείρλεη ηε ζηνίβα ησλ θύβσλ πάλσ από ηνλ θύβν κε αξηζκό n. 3 P a g e http://www.pspo.org.cy/
Πεξηγξαθή γξακκήο: Κάζε γξακκή μεθηλά κε ηελ αξίζκεζε ηνπ θύβνπ n αθνινπζνύκελε από κηα άλσ-θάησ ηειεία. Αλ ππάξρνπλ θύβνη πάλσ από ηνλ θύβν n ηόηε ε άλσ-θάησ ηειεία αθνινπζείηαη από έλα θελό δηάζηεκα θη έπεηηα ηελ ιίζηα ησλ θύβσλ πνπ βξίζθνληαη πάλσ από ηνλ θύβν n. Η ιίζηα ησλ θύβσλ πνπ αθνινπζεί ηελ άλσ-θάησ ηειεία, πξέπεη λα εθηππσζεί κε ηελ ζεηξά κε ηελ νπνία είλαη ζηνηβαζκέλνη, μεθηλώληαο από ηνλ θύβν πνπ βξίζθεηε πην ρακειά ζηελ ζηνίβα. Παπάδειγμα Ειζόδορ 5 3 1 5 3 3 4 2 3 3 0 4 2 0 0 Εξόδορ 1: 2: 4 3: 2 4 4: 5: 4 P a g e http://www.pspo.org.cy/
Problem 2: Blocks (Points: 35) There are N blocks on the surface of a table, numbered from 1 to N (where 1 N 100). A list of commands moves the blocks. Each command is composed of two integers, A and B, which indicates that block numbered A is moved above block numbered B. For example, the command 1 3 indicates that block with the number 1 is moved above block with the number 3. The surface of the table is denoted with the number 0. Therefore, the command 7 0 indicates that block with the number 7 should be moved on to the surface of the table. If block with number A has a stack of blocks above it, then that stack remains above A and is moved together with block A. If block with number B has a stack of blocks above it then that stack is moved to the surface of the table as is, so that there are no blocks above B, and then A (with any blocks above it) is moved on top of B. Write a program, that accepts from input file input2.txt the total number of blocks and the commands and prints the final state of the blocks after all commands have been followed. The first line is an integer indicating the number of blocks that are on the surface of the table. Each of the following lines in the file is a command. Each command is composed of two integers (A and B) separated with a single space (0 A N and 0 B N). The last line of the input file has the command 0 0, where no action should be carried out. Assume no invalid commands will appear in the input file. For example the command 0 3 is invalid and would never appear in the input file. Your program should print N lines. The first line indicates the stack of the blocks above block 1. The second line indicates the stack of blocks above block 2. The nth line indicates the stack of blocks above block n. 5 P a g e http://www.pspo.org.cy/
Line description: Each line starts with the block number n followed by a colon. If there are any blocks above block n then the colon is followed by a space, and then a list of blocks above block n. The list of block numbers after the colon has to appear in the order in which they are stacked, beginning with the block that is the lowest in the stack. Sample test data 5 3 1 5 3 3 4 2 3 3 0 4 2 0 0 1: 2: 4 3: 2 4 4: 5: 6 P a g e http://www.pspo.org.cy/
Ππόβλημα 3: Μέγιζηο Άθποιζμα (Μονάδερ: 45) Γίλεηαη από ην πιεθηξνιόγην έλαο 2-δηάζηαηνο πίλαθαο από ζεηηθνύο θαη αξλεηηθνύο αθέξαηνπο αξηζκνύο. Βξείηε ηνλ ππνπίλαθα κε ην κεγαιύηεξν άζξνηζκα θαη εθηππώζηε ζηελ νζόλε ην άζξνηζκα ηνπ θαη ηηο ζπληεηαγκέλεο (x,y) γηα ην πάλσ αξηζηεξά ζεκείν ηνπ πίλαθα θαη ην θάησ δέμηα ζεκείν πνπ πξνζδίνξίδνπλ ηηο ζπληεηαγκέλεο θαη ην κέγεζνο ηνπ. Τν άζξνηζκα ελόο πίλαθα είλαη ην άζξνηζκα όισλ ησλ ζηνηρείσλ ζε εθείλν ην πίλαθα. Ο ππνπίλαθαο κε ην κεγαιύηεξν άζξνηζκα ζα αλαθέξεηαη σο ν κέγηζηνο ππνπίλαθαο. Έλαο ππνπίλαθαο είλαη θάζε εληαίνο πίλαθα κέγέζνπο 1x1 ή κεγαιύηεξν. Παξάδεηγκα: Ο κέγηζηνο ππνπίλαθαο βξίζθεηαη ζηελ θάησ αξηζηεξή γσληά: θαη έρεη άζξνηζκα 15. Είζοδορ Η είζνδνο μεθηλά κε έλα αθέξαην αξηζκό N ζηελ πξώηε γξακκή ππνδεηθλύνληαο ην κέγεζνο ηνπ 2-δηάζηαηνπ ηεηξαγσληθνύ πίλαθα. Η δεύηεξε γξακκή ηνπ αξρείνπ εηζόδνπ απνηειείηαη από έλα NxN πίλαθα κε αθέξαηνπο αξηζκνύο, όπνπ 1 N 100. Οη αξηζκνί ηνπ πίλαθα ζα είλαη αξηζκνί κεηαμύ ηνπ -127 θαη ηνπ 127, ζπκπεξηιακβαλνκέλνπ. Έξοδορ Η πξώηε γξακκή ζα απνηειείηε από έλα αξηζκό ν νπνίνο ζα είλαη ην άζξνηζκα ηνπ κέγηζηνπ ππνπίλαθα. Σηελ δεύηεξε γξακκή ζα βξίζθνληαη δύν αξηζκνί, νη νπνίνη ζα πξνζδηνξίδνπλ ηηο ζπληεηαγκέλεο ηνπ πάλσ αξηζηεξά ζεκείνπ. 7 P a g e http://www.pspo.org.cy/
Σηελ ηξίηε γξακκή ζα βξίζθνληαη δύν αξηζκνί, νη νπνίνη ζα πξνζδηνξίδνπλ ηηο ζπληεηαγκέλεο ηνπ θάησ αξηζηεξά ζεκείνπ. Παπάδειγμα Ειζόδορ 4 0-2 -7 0 9 2-6 2-4 1-4 1-1 8 0-2 Εξόδορ 15 0 1 1 3 8 P a g e http://www.pspo.org.cy/
Problem 3: Maximum Sum (Points: 45) You will be given from the keyboard a 2-dimensional array of positive and negative integers, find the sub-array with the largest sum and print on the screen the sum and the coordinates (x,y) of the top left point of the sub-array and the bottom right point which define the coordinates and the size of the sub-array. Example: The sum of an array is the sum of all the elements in that array. The sub-array with the largest sum is referred to as the maximal sub-array. A sub-array is any contiguous area of size 1x1or greater located within the whole array. is in the lower-left-hand corner: and has the sum of 15. The input consists of an NxN array of integers, where 1 N 100. The input begins with a single positive integer N on a line by itself indicating the size of the square two dimensional array. The second line of the input file has N 2 integers separated by spaces. The numbers in the array will be in the range -127 to 127, inclusive. The first line will contain one number which will be the sum of the maximal sub-array. In the second line there will be two numbers which define the coordinates of the upper left point. In the second line there will be two numbers which define the coordinates of the bottom right point. Sample test data 9 P a g e http://www.pspo.org.cy/
4 0-2 -7 0 9 2-6 2-4 1-4 1-1 8 0-2 15 0 1 1 3 10 P a g e http://www.pspo.org.cy/