vendredi 11 novembre 2016

Skipping values in a sequence of positive integers

I want to print the following number sequence:

1 2 3 6 7 8 11 12 13 16 17 18...

It prints three positive integers then skips two following values and then repeats the process

A possible solution for the first 100 positive integers can be

for (int i=1; i<=100; i++)
{
   if (i==4)
   {
     i+=2;
   }
   else
   if (i==9)
   {
     i+=2;
   }
   else
   if (i==14)
   {
      i+=2;
   }   
   .
   .
   .
   cout<<i<<" ";
}

Anyway I can avoid these hefty amount of if..else statements?, or any other efficient solution would be appreciated .Thanks

Aucun commentaire:

Enregistrer un commentaire