C language hundred people pull lights problem

Source: Internet
Author: User

Topic:

There are 100 people, numbered from 1 to 100; Another 100 lamps, numbered from 1 to 100. Everyone is required to pull all the lights divisible by their own number, such as the number of 1 should be all the lights are pulled once, numbered 2 should be all numbered even lights are pulled again, and the number 100 can only pull the lamp numbered 100. Assuming all the lights are off at first, how many lights are there in the end? What are the numbers, respectively?

Requirements:

Define two functions:
void switch_light (int light[], int man);
int lights_on (int *light, int *on);


An array light is defined in main (), and the values of each element represent
The light off state of each lamp (e.g. 1 indicates bright, 0 is off), and then
Define another array on, the value of each element indicates the last light
The number of the lamp.


The role of the Switch_light () is to try to pull the man number
A lamp that is divisible by his number, which affects the elements of the array light
The value;


LIGHTS_ON () Statistics A total of a few lights are bright, and put on the light
The number of the lamp is stored to on, and the return value is the total number of lights lit.

Program:

1#include <stdio.h>2 #defineLight 1003 intMainvoid)4 {5     intI, J, A[light +1] = {0}, Count =0;6      for(i =1; I <= light; i++)7      for(j = i; J <= Light; J + =i)8A[J] ^=1;9printf": Bright 0: Dark \ n");Ten      for(i =1; I <= light; i++) One     { A         if(A[i]) -         { -printf"●"); thecount++; -         } -         Elseprintf"0"); -     } +printf"\ n Total%d lights on", count); -     return 0; +}

1#include <stdio.h>2 intMain ()3 {4     voidSwitch_light (intLight[],intMan ); 5     intLIGHTS_ON (int*light,int*On );6     inti,man,light[101]={0},on[ -],sum;7      for(man=1; man<= -; man++)8 switch_light (Light,man);9sum=lights_on (light,on);Tenprintf"Last there is%d lights on!\n", sum);  Oneprintf"These lights is:"); A      for(i=0; i<sum;i++) printf ("%d", On[i]); - } - voidSwitch_light (intLight[],intMans) the { -     inti; -      for(i=man;i<= -; i++) -     if(i%man==0) +     { -         if(light[i]==0) +light[i]=1;  A         Elselight[i]=0; at     } - } -  - intLIGHTS_ON (int*light,int*On ) - { -     inti,j=0; in      for(i=1; i<= -; i++) -     if(light[i]==1) to     { +On[j]=i; J + +; -     } the     return(j); *}

C language hundred people pull lights 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.