Re: I need to create an inverted pyramid, please help me
Zeppe wrote:
rh75691@gmail.com wrote [18/02/09 16:48]:
Hey guys,
I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:
******
****
**
*
// pyramid
#include <iostream>
int main() {
unsigned n = 60;
for(unsigned i = 0; i < n*(n+1)/2; ++i)
std::cout << (i%(n+1) == n ? '\n' : i%(n+1)-i/(n+1) < n-1-2*i/(n+1)
? '*' : ' ');
}
#include <algorithm>
template<int Width, char Fill ='*', char Space =' ', int Indent =0>
struct pyramid
{
template<class Output_iterator>
void operator()(Output_iterator out) {
using std::fill_n;
fill_n(out, Indent, Space);
fill_n(out, Width, Fill);
*out = '\n';
pyramid<Width - 2, Fill, Space, Indent + 1>( )(++out);
}
};
template<char Fill, char Space, int Indent>
struct pyramid<0, Fill, Space, Indent>
{
template<class Output_iterator>
void operator()(Output_iterator) { }
};
template<char Fill, char Space, int Indent>
struct pyramid<-1, Fill, Space, Indent>: pyramid<0, Fill, Space, Indent>
{ };
#include <iostream>
#include <iterator>
int main() {
pyramid<61>( )(std::ostream_iterator<char>( std::cout ));
}
// nuke!!
#include <iostream>
int main() {
unsigned n = 60;
for(unsigned i = 0; i < n*(n+1); ++i)
std::cout << (i%(n+1) == n ? '\n' : i%(n+1)-i/(n+1) < n-1-2*i/(n+1)
? '*' : ' ');
}
That's amusing. :)