Questito su un ciclo for

red3
Di recente ho trovato un quesito su un ciclo for. Penso che il linguaggio si C++, ma non è specificato.
Lo posto qui, nella sezione informatica, anche se andrebbe probabilmente nei giochi. Non mi torna per niente, soprattutto il fatto di trovare tre risposte. Il quesito è il seguente:
Puzzle: In Three Ways.
Find three ways to make the program below print 20 copies of the dash character '-' by changing or adding one character:

int i, n = 20;
for (i = 0; i < n; i--)
{
printf("-");
}
Incorrect solutions:
Changing "n = 20" to "n = -20" won't work.
Changing "i--" to "i++" doesn't satisfy the one character condition.
Changing "i = 0" to "i = 40" won't work.
Changing "i < n" to "i < -n" won't work.

Potete trovarlo qui: http://www.tanyakhovanova.com/Puzzles/i ... rogramming

Risposte
Raptorista1
"Print 20 copies" oppure "print exactly 20 copies"?

vict85
Penso che vada inteso come exactly.

@red3: hai risolto il problema? Può essere sia C che C++, ma non è molto importante[nota]Penso sinceramente che fosse stato ideato per l'ANSI C, quello di K&R per capirci. Infatti nel C++ è raro che la variabile del for sia definita fuori dal for.[/nota].


red3
"vict85":
Penso che vada inteso come exactly.

@red3: hai risolto il problema? Può essere sia C che C++, ma non è molto importante[nota]Penso sinceramente che fosse stato ideato per l'ANSI C, quello di K&R per capirci. Infatti nel C++ è raro che la variabile del for sia definita fuori dal for.[/nota].



no, per me manca di capire qualcos'altro o a livello di specifiche o di sintassi. comunque esattamente 20 caratteri

Raptorista1
Io credo di aver trovato una delle possibili soluzioni.

vict85
"Raptorista":
Io credo di aver trovato una delle possibili soluzioni.


Credi? Io ne ho trovata una e penso sia anche l'unica. Non ho provato a risolvere gli altri problemi:
pochmann and SnapDragon from TopCoder suggested entertaining and challenging variations to this problem (using the same restrictions):

Find a way to get it to print exactly 21 dashes.
Find a way to get it to print exactly 1 dash.
Find many ways to print infinitely many dashes.
Find many ways to print 0 dashes.
Print 20 dashes with the given code, without changing it at all, but you are allowed to add some preceding code.
Print 1 dash with the given code, without changing it at all, but you are allowed to add some preceding code.

red3
scusa, e quale sarebbe la tua soluzione?

anonymous_be1147

vict85
Ok, ne esistono almeno 2... Quella di stan e questa:


edit: Non mi ero reso conto che il testo diceva che ne esistevano 3.

Raptorista1
@vict intendevo dire che ne ho trovata una, non tre. Non ho guardato gli altri quesiti. La soluzione che ho trovato è la stessa riportata da Stan.

vict85
Trovata l'ultima :D


anonymous_be1147
@vict85 Bella!

Raptorista1
@vict nice! Che esercizio perverso XD :D

vict85
Ho trovato anche la/una soluzione per il 21 e una per l'1.

21:


1 (il mio editor formatta automaticamente, quindi fa sembrare che abbia aggiunto più caratteri di quelli che sembrano):


Alcune considerazioni su queste due soluzioni:

Rispondi
Per rispondere a questa discussione devi prima effettuare il login.