11 Ocak 2013 Cuma

Pyramids


A research center in Sao Paulo recently suffered from flood and the technicians 
have to fall back to old equipment with less RAM. 
Out of necessity the tech team has decided to rewrite every program used in research to accommodate the machines. 
One of algorithms in the programs the team found was difficult to understand and was in need to be updated. 
Given the number of pyramids N, print out horizontally the N pyramids with decreasing height starting from N. 
(First pyramid has height N, second pyramid has height N-1, etc) 
Due to its age, the machine cannot run for too long in any situation. 
Devise an efficient algorithm so that the program will run with the least loops.


Input File


Format The input is a number N to indicate both the height of the first pyramid, 
and the number of pyramids in total.


Output Format


The output will display visually the pyramids with the greatest height on the left 
with gradually decreased pyramids trailing. 
There is at least one space before an ‘x’, and at the bottom of each pyramid, 
the ‘x’s are separated with only one space.

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. int main(){  
  4.     int a,i,j,k;  
  5.     scanf("%d",&a);  
  6.        for(i=0;i<a;i++){  
  7.         for(j=0;j<a;j++)  
  8.         {  
  9.             if(i>=j)  
  10.             {  
  11.                 for(k=0;k<a-i-1;k++)  
  12.                 {  
  13.                     printf("  ");  
  14.                 }      
  15.             }  
  16.             for(k=0;k<(i-j)*2+1;k++)  
  17.             {  
  18.                 printf(" x");  
  19.             }  
  20.             for(k=0;k<a-i-1;k++)  
  21.             {  
  22.                     printf("  ");  
  23.             }      
  24.         }  
  25.         printf("\n");  
  26.     }  
  27.     system("PAUSE");  
  28.     return 0;  
  29. }

Hiç yorum yok:

Yorum Gönder

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...