ACM BASIC problem--hundred lamps

Source: Internet
Author: User
Description

There are m lights, numbered for 1~m, respectively controlled by the corresponding m-switch. Start with all the switches facing up (up for, light on), and then do the following: numbers usually 1 times the lamp in the opposite direction dial a switch; is a multiple of 2 of the lamp again in the opposite direction dial a switch, is 3 times the lamp and the opposite direction to dial a switch, ..., until the multiple of M is the lamp again direction dial a switch. Please input an integer m from the keyboard to represent the number of lights, find out the number of lights (not lit) that are extinguished and the number and output.
Input

Enter an integer m (1<=n<=100).

Output

The output is two lines, the first line is the number of lights that are out of state, and the second line is the number of the last light that was extinguished (each data is displayed as a field width of 4 columns).
Sample Input

100

Sample Output
   1   4   9   81 100

HINT
Output control is%4d
Train of thought: we can let the light be 1, turn off the light for-1. The C language code is as follows #include <stdio.h>
int main ()
{
int M;
int i,j,light[128],number=0;
scanf ("%d", &m);
for (i=1;i<=m;i++)///array light to be assigned a value of 1
Light[i]=1;
for (i=1;i<=m;i++)
{
for (j=1;j<=m;j++)
{
if (j%i==0)//Every time the first light starts to try, if it can be divisible by I, close (turn on) the light
{
LIGHT[J]=-1*LIGHT[J];
}
}
}
for (i=1;i<=m;i++)
{
if (light[i]<0)//If the lamp is off, count it once
Number+=1;
}
printf ("%d\n", number);
for (i=1;i<=m;i++)
{
if (light[i]<0)
{
printf ("%4d", I);
}
}
printf ("\ n");
return 0;
}

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.