Home > Back-end >  Good intention person to help solve the problem of a C language
Good intention person to help solve the problem of a C language

Time:10-16

A corridor, in turn, is equipped with n (1 n 65535) or less or less electric lamps, from head to tail number 1, 2, 3,... N, n - 1, each electric lamp is controlled by a pull switch, began, the lights all closed,
There are n students through the corridor, the first student number every 1 multiples of the light switch pull; Then the second student number every 2 multiples of the light switch and play; Then the third student number every multiple of 3 the light switch and play; And so it went on, the last student number n where n multiples of the light switch heave, and n students according to the regulations after the walk, corridor is the lights lit, there are a few
Note: the light number and student number is consistent,
Here is my application: I don't know why when the definition of n is odd number is not the output

# include "stdio.h"
# define n 6
Int main ()
{
Int I, j, a [n], k=0;
for(i=1; I<=n; I++)
A [I]=1;
for(i=2; I<=n; I++)
{
for(j=i; J<=n; J=j + I)
[j] a [j]=(a + 1) % 2;
}
for(i=1; I<=n; I++)
If (a [I]==1) k++;
Printf (" there are % d light on ", k);
return 0;
}

CodePudding user response:

The subscript C from zero beginning,
# include "stdio.h"
# define n 6
Int main ()
{
Int I, j, a (n + 1), k=0;
for(i=1; I<=n; I++)
A [I]=1;
for(i=2; I<=n; I++)
{
for(j=i; J<=n; J=j + I)
[j] a [j]=(a + 1) % 2;
}
for(i=1; I<=n; I++)
If (a [I]==1) k++;
Printf (" there are % d light on ", k);
return 0;
}

CodePudding user response:

Uh huh, array subscript begins with 0, a value of [n] is a [0] -- -- -- -- -- a [n - 1); If the value of a [n] array bounds,

CodePudding user response:

 
# pragma hdrstop
# define N 100
# include & lt; stdio.h>
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# pragma argsused
Int main (int arg c, char * argv [])
{
Int a [N].
Int I, j, light=0;
Memset (a, 1, N);
for(i=0; Ifor(j=0; JIf ((j + 1) % (I + 1)==0)
A [j]=-a [j];
for(j=0; JIf (a [j]==1)
Light++;
Printf (" light_sum: % d ", light);
getchar();
return 0;
}

CodePudding user response:

There is something wrong with the above
 
# pragma hdrstop
# define N 1000
# include & lt; stdio.h>
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# pragma argsused
Int main (int arg c, char * argv [])
{
Char a, [N].
Int I, j, light=0;
Memset (a, 1, N);
for(i=0; Ifor(j=0; JIf ((j + 1) % (I + 1)==0)
A [j]=-a [j];
for(j=0; JIf (a [j]==1)
{
Printf (" % d ", j + 1);
Light++;
}
Printf (" light_sum: % d ", light);
getchar();
return 0;
}
  • Related