杭电 2053(循环)

题意:有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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值