掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
教えてください (ID:16678)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
私も書いてみました。 もちろんDelphiですが。 procedure TForm1.TestBtnClick(Sender: TObject); var i: integer; sl: TStringList; begin sl := TStringList.Create; try for i := 1 to 9999 do begin if (i mod 1111) = 0 then Continue; Proc(sl, i); end; sl.SaveToFile(ChangeFileExt(Application.ExeName, '.txt')); finally sl.Free; end; end; function TForm1.MaxNum(const Num: Integer): integer; var c: array[0..3] of integer; i, j, tmp: integer; begin c[0] := Num div 1000; c[1] := (Num div 100) mod 10; c[2] := (Num div 10) mod 10; c[3] := Num mod 10; for i := 0 to 2 do for j := i + 1 to 3 do begin if c[i] < c[j] then begin tmp := c[i]; c[i] := c[j]; c[j] := tmp; end; end; Result := (c[0] * 1000) + (c[1] * 100) + (c[2] * 10) + c[3]; end; function TForm1.MinNum(const Num: Integer): integer; var c: array[0..3] of integer; i, j, tmp: integer; begin c[0] := Num div 1000; c[1] := (Num div 100) mod 10; c[2] := (Num div 10) mod 10; c[3] := Num mod 10; for i := 0 to 2 do for j := i + 1 to 3 do begin if c[i] > c[j] then begin tmp := c[i]; c[i] := c[j]; c[j] := tmp; end; end; Result := (c[0] * 1000) + (c[1] * 100) + (c[2] * 10) + c[3]; end; procedure TForm1.Proc(sl: TStringList; const Num: Integer); var iMax, iMin, iAns: Integer; strAns : String; begin strAns := ''; iMax := MaxNum(Num); iMin := MinNum(Num); iAns := iMax - iMin; strAns := IntToStr(Num); while not ((iAns = 6174) Or (iAns = Num)) do begin strAns := strAns + '->' + IntToStr(iAns) + '(' + IntToStr(iMax) + ' - ' + IntToStr(iMin) + ')'; iMax := MaxNum(iAns); iMin := MinNum(iAns); iAns := iMax - iMin; end; strAns := strAns + '->' + IntToStr(iAns); if iAns = 6174 then strAns := strAns + '->OK' else strAns := strAns + '->NG'; sl.Add(strAns); end;
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.