码迷,mamicode.com
首页 > 其他好文 > 详细

水题 开灯问题

时间:2014-07-19 20:34:58      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   数据   

开灯问题
时间限制: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

bubuko.com,布布扣
#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

嗯 我是判断奇数或者偶数

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

 

水题 开灯问题,布布扣,bubuko.com

水题 开灯问题

标签:style   blog   http   color   os   数据   

原文地址:http://www.cnblogs.com/qscqesze/p/3849529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!