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.
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- int a,i,j,k;
- scanf("%d",&a);
- for(i=0;i<a;i++){
- for(j=0;j<a;j++)
- {
- if(i>=j)
- {
- for(k=0;k<a-i-1;k++)
- {
- printf(" ");
- }
- }
- for(k=0;k<(i-j)*2+1;k++)
- {
- printf(" x");
- }
- for(k=0;k<a-i-1;k++)
- {
- printf(" ");
- }
- }
- printf("\n");
- }
- system("PAUSE");
- return 0;
- }
Hiç yorum yok:
Yorum Gönder