The print hourglass for PAT exercises

Source: Internet
Author: User

Title Original title DescriptionL1-2. Print Hourglass time limit ms
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen

The subject asks you to write a program to print the given symbol into an hourglass shape. For example, given 17 "*", require printing in the following format

***** ***  * ********

The so-called "hourglass shape" refers to the output of an odd number of symbols per line, the line symbol center alignment, the adjacent two lines sign number difference 2, the number of symbols from large to small order descending to 1, and then from small to large order increment;

Given any n symbols, it is not always possible to form an hourglass exactly. Require that the printed hourglass be able to use as many symbols as possible.

Input format:

The input gives 1 positive integers n (<=1000) and a symbol in one line, separated by a space in the middle.

Output format:

The largest hourglass shape consisting of a given symbol is printed first, and the number of symbols left unused in a row is finally output.

Input Sample:
19 *
Sample output:
* ********2
Analysis: Can be combined with the sum formula of the light differential column and the arithmetic progression formula
1, 3, 5, 7 .... The general formula of this series is 2*n-1, and the sum formula is the square of N.
The difficulty is to print out the hourglass shape
Answer code:
#include <iostream>
#include <cmath>
using namespace Std;

int main () {
int N,hang;
Char ch;
scanf ("%d%c", &n,&ch);
Hang =sqrt ((n+1)/2); Here, hang represents the number of rows in the upper part of the hourglass.
for (int x=0;xfor (int t=0;t<x;t++) {//print the space on the upper part of the Hourglass
cout<< "";
}
for (int t=0;t<2* (hang-x) -1;t++) {//print the symbol on the top half of the hourglass
cout<<ch;
}
cout<<endl;
}
for (int x=hang-1;x>0;x--) {
for (int t=0;t<x-1;t++) {//Print space in the lower half of the hourglass
cout<< "";
}
for (int t=0;t<2* (hang-x) +1;t++) {//print the symbol for the lower half of the hourglass
cout<<ch;
}
cout<<endl;
}
cout<<n-(2*hang*hang-1); The remaining symbols
return 0;
}

The print hourglass for PAT exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.