0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

砂時計形星タワーを印字 Print Stars Tower In Hourglass

Last updated at Posted at 2019-06-03

Question

  • 下記の砂時計形★タワーを印字する(△はスペース)

  • Example:

input_5
        ★★★★★
        △★★★△     
        △△★△△
        △★★★△
        ★★★★★
input_3
        ★★★     
        △★△
        ★★★
input_9
        ★★★★★★★★★    
        △★★★★★★★△
        △△★★★★★△△
        △△△★★★△△△    
        △△△△★△△△△
        △△△★★★△△△
        △△★★★★★△△
        △★★★★★★★△
        ★★★★★★★★★

Solution

solution.cpp
#include <iostream>
#define SKIP 2
using namespace std;

void printStar( int stars ){
    
    for( int i = 0; i < stars; i++ )
        cout << "*";
}

void printSpace( int spaces ){
    
    for( int i = 0; i < spaces; i++ )
        cout << " ";
}

int main(void){

    int times;
    cout << "Input an ODD number to print star hourglass:\n";
    cin >> times;

    if( 0 == times % 2 ){
        cout << "Please input an ODD number!" << endl;
        return 0;
    }

    int stars, spaces, revPos;
    
    // reverse point is the most stars of lines
    revPos = times / 2;
    
    for( int i = 0; i < times; i++ ){

        // count stars and spaces in this line
        if( i <= revPos ){
            stars = times - i * SKIP;
            spaces = times - stars;
        }else{
            stars = times - ( ( times - ( i + 1 ) ) * SKIP );
            spaces = times - stars;
        }
        
        // stars always start from the center, insert spaces in the front and tail of stars
        printSpace(spaces/2);
        printStar(stars);
        printSpace(spaces/2); //this line can be commented out
        
        // change line
        cout << endl;
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?