Water collected between towers: Difference between revisions
Content added Content deleted
(→{{header|QuickBASIC}}: Added a solution.) |
(→{{header|Nascom BASIC }}: Added a solution.) |
||
Line 970: | Line 970: | ||
Block 7 holds 0 water units.</pre> |
Block 7 holds 0 water units.</pre> |
||
==={{header|Nascom BASIC}}=== |
|||
{{trans|FreeBasic}} |
|||
{{works with|Nascom ROM BASIC|4.7}} |
|||
<syntaxhighlight lang="basic"> |
|||
10 REM Water collected between towers |
|||
20 MXN=19 |
|||
30 REM Heights of towers in each city |
|||
40 REM prefixed by the number of towers |
|||
50 DATA 5,1,5,3,7,2 |
|||
60 DATA 10,5,3,7,2,6,4,5,9,1,2 |
|||
70 DATA 16,2,6,3,5,2,8,1,4,2,2,5,3,5,7,4,1 |
|||
80 DATA 4,5,5,5,5 |
|||
90 DATA 4,5,6,7,8 |
|||
100 DATA 4,8,7,7,6 |
|||
110 DATA 5,6,7,10,7,6 |
|||
120 DIM A(MXN,1) |
|||
130 FOR I=1 TO 7 |
|||
140 READ N |
|||
150 FOR J=0 TO N-1 |
|||
160 READ A(J,0) |
|||
170 A(J,1)=J |
|||
180 NEXT J |
|||
190 GOSUB 390 |
|||
200 IF A(0,1)>=A(1,1) THEN 220 |
|||
210 FRST=A(0,1):LST=A(1,1):GOTO 230 |
|||
220 FRST=A(1,1):LST=A(0,1) |
|||
230 WTR=A(1,0)*(LST-FRST-1) |
|||
240 FOR J=2 TO N-1 |
|||
250 IF FRST>=A(J,1) OR A(J,1)>=LST THEN 270 |
|||
260 WTR=WTR-A(J,0) |
|||
270 IF A(J,1)>=FRST THEN 300 |
|||
280 WTR=WTR+A(J,0)*(FRST-A(J,1)-1) |
|||
290 FRST=A(J,1) |
|||
300 IF A(J,1)<=LST THEN 330 |
|||
310 WTR=WTR+A(J,0)*(A(J,1)-LST-1) |
|||
320 LST=A(J,1) |
|||
330 NEXT J |
|||
340 PRINT "Bar chart";I;"collected"; |
|||
350 PRINT WTR;"units of water." |
|||
360 NEXT I |
|||
370 END |
|||
380 REM ** ShellSort |
|||
390 GAP=N-1 |
|||
400 GAP=INT(GAP/2.2) |
|||
410 IF GAP=0 THEN GAP=1 |
|||
420 FOR K=GAP TO N-1 |
|||
430 TH=A(K,0):TP=A(K,1) |
|||
440 L=K |
|||
450 IF L<GAP THEN 500 |
|||
460 IF A(L-GAP,0)>=TH THEN 500 |
|||
470 A(L,0)=A(L-GAP,0):A(L,1)=A(L-GAP,1) |
|||
480 L=L-GAP |
|||
490 GOTO 450 |
|||
500 A(L,0)=TH:A(L,1)=TP |
|||
510 NEXT K |
|||
520 IF GAP<>1 THEN 400 |
|||
530 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Bar chart 1 collected 2 units of water. |
|||
Bar chart 2 collected 14 units of water. |
|||
Bar chart 3 collected 35 units of water. |
|||
Bar chart 4 collected 0 units of water. |
|||
Bar chart 5 collected 0 units of water. |
|||
Bar chart 6 collected 0 units of water. |
|||
Bar chart 7 collected 0 units of water. |
|||
</pre> |
|||
==={{header|QuickBASIC}}=== |
==={{header|QuickBASIC}}=== |