20200205 hourglass Print

**

Print hourglass

**

(Ha ha ha ha Today is the first day of vacation to learn just asked the driver did not expect him to spank the code! Idol ah!)

This question asks you to write a program to print a given symbol into an hourglass shape.
The so-called "hourglass shape" refers to each row outputs odd symbols; symbol center of each row are aligned; the difference between the number of symbols two adjacent rows 2; the number of symbols in descending order of descending to the first one, then the order of small to large increments; inclusive symbol equal numbers.
Given any N symbols, not necessarily exactly composed of an hourglass. Requirements can be printed out as much of the hourglass symbol.

Input format:
Input gives a positive integer N (≤1000) and a symbol in a row, separated by a space.
Output format:
first print largest hourglass shape consisting of a given symbol, the symbol number output last remaining useless fall in a row.
Here Insert Picture DescriptionThis is a screenshot of the PTA

(See other children a title are thought arithmetic sequence I am a silly)

I think the problem: how to center the output? How output space?

The following are the main body of the function

#include<iostream>
using namespace std;

int fun(int n);
void output(int n,char x,int i);

int main()
{
 int n;
 char x;
 cin>>n>>x;
 
 int i=fun(n);//实现计算功能 返回i 即沙漏首行符号数 
~~// 调试  cout<<i;~~ 
    
 output(n,x,i);//实现输出功能 计算余数 
 
 return 0;
}

(I wanted a direct function of the package but then think of independence wrote one more)

The following function is obtained the number of symbols in the first line should be printed

int fun(int n)
{
 int a=(n-1)/2;
 int i=1;
 if(a<3&&a>0) ;
 else if(a<8&&a>2) i=3;
 else
 {
     while(a>i)
     {
        i+=2;
        a-=i;
     }
 }
 ~~//调试  cout<<i<<endl;~~  
 return i; 
 }

I started while the condition (a> 3) is obviously wrong
(ha ha ha ha ha ha but it had a magical three test points later changed not a case or not fully considered and then I fill the pot ha ha ha)

Here is the output function I was separated on a triangle two and a half years of declining output and a lower triangular increment remainder I took a sum used to count how many minus sign out

void output(int n,char x,int i)
{
  int m=i;
  int sum=0;//沙漏中符号个数 算余数 
  while(m>0)//输出递减 有1 
  {
     if(m<i)//判断是否首末行 否则输出空格使得沙漏居中 
     {
        for(int p=(i-m)/2;p>0;p--)//注意是居中 不是靠右 p要除以2的
        {
           cout<<" ";
        }
     }

    for(int p=m;p>0;p--)//输出符号 
    {
        cout<<x;
    }
   
   cout<<endl;
   sum+=m; 
   m-=2}
   
   m=3; 
 
   while(m<=i)//输出递增 
   {
      if(m<i)//判断是否首末行 否则输出空格使得沙漏居中 
      {
         for(int p=(i-m)/2;p>0;p--)//注意是居中  
         {
            cout<<" ";
         }
      } 
  
     for(int p=m;p>0;p--)//输出符号 
     {
         cout<<x;
     }
  
  cout<<endl;
  sum+=m;
  m+=2;
  }

cout<<n-sum;
 
} 

We must pay attention to the conditions of circulation judge ah!

Then I compared it to a reference code first big difference is that my code run time and more time-consuming

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
  int x, i, j, n, blank, count;
  char ch;
  
  cin >> n >> ch;
  
  for(count=1, x=1; count+(x+2)*2<=n; count+=(x+2)*2, x+=2);
  //计算可以输出的符号总数(count),以及第一行的符号个数(x) 
  

When the first count in the calculation using (x + 2) instead of x + 2 and finally
the number sign him is to first count need to use to figure out the x down from 3 +2 has been
his good judgment conditional loop ( ha ha ha ha me what the hell)

Then his output

//上半区,含中心点 
  for(i=x, blank=0; i>=1; blank++, i-=2)
  {//i:当前行的符号个数,blank:当前行的行首空格个数 
    for(j=0; j<blank; j++)
      cout << " "; //输出当前行的行首空格 
    for(j=0; j<i; j++)
      cout << ch;    
    cout << endl;//行末空格无需输出 
  }
  
  //下半区,由3个符号开始输出 
  for(i=3, blank-=2; i<=x; blank--, i+=2)
  {
    for(j=0; j<blank; j++)
      cout << " ";
    for(j=0; j<i; j++)
      cout << ch;
    cout << endl;
  }
  
  cout << n-count << endl; //输出剩余空格 
  
  return 0;
}

Output feeling with me but he almost simple ah well written
(there is the original that is called a central point ah ha ha ha)

(Well, good day so later I want to watch TV)
(code not like to play kids today prefer to play in the code yet)

Released two original articles · won praise 0 · Views 78

Guess you like

Origin blog.csdn.net/wasabidragon/article/details/104186550