题意:有n个灯初始状态为0(关),从1开始对它操作,每次这些灯只要是1的倍数,那么就取反(0到1或者1到0)。直到操作到n,最后输出第n个灯的状态。
例如:Consider the second test case:
The initial condition : 0 0 0 0 0 …
After the first operation : 1 1 1 1 1 …
After the second operation : 1 0 1 0 1 …
After the third operation : 1 0 0 0 1 …
After the fourth operation : 1 0 0 1 1 …
After the fifth operation : 1 0 0 1 0 …
The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.
思路:主要是循环的基础使用。
#include<stdio.h>
int main()
{
int a[100005],i;
for(i =0; i < 100005; i++)
a[i] = 0;
int n;
while(scanf("%d",&n) != EOF)
{
int j,N = 1;
for(i = 1; i <= n; i++)
{
N = 1; //注意不可少
for(j = i; j <= n; j = i*N)
{
if(a[j] == 0)
a[j] = 1;
else
a[j] = 0;
N++;
}
}
printf("%d\n",a[n]);
for(i =1; i <= n; i++)
a[i] = 0;
}
return 0;
}