Common errors and problems in C beginner code

  • 2020-04-02 01:54:44
  • OfStack

The problem
Turn on the light problem

There are n lights, numbered 1~n, the first person turns all the lights on, the second person presses all the switches of multiples of 2 (the lights will be turned off), the third person presses all the switches of multiples of 3 (the lights off will be turned on, the lights on will be turned off), and so on. There are k people, so which lights are on at the end? Input: n and k, output the number of the light on. K n acuities were 1000 or less


#include <stdio.h> 
#include <math.h> 
int main() 
{ 
   int a[1001],n,k,i,j; 

   printf(" Please enter the number of lights and people respectively n"); 
   scanf("%d%d",&n,&k); 

   while(1) //Check for excess
   { 
      if(k>=1 && k<=1000 && n>=k && n<=1000) 
         break; 
      else 
      { 
         printf(" The value does not match, please enter again: n"); 
         scanf("%d%d",&n,&k); 
      } 
   } 

   for(i=2;i<=k;i++) //Each operation I *j switch, a[I *j] plus 1
   { 
      for(j=1;i*j<=n;j++) 
      { 
         a[i*j]=a[i*j]+1; 
      } 
   } 

   for(i=1;i<=n;i++) //If the number of operations is even, the light at that position is on
   { 
      if(a[i]%2==0 && a[i]!=0) 
      printf("%dt",i); 
   } 

   printf("n"); 
   return 0; 
}

Testing:

"I tested myself and found it was ok.. The online system results are WrongAnswer.. No reason has been found..."
  Review:

The most obvious mistake is line 26


a[i*j]=a[i*j]+1;

Since the a array was previously defined as a local auto category, the data in a is garbage value without initialization. In other words, the data in a is meaningless. So the expression a[I *j]+1 doesn't make sense.

Also, line 22


  for(i=2;i<=k;i++) //Each operation I *j switch, a[I *j] plus 1

It is also logically wrong, lacking the "first person turns all the lights on" step.

In addition


printf(" Please enter the number of lights and people respectively n"); 
   scanf("%d%d",&n,&k); 

   while(1) //Check for excess
   { 
      if(k>=1 && k<=1000 && n>=k && n<=1000) 
         break; 
      else 
      { 
         printf(" The value does not match, please enter again: n"); 
         scanf("%d%d",&n,&k); 
      } 
   }

This paragraph is very ugly, belongs to the typical tan haoqiang style, C language should be written like this:


printf(" Please enter the number of lights and people respectively n"); 
while(scanf("%d%d",&n,&k) ,( k <1 || n<k || n>1000) ) 
{ 
   printf(" The value does not match, please enter again: n"); 
}

There are other bugs in the code that are not addressed here as they are not the main problem.

Refactoring:


#include <stdio.h> 
#define MAXNUM 1000 
#define ON 0 
#define OFF 1 
int main( void ) 
{ 
   int light[MAXNUM] = { ON } ; //Turn on all the lights
   int n , k ; 
   int i ; 

   printf( " Please enter the number of lights and people respectively n" ); 
   while ( scanf("%d%d", & n ,& k ) ,( k < 1 || n < k || n > MAXNUM ) ) 
      printf(" The value does not match, please enter again: n"); 

   for ( i = 2 - 1 ; i < k ; i ++ ) //The second person presses all switches in multiples of 2...
   { 
      int j ; 
      for ( j = i ; j < n ; j += i + 1) 
         light[j] = ! light[j];     
   } 

   for ( i = 0 ; i < n ; i ++ ) 
      if( light[i]==ON ) 
         printf( "%d " , i + 1 ); 
   putchar('n'); 

   return 0; 
}


Related articles: