zoukankan      html  css  js  c++  java
  • nyoj 77-开灯问题 (倍数遍历)

    77-开灯问题

      内存限制:64MB 时间限制:3000ms 特判: No
      通过数:13 提交数:24 难度:1


    题目描述:

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

    输入描述:

    输入一组数据:n和k

    输出描述:

    输出开着的灯编号

    样例输入:

    7 3

    样例输出:

    1 5 6 7

    分析:
      1、通过数组存原先的状态;
      2、以其倍数关系遍历数组,改变该位置的状态

    C/C++代码实现(AC):
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <stack>
     7 #include <map>
     8 #include <queue>
     9 #include <set>
    10 
    11 using namespace std;
    12 const int MAXN = 1005;
    13 int f[MAXN] = {0};
    14 
    15 int main()
    16 {
    17     int n, k;
    18     scanf("%d%d", &n, &k);
    19     for (int i = 1; i <= k; ++ i)
    20     {
    21         for (int j = i; j <= n; j += i)
    22         {
    23             if (f[j] == 0) f[j] = 1;
    24             else f[j] = 0;
    25         }
    26     }
    27     for (int i = 1; i <= n; ++ i)
    28         if (f[i])
    29             printf("%d ", i);
    30     return 0;
    31 }
  • 相关阅读:
    11111 Generalized Matrioshkas
    Uva 442 Matrix Chain Multiplication
    Uva 10815 Andy's First Dictionary
    Uva 537 Artificial Intelligence?
    Uva 340 MasterMind Hints
    SCAU 9508 诸葛给我牌(水泥题)
    Uva 10420 List of Conquests(排序水题)
    Uva 409 Excuses, Excuses!
    10/26
    11/2
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9112727.html
Copyright © 2011-2022 走看看