Autor Beitrag
LuGo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Mi 07.07.10 16:43 
Hallo erstmal,

wir haben heute in Mathe so ein Problem besprochen, das mit dem Phalindrom, das wenn man zwei dreistellige Zahlen addiert (wo bei die 2te die umgedrehte erste ist (z. B. 125 + 521)), man solange weiterrechnen sollte (also dann 646 + 646) usw. bis es ein sog. Phalindrom (oder so ähnlich gibt (Bsp. 1234321). Ich dachte mir, vielliecht könnte man gut ein Programm dazu schreiben. Wie kann man das Ergebnis vergleichen und erkennen , das es ein Phalindrom ist?

Danke

LuGo

Moderiert von user profile iconNarses: Titel geändert, war: "Werte auslesen und vergleichen (Phalindrome)".
delphi10
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 447
Erhaltene Danke: 2

W2K, XP, Vista64, Win7 64
RAD-Studio 2010
BeitragVerfasst: Mi 07.07.10 17:21 
Guckst du:
de.wikipedia.org/wik...nung_von_Palindromen
Googlen nach Palindrom und Delphi und du kannst dir die Lösungen aussuchen.

_________________
Salus populi suprema lex esto
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 07.07.10 17:40 
D2009++:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function IsP(X: Integer): Boolean;
    function NP(X:Integer): Integer; InlineBegin Result := Power(10, Trunc(Log(X))); end;
begin
    If X < 10 Then Exit(-1 < X);
    Result := ((X mod 10) = (X div NP(X))) and IsP((X - (X div NP(X)) * NP(X)) div 10);
end;


k, als Einzeiler (D7+):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function IsP(X: Integer): Boolean;
    function NP(X:Integer): Integer; Begin Result := Power(10, Trunc(Log(X))); end;
begin
    Result := IIf(X < 10, -1 < X, ((X mod 10) = (X div NP(X))) and IsP((X - (X div NP(X)) * NP(X)) div 10));
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Fiete
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 618
Erhaltene Danke: 368

W7
Delphi 6 pro
BeitragVerfasst: Fr 09.07.10 21:14 
Moin LuGo,
am einfachsten neue Zahl umdrehen und mit der alten vergleichen.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TSpiegelzahl.Umdrehen(Alt:String;var Neu:String);
  var K,LA:Integer;
  begin
   Neu:='';LA:=Length(Alt);
   for K:=LA downto 1 do Neu:=Neu+Alt[K];
  end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TSpiegelzahl.StartClick(Sender: TObject);
  var Zahl,NeueZahl:String;
  begin
   Zahl:=EditZ.Text;Ausgabe.Clear;
   Ausgabe.Lines.Add(Zahl);Umdrehen(Zahl,NeueZahl);
   Abbruch:=False;
   Screen.Cursor:=crHourGlass;
   repeat
    Ausgabe.Lines.Add(NeueZahl);
    LangAdd(Zahl,NeueZahl,Zahl); // Zahl:=Zahl+NeueZahl - numerisch
    Ausgabe.Lines.Add(Zahl+' - '+IntToStr(Length(Zahl)));
    Umdrehen(Zahl,NeueZahl);
    Application.ProcessMessages;
   until (Zahl=NeueZahl) or Abbruch;
   Screen.Cursor:=crDefault;
  end;


Die procedure TSpiegelzahl.LangAdd(A,B:String;var C:String); mußt du schon selbst entwickeln :wink:
Die Zahl 196 ist für dies Problem :evil:
Gruß
Fiete

_________________
Fietes Gesetz: use your brain (THINK)