Buchstabenpyramide

CDW

0
Mitarbeiter
eingereicht von Ook!
Es soll eine Buchstabenpyramide erstellt werden.
Dem Benutzer soll es möglich sein, einen Buchstaben einzugeben, auf Basis dieses Buchstabens soll dann die Pyramide gebaut werden.

Beispiel:
Benutzer Eingabe >> "H"
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA

Zusatzaufgabe: gib die Pyramide zentriert aus
Code:
  A
 ABA
ABCBA
 
Code:
$ echo h|perl -e 'sub Z{$_=join"",@_;$_.=reverse;y/a-z//s;return$_};$b=getc;for(a..$b){print" "x(ord($b)-ord),Z(a..$_),"\n"}'
       a
      aba
     abcba
    abcdcba
   abcdedcba
  abcdefedcba
 abcdefgfedcba
abcdefghgfedcba
chop($b = <>) in ein einfaches $b = getc umgeändert..
 
Code:
import java.lang.*;
public class AlphPyr {
  public static void main(String[] args) {
    String alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int j,i,k;
    try{
	 for(i=0;i<alph.length();i++) {
	   for(k=alph.indexOf(args[0])-i;k>0;k--) System.out.print(" ");
	   for(j=0;j<=i;j++) System.out.print(alph.charAt(j));
	   for(j=i-1;j>=0;j--) System.out.print(alph.charAt(j));
	   System.out.println();
	   if(new Character(alph.charAt(i)).compareTo(args[0].toCharArray()[0]) == 0) break;
	 }
    }
    catch (ArrayIndexOutOfBoundsException e) {
	 System.out.println("[+] Bitte uebergeben sie dem Programm eine Basis. Beispielaufruf: java AlphPyr \"H\"");
    }
  }
}


Code:
fetzer:~> java AlphPyr "H"
       A
      ABA
     ABCBA
    ABCDCBA
   ABCDEDCBA
  ABCDEFEDCBA
 ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
 
Hallo!

Meine Java Lösung

Code:
public class PyraChars {

	private int maxChars;
	private char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
			   				 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	
	/**
	 * @param value
	 */
	public PyraChars(int value) {
		System.out.println();
		if(value > 90)
			value -= 32;
		value -= 64;
		maxChars = value;
		
		createPyra(1);  	
	}
	
	/**
	 * @param i Grenze
	 */
	private void createPyra(int i) {		
		int charsRow = ((i * 2) - 1); 
		String s = "";
		
		for(int z=0; z<(maxChars-i); z++)
			s += " ";

		for(int z=0; z<charsRow; z++) {				
			if(z < (charsRow / 2))
				s += chars[z];
			else if(z == (charsRow / 2))
				s += chars[(charsRow / 2)];
			else
				s += chars[(charsRow - z - 1)];
		}
		
		System.out.println(s);
		if(i < maxChars)
			createPyra(i+1);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("   Buchstaben Pyramide\n-------------------------"); 
		System.out.print("Buchstabe eingeben: "); 
		new PyraChars(readValue());
	}

	/**
	 * @return value
	 */
	public static int readValue() {
		try { return new BufferedReader(new InputStreamReader(System.in)).read(); }
		catch(IOException ex) { return 0; }
	}
	
}

Gruß
Felix
 
Ich hab's mal als Ansporn genommen, um Brainfuck zu lernen :D
Code:
++++++++++>++++++++[>++++++++<-]>>,>+++++[<------------->-]<+[>+[>+>+>+<<<-]>>>[<<<+>>>-]<<<>[<<<+.>>>-]>-[<<<<-.>>>>-]<<<<-<<.>>>-]
Das geht bestimmt kürzer und die Pyramide wird nicht zentriert, aber: es funktioniert! ;)
 
ist zwar schon was her aber die anderen aufgaben die ich gemacht hab sind noch älter ;)
hier meine lösung in python

Code:
l=[]
m=[65]
n=[]
i=0
out=""

a= raw_input("Bitte einen Buchstabe von a-z eingeben: ")
a=a.upper()
if len(a)== 1 and ord(a) in range(65,91):
    b=ord(a)-65
    if a == "A":
        print "A"
    else:
        print " "*b+"A"
        while a not in out:
            m += [m[i]+1]
            n = m[:]
            n.reverse()
            del n[0]
            l=m[:]
            l.extend(n)
            j = 0
            i += 1
            out=" "*(b-i)
            while j < len(l):
                out +=chr(l[j])
                j += 1     
            print out
else:
    print "Ich hab gesagt EINEN BUCHSTABE, du Lulli!"

hab vor nicht all zu langer zeit angefangen und bin für tips und verbesserungsvorschläge immer dankbar :)
 
Auch mal in brainfuck: (wirklich gute Aufgabe zum brainfuck-Lernen!)
Code:
,>>++++++++[-<++++++++>>>+<<]>>++<<<+[-<->>>+<<]<[>[-<+>>+<]>[-<<->+>]<[->+>.+<<]>>.<[-<+>>-.<]>>.<<+<<]

Edit:
Oder zentriert:
Code:
,>>++++++++[-<++++++++>>>+>++++<<<]>>++<<<+[-<->>>+<<]>>>>.<<<<<[[->>>>>>+<.<<<<<]>>>>>>[-<<<<<<+>>>>>>]<<<<<[-<+>>+<]>[-<<->+>]<[->+>.+<<]>>.<[-<+>>-.<]>>.<<+<<]
 
das ganze in C# :

Code:
using System;
class BuchstabenPyramide {
    public static void Main(string[] args) {
        int Spitze = (int)(Convert.ToChar(args[0]));
        int Stufen = Spitze - 65; 
        int w,x,y,z;
        for(x=0; x<=Stufen; x++) {
            for(w=0; w<Stufen-x; w++) { // Leerzeichen
                Console.Write(" ");
            }   
            for(y=0; y<=x; y++) { // Vorwärts
                Console.Write("{0}", Convert.ToChar(y+65));
            }   
            for(z=y-2; z>=0; z--) { // ... und Rückwärts
                Console.Write("{0}", Convert.ToChar(z+65));
            }   
            Console.WriteLine("");
        }   
    }   
}

Ausgabe:
Code:
18:12 xeno@gideon:~/source/csharp/buchstabenpyramide mono bp.exe H
       A
      ABA
     ABCBA
    ABCDCBA
   ABCDEDCBA
  ABCDEFEDCBA
 ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
 
Ich konnts mir nicht verkneifen, das mal schnell zusammenzuhacken. Den Array-Inhalt hab ich natürlich nicht von Hand geschrieben, sondern mir generieren lassen - ich hab also die Aufgabe tatsächlich gelöst ;)

Code:
public class Pyr {

private static char[][] chars = new char[][] {
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' '},
{' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' '},
{' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' '},
{' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' '},
{' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' '},
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'}
};

        public static void main(String[] args){
                for (int i = 0; i < (int)args[0].charAt(0)-64; i++){
                        for (int j = 0; j < 51; j++){
                                System.out.print(chars[i][j]);
                        }
                System.out.println();
                }
	}
}
 
Hier mal etwas exotisch in AHK:
Funktion, die eine Variable mit der Buchstabenpyramide zurückgibt. (Eingemittet)

Code:
pyramid_alpha($char){
	Loop, % len := asc($char) - 64
	{
		Loop, % len - strlen(prev_last)
			_space .= a_space 
		c := chr(65 + A_index - 1)
		line .= _space . prev_last . c . suff_last "`n"
		prev_last	.=  c, suff_last :=  c . suff_last, _space := ""
	}
return, line	
}
:)
 
Ich hab das mal in C++ geschrieben:
Code:
/* A
ABA
ABCBA
ABCDCBA
*/
#include <iostream>
using namespace std;

char CinBuch[100];
char kurz[2];
char BSalat[100];
int Array = 0;
bool Buch = false;

int main(int argc, char *argv[]) {
BSalat[Array] = 'A';
cout << "Grossbuchstabe eingeben" << endl;
cin >> CinBuch;
int Oft;
Oft = (int) CinBuch[0] - (int) 'A';

int line = 0;
while (true) {//bis CinBuch[0]
kurz[0] = 'A';
kurz[0] = (int) kurz[0] + line;
int j = 0;
j = j + line;

if (kurz[0] == CinBuch[0]) {
Buch = true;
}

for (; j < Oft; j++) {
cout << " ";
}
for (; (int) BSalat[Array] < (int) kurz[0];) {
Array++;
BSalat[Array] = BSalat[Array - 1] + 1;
}
for (; BSalat[Array] != 'A';) {
Array++;
BSalat[Array] = BSalat[Array - 1] - 1;
}
cout << BSalat << endl;
Array = 0;
line++;
if (Buch) {
system("PAUSE");
return EXIT_SUCCESS;
}
}
}

Wenn euch irgendetwas auffällt, was man unbedingt ändern sollte, zögert nicht mir eine PN zu schicken. :)
 
Meins in Ruby, relativ kurz:

Code:
argv = ARGV[0]; i = 1; a = ("A".."Z").to_a; j = 0
a.each { |part|
	if part == argv
		j = i+1
	end
	i += 1
}
i = 1; k = 0; l = j-1
while i < j
	print (" "*l)
	i.times do
		print "#{a[j-(j-k)]}"; k += 1
	end
	(i-1).times do
		print "#{a[j-(j-(k-2))]}"; k -= 1
	end
	puts; i+=1; k = 0; l -= 1
end

Ausgabe:

Code:
athelstan@127.0.0.1:~$ ruby Buchstabenpyramide.rb O
               A
              ABA
             ABCBA
            ABCDCBA
           ABCDEDCBA
          ABCDEFEDCBA
         ABCDEFGFEDCBA
        ABCDEFGHGFEDCBA
       ABCDEFGHIHGFEDCBA
      ABCDEFGHIJIHGFEDCBA
     ABCDEFGHIJKJIHGFEDCBA
    ABCDEFGHIJKLKJIHGFEDCBA
   ABCDEFGHIJKLMLKJIHGFEDCBA
  ABCDEFGHIJKLMNMLKJIHGFEDCBA
 ABCDEFGHIJKLMNONMLKJIHGFEDCBA
 
eine nette Python Lösung

Code:
from itertools import takewhile

def row(c, limit):
  rowSpace = (alph.find(limit) - alph.find(c) - 1) * " "
  row = "".join(takewhile(lambda x: x != c, alph))
  return rowSpace + row + row[::-1][1:] + "\n"

def pyra(limit):
  limit = chr(ord(limit)+2)
  return "".join(map(lambda c: row(c, limit), takewhile(lambda x: x != limit, alph)))[:-1]

alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print pyra('F')

... und eine Haskell Lösung

Code:
import Data.Char
import Data.List

doIt :: Char -> [String]
doIt limit = map row $ takeWhile (/= limit) $ alph
  where
    alph = ['A'..'Z']
    row c = (rowSpace c 0) ++ (rowAlph c) ++ (drop 1 $ reverse $ rowAlph c)
    rowAlph c = takeWhile (/= c) $ alph
    rowSpace c i = if i == rowSpaceCnt c then "" else ' ' : rowSpace c (i+1)
    rowSpaceCnt c = ((index (limit, 0)) - (index (c, 0)) - 1)
    index (c, i) = if i > 25 then 1 else if alph !! i == c then i else index (c, i+1)

pyra :: Char -> String
pyra limit = concat . intersperse "\n" $ doIt (chr $ ord limit + 2)

main :: IO()
main = putStrLn $ pyra 'F'

Gruß
Felix
 
ne Delphi Version ;)

Code:
// Delphi
type
  TA_Z = 'A'..'Z';

function CharPyramid( const Input: TA_Z ): String;
var
  i, j,
  k, l,
  m: Integer;
begin
  j := 0;
  k := ORD(Input)-64;
  l := -1;
  // Summe der Folge Ai = i*2+1 berechnen
  for i := 1 to k do
    inc( j, i*2+1 );
  // Result = Länge - Letzte LFCR (#13#10)
  SetLength( Result, j-2 );
  for j := 1 to k do
  begin
    m := (j-1)*2;
    inc( l, m+1 );
    inc( m, 2 );
    for i := 1 to j do
    begin
      Result[l+i]   := chr(64+i);
      Result[l+m-i] := chr(64+i)
    end;
    if j<k then
    begin
      Result[l+m]   := #13;
      Result[l+m+1] := #10;
    end;
  end;
end;

function Center( Input: String ): String;
var
  i, j,
  k, l: Integer;
begin
  j := 1;
  for i := 1 to Length(Input) do
    if input[i] = #13 then
      inc(j);
  for i := 1 to j do
  begin
    l := 0;
    if i>1 then
      for k := 1 to Length(Input) do
      begin
        if Input[k] = #13 then
        begin
          inc( l );
          if l = i-1 then
          begin
            l := k+2;
            break;
          end;
        end;
      end;
    Insert( StringOfChar( ' ', j-i ), Input, l );
  end;
  Result := Input;
end;

// Möglicher Aufruf:
// (~Font = Courier New)
Center( CharPyramid( 'Z' ) );

Code:
                         A
                        ABA
                       ABCBA
                      ABCDCBA
                     ABCDEDCBA
                    ABCDEFEDCBA
                   ABCDEFGFEDCBA
                  ABCDEFGHGFEDCBA
                 ABCDEFGHIHGFEDCBA
                ABCDEFGHIJIHGFEDCBA
               ABCDEFGHIJKJIHGFEDCBA
              ABCDEFGHIJKLKJIHGFEDCBA
             ABCDEFGHIJKLMLKJIHGFEDCBA
            ABCDEFGHIJKLMNMLKJIHGFEDCBA
           ABCDEFGHIJKLMNONMLKJIHGFEDCBA
          ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
         ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
        ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
       ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
      ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
     ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
    ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA
   ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA
  ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA
 ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA
 
Hier mal vorab unformatiert in Python 3. Ich glaube, dass ginge auch kürzer ;)


Code:
alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def turn(str):
    try:
        i = 1
        len_ = len(str)
        while i < len_:
            str = str + str[(len_ - i -1):(len_ - i)]
            i = i +1
        return str
    except:
        print("Leider trat ein Fehler auf!")

def pyramide():
    try:
        zentrum = input("Mitte der untersten Reihe: ")
        i = 0
        while i <= alph.index(zentrum):
            print(turn(alph[:(i+1)]))
            i = i+1
    except:
        print("Leider trat ein Fehler auf!")

pyramide()
 
So ich habs mal in C geschrieben:

Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
  int i, j;
  char inp=0;
  while(inp<1 || inp>26)
  {
    printf("Bitte einen Grossbuchstaben eingeben: ");
    inp = getchar();
    inp -=64;
    while(getchar()!='\n');
  }

  for(i=1; i<=inp; i++)
  {
    for(j=i; j<inp; j++) putchar(' ');
    for(j=1; j<=i; j++) putchar(64+j);
    for(j-=2; j>0; j--) putchar(64+j);
    putchar('\n');
  }
  return 0;
}
Grüsse mYstar
 
Hier hab ich auch mal eine Möglichkeit in C++:

Code:
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {

if(argc < 2) {
	cout << "Usage: a.out <Buchstabe>" << endl;
	return -1;
}
char *fin = argv[1];
int max = (int)*fin - 65;

char alph[] = {'A','B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U','V', 'W', 'X', 'Y', 'Z'};
int k=0,i=0,j=0,p=0,q=max;

while(k <= max) {
	for(p=q; p>=0; p--) cout << ' ';
	for(i=0; i<=k; i++) cout << alph[i];
	for(j=(k-1); j>=0; j--) cout << alph[j];
	cout << endl;
	k++;
	q--;
}
return  0;
}

./a.out G
Code:
       A
      ABA
     ABCBA
    ABCDCBA
   ABCDEDCBA
  ABCDEFEDCBA
 ABCDEFGFEDCBA
 
Python, eine Zeile:
Code:
for c in range(ord("A"), ord(raw_input("Geben Sie einen Grossbuchstaben ein: ").capitalize()) + 1): print ''.join([" " for d in range(ord("A"), 168 - c)])+''.join([chr(d) for d in range(ord("A"), c)])+''.join([chr(d) for d in range(c, ord("A") - 1, -1)])
Ich hab Spaß an den Aufgaben hier :)

Die Pyramide ist natürlich zentriert.

So, Update: Pyramidenprogramm fällt nicht mehr um, wenn man einen Kleinbuchstaben eingibt.
Das ist nur ein Vorwand, einen blöden Kommentar zu dem Post unter meinem abzugeben, so etwas wie:
DAS nennst du kurz!? :p Und dann gibt es nichtmal Benutzereingabe, tsts...

mfg,
Cemper
 
Java, kurz und prägnant :p

Code:
public class Main {
	public static void main(String[] args) {
		final char start = 'A', mid = 'C';

		for (int row = start; row <= mid; ++row) {
			for (int format = 0; format < (mid - row); ++format) System.out.print(" ");
			for (char left = start; left <= row; ++left) System.out.print(left);
			for (char right = (char) (row - 1); right >= start; --right) System.out.print(right);
			System.out.println();
		}
	}
}
 
hab auchma schnell eine sicherlich nicht ganz so elegante aber funktionierende loesung in perl geschrieben....
Code:
use strict;

my $c = getc(STDIN);
my $spn=ord(uc($c)) -65;

for (0..$spn){
my @a;
for (0 .. $_) {
push(@a, chr(65+$_));
$_++;
}
print " "x$spn--;
print @a;
pop @a;
print reverse @a;
print "\n";
}
 
Zurück
Oben