Algorithm Competition Introduction Classic example 3-1 turn on the light problem

Source: Internet
Author: User
Tags switches

There are n lights, numbered 1~n, the 1th person to turn all the lights on, the 2nd person presses all the switches numbered in multiples of 2 (these lights will be turned off), the 3rd person presses all the switches numbered in multiples of 3 (where the lights are turned on, the lights are turned off), and so on. A total of k individuals, asked the last which lights on?

Input: N and K, the output light number of the open. k≤n≤1000.

Sample Input: 7 3

Sample output: 1 5 6 7

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 10int A[maxn];int Main ( int argc, char *argv[]) {  int i, J, N, K, first = 1;  scanf ("%d%d", &n, &k);  Memset (A,-1, sizeof (a));  for (i = 1; l <= K; i++) for     (j = 1; J <= N; j + +)     {        if (j%i = = 0) A[j] =-a[j];     }     /*   for (j = 1; J <= N; j + +)     if (a[j] = = 1) printf ("%d", j); Finally there should be no space */For                                   (j = 1; J <= N; + j)     if (a[j] = 1) {if (first) first = 0; else printf (""); printf ("%d", j);}  printf ("\ n");     Finally, the output line break   system ("PAUSE");  return 0;}

Summary: 1 Use a flag to determine whether the first output, if, do not output space, or lose the first space after the loss of digital

2 switching lights can also be used 0 1

Algorithm Contest Introduction Classic example 3-1 open light problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.