cin >> K;
cin >> m;
// Initial array: 1 2 3 4 5 6 7 8 ... K
for ( int i = 1; i <= K; i++ )
a[i] = i;
for ( int i = 1; i <= m; i++ )
{
cin >> skip;
// Easy way (you can do it inside array a[ ] the hard way)
copy a[i] to b[ ]; but omit a[skip], a[2*skip], ...;
copy b[ ] back to a[ ]
}
|
int nLeft = K; // number of friends left: a[1..nLeft]
for ( int i = 1; i <= m; i++ )
{
cin >> skip;
/* -------------------------------------------
Copy not deleted friends in b[ ]
[1] [2] ... [skip] [skip+1]
^ skip
------------------------------------------- */
to = 1;
for ( from = 1; from <= nLeft; from++ )
if ( from%skip != 0 )
b[to++] = a[from];
/* --------------------------------------
Copy back to a[ ]
-------------------------------------- */
nLeft = to - 1; // Note: to = #elements copied !
for ( int j = 1; j <= nLeft; j++ )
a[j] = b[j];
}
|