Array126. Дано целое число L > 0 и целочисленный массив размера N. Заменить каждую серию массива, длина которой равна L, на один элемент с нулевым значением.
>Решение Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | program Array126; var a,temp : array [1..10] of real; LengthSeries,BeginSeries : array [1..10] of integer; N,i,i2,i3,NumSeries, L: integer; begin Write('N: '); Readln(N); Write('L: '); Readln(L); Writeln('A: '); for i:=1 to N do begin write(i,' :'); readln(a[i]); end; NumSeries:=1; LengthSeries[NumSeries]:=1; BeginSeries[NumSeries]:=NumSeries; for i:=2 to N do begin if a[i-1]=a[i] then LengthSeries[NumSeries]:=LengthSeries[NumSeries]+1 else begin inc(NumSeries); LengthSeries[NumSeries]:=1; BeginSeries[NumSeries]:=i; end; end; BeginSeries[NumSeries+1]:=N+1; i3:=0; for i:=1 to NumSeries do begin if LengthSeries[i]<>L then for i2:=BeginSeries[i] to BeginSeries[i+1]-1 do begin inc(i3); temp[i3]:=a[i2]; end else begin inc(i3); temp[i3]:=0; end; end; a:=temp; for i:=1 to i3 do writeln(a[i] ); end. |
>
Решение C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include int main(void) { int a[20],b[20]; int n,l; printf("N: "); scanf("%i",&n); printf("L: "); scanf("%i",&l); int i; for (i=0; i<n; ++i){ printf("a[%i] : ",i+1); scanf("%i",&a[i]); } int i2=-1,len=1,i3,endn=n; for (i=1; i<n; ++i){ if (a[i-1]!=a[i]){ if (len==l) { b[++i2]=0; endn-=len-1; } else { for (i3=0; i3<len; ++i3) b[++i2]=a[i-1]; } len=0; } ++len; } if (len==l) { b[++i2]=0; endn-=len-1; } else { for (i3=0; i3<len; ++i3) b[++i2]=a[i-1]; } for (i=0; i<endn;++i) a[i]=b[i]; for (i=0; i<endn;++i) printf("a %i : %i\n",i+1,a[i]); return 0; } |