00-Self-Test 1. Print the hourglass (20), 00-hourglass

Source: Internet
Author: User

00-Self-Test 1. Print the hourglass (20), 00-hourglass

This question requires you to write a program to print the given symbol into the shape of an hourglass. For example, if 17 "*" characters are specified, print them in the following format:

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

The so-called "hourglass shape" refers to the odd number of characters output for each row; the center of each line is aligned; the number difference between two adjacent lines is 2; the number of symbols first decreases from large to small to 1, the number of first and last symbols is equal.

Given any N symbols, an hourglass may not be formed. The printed hourglass is required to use as many symbols as possible.

Input Format:

Enter a positive integer N (<= 1000) and a symbol in a row, separated by spaces.

Output Format:

Print the largest hourglass shape composed of given symbols, and output the remaining unused symbols in a row.

Input example:
19 *
Output example:
***** ***  * ********2
#include<iostream>#include<cmath>using namespace std;int main(){int N;char C;cin>>N>>C;int k = sqrt((N+1)/2);for(int i=1;i<=k;i++){for(int j =1;j<i;j++){cout<<' ';}        for (int j = 2*(k - i) + 1; j >= 1; j--)  {cout<<C;}cout<<endl;}for(int i=1;i<=k-1;i++){for(int j=1;j<=k-i-1;j++){cout<<' ';}for(int j=1;j<=2*i+1;j++){cout<<C;}cout<<endl;}cout<<N-2*k*k+1<<endl; return 0;}


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.