zoukankan      html  css  js  c++  java
  • Switch Game

    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

    Hint

    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 fif

    th operation : 1 0 0 1 0 … The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.

    分析:

    1.本题的意思是进行无穷次开关之后,输出第几盏灯的亮暗状态;只有在是i的倍数,才switch一次。偶数次的swtich是不变的0,奇数次才变化为1;

    因此只需要计算给定n中因子的奇偶性就可以了,代码1可以AC。

    2.再思考下,其实会发现乘积因子都是一对对的,如16=2*8=1*16=4*4 ,其实只要不是完全平方数,那因子总数一定是偶数,只有完全平方数才是奇数,这样只要判断是否完全平方数既可,很简单。

     
    //1,
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        int n,i;
        int count;
        while(scanf("%d",&n)!=EOF)
        {
            count = 0;
            for(i=1;i<=n;i++)
                if(n%i==0)
                    count++;
            if(count & 0x01)
                printf("1
    ");
            else
                printf("0
    ");
            /*
            printf("%d
    ",count &0x01);//判断奇数为1
            */
        }
        return 0;
    }
    //判断完全平方数)
    printf("%d
    ",((int)sqrt((double)n)*(int)sqrt((double)n))==n);
  • 相关阅读:
    响应式布局
    Margin是什么?
    分布式系统设计(1)
    Hadoop处理大量小文件的问题和解决方法
    Facebook揭密:如何让MySQL数据库集群自主运行
    大数据营销的优势
    LevelDB系列之SSTable(Sorted Strings Table)文件
    LevelDB系列之Log文件
    LevelDB系列之整体架构
    LevelDb系列之简介
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3187741.html
Copyright © 2011-2022 走看看