{ Если что, часть программы не нужна для построения цепочки. Она просто иллюстрирует, что полученный результат верен. }
var sq : array[0..999] of array[0..9] of boolean; co : array[0..999] of integer; ar : array[1..10003] of 0..9; i,j: integer; x: integer; t : boolean; begin for i := 0 to 999 do begin for j := 0 to 9 do sq[i][j] := false; co[i] := 0; end; for i := 1 to 3 do ar[i] := 0; i := 3; t := true; {write('000');} while t do begin i := i + 1; x := ar[i-3]*100 + ar[i-2]*10 + ar[i-1]; if co[x] >= 10 then t := false else begin j := 1; while sq[x][j] do j := (j + 1) mod 10; ar[i] := j; sq[x][j] := true; co[x] := co[x] + 1; {write(j)} end; end; {writeln;} writeln('Length: ',i - 1);
{просто чтобы убедиться} for i := 0 to 999 do for j := 0 to 9 do sq[i][j] := false;
t := true; j := 0; i := 1; while (i <= 10000) and t do begin x := ar[i] * 100 + ar[i+1] * 10 + ar[i+2]; if sq[x][ar[i+3]] then t := false else begin sq[x][ar[i+3]] := true; j := j + 1; end; i := i + 1 end; if t and (j = 10000) then write('Confirmed') end.
#include <iostream>
using namespace std;
int main()
{
#define B_LENGTH 10
// Variables
int b[B_LENGTH] = { 0 };
int bSum = 0;
// Input data
cout << "Input " << B_LENGTH << " numbers in your array." << endl;
cout << "Important condition, your number can be more then 1, but less then 7" << endl;
for (int i = 0; i < B_LENGTH; i++) {
while (b[i] <= 1 || b[i] >= 7) {
cout << "B[" << i << "] = ";
cin >> b[i];
}
}
// Output data
cout << "Good. Your array is:" << endl;
for (int i = 0; i < B_LENGTH; i++) {
bSum += b[i];
cout << b[i] << " ";
}
cout << endl << "Sum of the array elements is " << bSum << endl;
return 0;
}