SPOJ Problem - CPTTRN5 - Character Patterns (Act 5)

This is a problem from SPOJ. We have to print a character pattern according to the input given.

We have input 

33 1 24 4 12 5 2  

Here 3 is the number of test cases.
The output is

*****\.* *.\* **** *./* */.* **** *\.* *.\* ****  ********* *\*/*\*/* ********* */*\*/*\* ********* *\*/*\*/* ********* */*\*/*\* *********  **************** *\.*./*\.*./*\.* *.\*/.*.\*/.*.\* **************** *./*\.*./*\.*./* */.*.\*/.*.\*/.* ****************  
Here is the program to print above pattern in C++.

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {

    int l, c, s;
    scanf("%d %d %d", &l, &c, &s);
    const int breadth = (c * (s + 1)) + 1;
    const int height = (l * (s + 1)) + 1;
    for(int i = 0; i < height; i++)
    {
      for(int j = 0; j < breadth; j++)
      {
        if(i % (s+1) == 0 || j % (s+1) == 0)
        {
          printf("*");
        }
        else if((i/(s+1) + j/(s+1)) % 2 == 0)
        {
          if(i % (s+1) == j % (s+1))
          {
            printf("\\");
          }
          else
          {
             printf(".");
          }
        }
        else
        {
          if(i % (s+1) == (s+1) - (j % (s+1)))
          {
            printf("/");
          }
          else
          {
            printf(".");
          }
        }
      }
      printf("\n");
    }
    printf("\n");
   }
}
 
Here we have used scanf() and printf() because they are faster then cout and cin.

You can download this code from here.
You can run this program from here. (Don't forget to enter values)


                                                                  
                                                                    
You may also like: 
SPOJ Problem - CPTTRN6 - Character Patterns (Act 6)
C++: Maximum Priority Queue 
C++: Program to find smallest element is an array which is repeated exactly 'k' times 
C++: Swapping Node Links in Linked List 
C++: Sorting Elements according to Frequency 

Comments