Docsity

Examen programacion 1, Exámenes de Algoritmos y Programación

Toggle sidebar
bg1
Solutions and Evaluation Criteria for Exam #1
(05/10/2015)
Professors of PRO1
October 13, 2015
1 General comments
The third and fourth exercises of each turn of the exam require writing loops
where the number of iterations is known beforehand. The structure and legibility
greatly improves if
for
-loops are used instead of
while
on those cases; the use
of a
while
loop might be eventually be penalized as a minor error, especially if
for and while loops are mixed in an inconsistent way, e.g., use a while to traverse
rows of a rectangle and a for for the columns.
We highly encourage you to read the full document, not only the solutions
to your turn. For convenience, the statements of all the exercises are collected
at the end of this document. Of course, all solutions given here are examples of
correct solutions, but many others are possible.
2 Turn #1
2.1 X13951: Sum of two numbers
#include <iostream >
us ing name s p a ce std;
int ma in ( ) {
int x, y;
wh ile ( cin > > x >> y ) {
co ut << x + y << endl ;
}
}
2.2 X81458: How many positives and how many negatives
#include <iostream >
us ing namespace s td ;
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Vista previa parcial del texto

¡Descarga Examen programacion 1 y más Exámenes en PDF de Algoritmos y Programación solo en Docsity!

Solutions and Evaluation Criteria for Exam

Professors of PRO

October 13, 2015

1 General comments

The third and fourth exercises of each turn of the exam require writing loops where the number of iterations is known beforehand. The structure and legibility greatly improves if for-loops are used instead of while on those cases; the use of a while loop might be eventually be penalized as a minor error, especially if for and while loops are mixed in an inconsistent way, e.g., use a while to traverse rows of a rectangle and a for for the columns. We highly encourage you to read the full document, not only the solutions to your turn. For convenience, the statements of all the exercises are collected at the end of this document. Of course, all solutions given here are examples of correct solutions, but many others are possible.

2 Turn

2.1 X13951: Sum of two numbers

include < iostream >

using namespace std ;

int main () { int x , y ;

while ( cin >> x >> y ) { cout << x + y << endl ; } }

2.2 X81458: How many positives and how many negatives

include < iostream >

using namespace std ;

int main () { // pos = number of positive elements read // neg = number de negative elements read // tot = total number of elements read // ( including 0 s ) int pos = 0; int neg = 0; int tot = 0; int x ; while ( cin >> x ) { if ( x > 0) ++ pos ; if ( x < 0) ++ neg ; ++ tot ; } cout << " Pos : " << pos << endl ; cout << " Neg : " << neg << endl ; cout << " Tot : " << tot << endl ; }

2.3 X17307: Maximum distance between consecutive ele-

ments

include < iostream >

using namespace std ;

int main () {

int n ; while ( cin >> n ) { // processing one case : a sequence of length n

// maxdist = maximum distance between consecutive elements // seen so far // curr = currently read element = x_i , 1 < i <= n // prev = previous to curr = x_ {i -1} , 1 < i <= n int maxdist = 0; int prev , curr ; cin >> prev ; for ( int i = 2; i <= n ; ++ i ) { cin >> curr ; int dist = curr - prev ; if ( dist < 0) dist = - dist ; if ( dist > maxdist ) maxdist = dist ; prev = curr ; } cout << maxdist << endl ;

int dist_main_diag ; if ( j < i ) dist_main_diag = i - j ; else dist_main_diag = j - i ; if ( mindist > dist_main_diag ) mindist = dist_main_diag ; int dist_inv_diag ; if ( n +1 < i + j ) dist_inv_diag = i +j -n -1; else dist_inv_diag = n +1 -i - j ; if ( mindist > dist_inv_diag ) mindist = dist_inv_diag ; cout << mindist % 10; }; cout << endl ; } } }

3 Turn #

3.1 X75658: Write x times a string

include < iostream >

using namespace std ;

int main () { string s ; int x ; cin >> x ; cin >> s ; for ( int i = 1; i <= x ; ++ i ) cout << s << endl ; }

3.2 X71650: Sum of positives and sum of negatives

The solution below uses the operator +=. Using this operator is not penalized; it is actually recommended as it can increase the legibility and clarity of the code. The statement x += z ;

is equivalent to

x = x + z ;

and can be read as add z to x. In general, if ⊗ is a binary operator (for instance, - or %), then

x ⊗= z ;

is equivalent to

x = x ⊗ z ;

include < iostream >

using namespace std ;

int main () { // sumpos = sum of positive elements // sumneg = sum of negative elements int sumpos = 0; int sumneg = 0; int x ; while ( cin >> x ) { if ( x > 0) sumpos += x ; if ( x < 0) sumneg += x ; }

int altp = 0; int curr ; cin >> curr ; bool is_positive_prev = curr > 0; for ( int i = 2; i <= n ; ++ i ) { cin >> curr ; bool is_positive_curr = curr > 0; if ( is_positive_curr != is_positive_prev ) ++ altp ; is_positive_prev = is_positive_curr ; } cout << altp << endl ; } }

4 X39064: Rectangles with minimun distance to

center or corners

In exercise X13464 (the fourth exercise of Turn #1) we have used the convention of indexing rows and columns from 1 to n. While it is perfectly OK to do the same here, we will be using a dierent convention, namely, indexing rows from 0 to n − 1 and columns from 0 to m − 1. This last convention could have been used to solve X13464. Whatever convention we use the resulting algorithms dier only in minor details and the structure does not cahnge at all. Recall that the distance (when we only count horizontal and vertical steps) between two valid positions (i, j) and (i′, j′) of a rectangle n × m is

|i − i′| + |j − j′|.

For example, the distance from (i, j) to the corner (0, 0) is i + j the distance to the corner (n − 1 , m − 1) is n − 1 − i + m − 1 − j, and the distance to the up border is i, that is, the distance from (i, j) to (0, j). For those who know: the denition and use of new functions or the use of existing functions (e.g., the function min from the library cmath) would notice- ably improve the clarity and legibility of the program, but the statement of all exercises explicitly disallowed using functions.

include < iostream >

using namespace std ;

int main () { bool first = true ; int n , m ; while ( cin >> n >> m ) { if ( first ) first = false ; else

cout << endl ; // print the rectangle n x m for ( int i = 0; i < n ; ++ i ) { for ( int j = 0; j < m ; ++ j ) { // mindist = minimum distance from (i , j ) to a corner // or to the center // = min ( i +j ,n -1 - i +j ,m -1 - j +i ,m -1 - j +n -1 -i , // |bn /2c-i |+|bm /2c-j |) // i + j = distance to (0 ,0) // n -1 - i + j = distance to (n -1 ,0) // m -1 - j + i = distance to (0 ,m -1) // m -1 - j +n -1 - i = distance to (n -1 ,m -1) // | n /2 - i |+| m /2 - j | = distance to center int mindist = i + j ; if ( m - 1 - j + i < mindist ) mindist = m - 1 - j + i ; if ( n - 1 - i + j < mindist ) mindist = n - 1 - i + j ; if ( m - 1 - j + n - 1 - i < mindist ) mindist = m - 1 - j + n - 1 - i ; int dc1 = n /2 - i ; if ( dc1 < 0) dc1 = - dc1 ; int dc2 = m /2 - j ; if ( dc2 < 0) dc2 = - dc2 ; if ( dc1 + dc2 < mindist ) mindist = dc1 + dc2 ; cout << mindist % 10; } cout << endl ; } } }

Jutge.org

The Virtual Learning Environment for Computer Programming

How many positives and how many negatives X81458 en

Given a list of integer numbers a 1 ,... , an , we want to count how many of them are strictly positive, how many of them are strictly negative, and how many are in total. For example,

in the list 1, 2, −3, 0, 4, −5 we have 3 positive values, 2 negative values, and 6 values in total.

Note: It is not allowed to use functions, vectors and any other way to store massive data. The solution must deal with the input data sequentially without storing arbitrarily large intermediate memory. Score: 2.5 points over 10 (100% automatic)

Input

The input consists in an arbitrary non-empty list of integer numbers in one line.

Output

The output describes the number of positive values, the number of negative values, and the total number of values in the list. Follow the format of the examples.

Sample input 1

1 2 -3 0 4 -

Sample output 1

Pos: 3 Neg: 2 Tot: 6

Sample input 2

0 0 0 0 0 0 0 0

Sample output 2

Pos: 0 Neg: 0 Tot: 8

Sample input 3

-1 -2 -3 -

Sample output 3

Pos: 0 Neg: 4 Tot: 4

Sample input 4

1 2 3 4

Sample output 4

Pos: 4 Neg: 0 Tot: 4

Problem information

Author : Guillem Godoy Generation : 2015-10-03 08:35:

© Jutge.org , 2006–2015. http://www.jutge.org

Jutge.org

The Virtual Learning Environment for Computer Programming

Maximum distance between consecutive elements X17307 en

Given a list of integer numbers a 1 ,... , an , we want to compute the maximum distance be-

tween consecutive pairs. For example, if the list is 1, −3, 0, 5, 7, then the distance between 1

and −3 is 4, the distance between −3 and 0 is 3, the distance between 0 and 5 is 5 and the

distance between 5 and 7 is 2. Thus, the maximum distance between consecutive elements is 5.

Note: It is not allowed to use functions, vectors and any other way to store massive data. The solution must deal with the input data sequentially without storing arbitrarily large intermediate memory. Score: 2.5 points over 10 (50% automatic, 50% human)

Input

The input has several cases, each one described in one line. For each case, we have a natural

number n ( n ≥ 2) followed by n integer numbers a 1 ,... , an.

Output

The output has the answer to the problem in one line for each case.

Sample input

5 1 -3 0 5 7 5 1 2 3 4 5 5 -1 -2 -3 -4 - 2 1 1 4 1 2 1 2 5 -1000000000 0 1000000000 0 -

Sample output

5 1 1 0 1 1000000000

Problem information

Author : Guillem Godoy Generation : 2015-10-03 08:36:

© Jutge.org , 2006–2015. http://www.jutge.org

Sample input 2

45

Sample output 2

000000000000000000000000000000000000000000000 001111111111111111111111111111111111111111100 010122222222222222222222222222222222222221010 011012333333333333333333333333333333333210110 012101234444444444444444444444444444432101210 012210123455555555555555555555555554321012210 012321012345666666666666666666666543210123210 012332101234567777777777777777765432101233210 012343210123456788888888888887654321012343210 012344321012345678999999999876543210123443210 012345432101234567890000098765432101234543210 012345543210123456789010987654321012345543210 012345654321012345678909876543210123456543210 012345665432101234567898765432101234566543210 012345676543210123456787654321012345676543210 012345677654321012345676543210123456776543210 012345678765432101234565432101234567876543210 012345678876543210123454321012345678876543210 012345678987654321012343210123456789876543210 012345678998765432101232101234567899876543210 012345678909876543210121012345678909876543210 012345678900987654321010123456789009876543210 012345678901098765432101234567890109876543210 012345678900987654321010123456789009876543210 012345678909876543210121012345678909876543210 012345678998765432101232101234567899876543210 012345678987654321012343210123456789876543210 012345678876543210123454321012345678876543210 012345678765432101234565432101234567876543210 012345677654321012345676543210123456776543210 012345676543210123456787654321012345676543210 012345665432101234567898765432101234566543210 012345654321012345678909876543210123456543210 012345543210123456789010987654321012345543210 012345432101234567890000098765432101234543210 012344321012345678999999999876543210123443210 012343210123456788888888888887654321012343210 012332101234567777777777777777765432101233210 012321012345666666666666666666666543210123210 012210123455555555555555555555555554321012210 012101234444444444444444444444444444432101210 011012333333333333333333333333333333333210110 010122222222222222222222222222222222222221010 001111111111111111111111111111111111111111100 000000000000000000000000000000000000000000000

Problem information

Author : Guillem Godoy Generation : 2015-10-03 13:33:

© Jutge.org , 2006–2015. http://www.jutge.org

Jutge.org

The Virtual Learning Environment for Computer Programming

Write x times a string X75658 en

Given a natural x and a string s as input, we must write x times s , with an end of line after each s.

Note: It is not allowed to use functions, vectors and any other way to store massive data. The solution must deal with the input data sequentially without storing arbitrarily large intermediate memory. Score: 2.5 points over 10 (100% automatic)

Input

The input has a natural number x ≥ 1 and a non-empty string s (formed by lowercase latin

letters).

Output

You must write x lines with the string s in each of them.

Sample input 1

5 hello

Sample output 1

hello hello hello hello hello

Sample input 2

10 goodbye

Sample output 2

goodbye goodbye goodbye goodbye goodbye goodbye goodbye goodbye goodbye goodbye

Sample input 3

1 end

Sample output 3

end

Problem information

Author : Guillem Godoy Generation : 2015-10-03 13:17:

© Jutge.org , 2006–2015. http://www.jutge.org

Jutge.org

The Virtual Learning Environment for Computer Programming

Number of positive-negative alternation X42771 en

Given a list of integer numbers a 1 ,... , an different from 0, we want to count the number of

consecutive pairs with different sign, that is, the amount pairs ( ai , ai + 1 ) for indices i holding

1 ≤ i < n such that either ai is positive and ai + 1 is negative, or ai is negative and ai + 1 is pos-

itive. For example, the list 3, 4, −5, 1, 2, −3, −2 has 3 of such pairs: (4, − 5 ), (−5, 1), (2, − 3 ).

Note: It is not allowed to use functions, vectors and any other way to store massive data. The solution must deal with the input data sequentially without storing arbitrarily large intermediate memory. Score: 2.5 points over 10 (50% automatic, 50% human)

Input

The input has several cases, each one described in one line. For each case, we have a natural

number n ( n ≥ 2) followed by n integer numbers a 1 ,... , an.

Output

The output has the answer to the problem in one line for each case.

Sample input

8 1 -1 1 -1 1 -1 1 - 8 1 2 3 4 5 6 7 8 8 -1 -2 -3 -4 -5 -6 -7 - 8 -4 -3 -2 -1 1 2 3 4 8 1 -1 2 -2 3 4 -3 - 7 3 4 -5 1 2 -3 - 2 1 1 2 -1 - 2 -1 1 2 1 -

Sample output

7 0 0 1 5 3 0 0 1 1

Problem information

Author : Guillem Godoy and Conrado Martinez Generation : 2015-10-03 13:23:

© Jutge.org , 2006–2015. http://www.jutge.org

Jutge.org

The Virtual Learning Environment for Computer Programming

Rectangles with minimun distance to center or corners X39064 en

Given odd natural numbers n , m , print a n × m rectangle of digits, where each digit repre-

sents the minimum distance (in number of horizontal and vertical movements) to the center or to one of the corners, printed modulo 10. Follow the format of the examples.

Note: It is not allowed to use functions, vectors and any other way to store massive data. The solution must deal with the input data sequentially without storing arbitrarily large intermediate memory. Score: 2.5 points over 10 (50% automatic, 50% human)

Input

The input has several cases, each one described in one line. For each case, we have a two odd natural numbers n , m.

Output

The output has the answer to the problem for each case. Separate two consecutive cases with a blank line.

Sample input 1

5 5 5 3 5 1 3 5 1 5 7 11

Sample output 1

01210 12121 21012 12121 01210

010 111 101 111 010

0 1 0 1 0

01110 11011 01110

01010

01234343210 12343234321 23432123432 34321012343 23432123432 12343234321 01234343210