Skip to content

Instantly share code, notes, and snippets.

@jeremy5189
Created October 10, 2013 16:24
Show Gist options
  • Save jeremy5189/6921251 to your computer and use it in GitHub Desktop.
Save jeremy5189/6921251 to your computer and use it in GitHub Desktop.
/*
* Author: Jeremy Yen Date: 2013.10.10
* http://oj.sslab.cs.nthu.edu.tw/problems/11
*
*/
#include <iostream>
#include <sstream>
using namespace std;
int input_n;
string genStar( int n )
{
stringstream ss;
for( int i = 0; i < n; i++ )
ss << '*';
return ss.str();
}
string genSpace( int n )
{
stringstream ss;
for( int i = 0; i < n; i++ )
ss << ' ';
return ss.str();
}
string genTriangle( int layer )
{
stringstream ss;
for( int i = 1; i <= layer; i++ )
{
int star_count = 2 * i - 1;
int space_count = ((2 * (input_n + 2) - 1) / 2);
space_count -= (int)(star_count / 2 );
ss << genSpace(space_count)
<< genStar(star_count)
<< genSpace(space_count)
<< endl;
}
return ss.str();
}
int main()
{
cin >> input_n;
for( int i = 3; i <= ( input_n + 2 ); i++ )
{
cout << genTriangle(i);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment