Turn on the lights--------"Algorithmic Competition introduction Guide" P83

Source: Internet
Author: User

Turn on the light.

There are n lamps, numbered 1~n. The 1th man turned all the lights on, and the 2nd man pressed the switches of all multiples numbered 2 (the lights would be turned off), and the 3rd man pressed the switch with multiples of 3 (where the lights were turned on, the lights turned off), and so on. A total of k individuals, asked the last which lights on? Enter n and k to output the number of lights that are open. k≤n≤1000.
Sample input:
3 ·
Sample output:
1 5 6 7

My Code is

#include <iostream>using namespacestd;intMainvoid){    intn,k; CIN>>n;//Number of lights ncin>>k;//number of people K    inti,j; intA[n]; J=1;  for(i=0; i<n;i++) {A[i]=1; }     for(j=2; j<=k;j++){         for(i=j-1; i<n;i+=j) {A[i]=-A[i]; }        }         for(i=0; i<n;i++){        if(a[i]==1) {cout<<i+1<<" "; }    }    return 0;}

Run results

Turn on the lights--------"Algorithmic Competition introduction Guide" P83

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.