水题 开灯问题

开灯问题
时间限制:3000 ms  |  内存限制:65535 KB
难度:1

描述
    有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 的倍数的开关(这些灯将被关掉),第3 个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依此类推。一共有k个人,问最后有哪些灯开着?输入:n和k,输出开着的灯编号。k≤n≤1000

输入
    输入一组数据:n和k
输出
    输出开着的灯编号
样例输入

    7 3

样例输出

    1 5 6 7

#include<iostream>  
#include<string.h>  
#include<stdio.h>  
#include<ctype.h>  
#include<algorithm>  
#include<stack>  
#include<queue>  
#include<set>  
#include<math.h>  
#include<vector>  
#include<map>  
#include<deque>  
#include<list>  
using namespace std;  
int main()
{
    int n,k,first=1;
    int a[1010];
    memset(a,0,sizeof(a));
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        for(int m=1;m<=k;m++)
        {
            if(i%m==0)
            a[i]=a[i]+1;
        }
    }
    for(int y=1;y<=n;y++)
    {
        if(a[y]%2!=0)
        printf("%d ",y);
    }
    return 0;
} 
View Code

嗯 我是判断奇数或者偶数

判断方法有很多 你们随意~

 

posted @ 2014-07-16 20:32  qscqesze  阅读(345)  评论(0编辑  收藏  举报