HDU-2053

Switch Game
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20444 Accepted Submission(s): 12547

Problem Description
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).

Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.

Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).

Sample Input
1
5

Sample Output
1
0

解题思路:

其实本题的目的就是求各个执行步骤,是否为输入的约数。即为求约数问题,求出约数的个数可以判断开关变换的次数,就能够确定最后的开关状态。

代码如下:

#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        int flag = 0;
        //判断执行的每个步骤的序号是否为n的约数。 
        for(int i = 1;i <= 100000;i++)
        {
            if(n%i==0)
            {
                flag++;     //是则标记加1 
            }
        }
        //如果标记为偶数,即为变化偶数次则输出0 
        if(flag%2==0)
            cout<<"0"<<endl;
        //如果标记为奇数,则输出1 
        else
            cout<<"1"<<endl;
    }
    return 0;
}
posted @ 2018-02-11 22:44  Western_Trail  阅读(143)  评论(0编辑  收藏  举报