教えてください


コマル  2005-07-28 23:40:21  No: 16675

プログラミングの授業を取ったんですけど、どうしてもわからなくて、7月30日までにプログラムを作って出さないといけないんです。単位落としたら来年やばい〜〜(汗)何とかお願いします
問題は
1111で割れない10000未満の自然数に対して、各桁をバラバラにして組み合わせ、最大値から最小値をひくという操作を考える。この操作を何回も行うと6174になることを確かめよ。
例、1234→3087(4321−1234)→8352(8730−378)→6174(8532−2358)
でした。
何を言っているのかさっぱりわかりません。おまけに授業もまともに出てなかったから余計わかりませんよ(号泣)
覚えているのは、C言語、コンパイル、後は。。。。。。コマンドなんとかソフトらしいもの
です。そんなにハイレベルのものじゃないと思いますので、一番簡単なプログラムをお願いします。習ってないプログラムを使っちゃうと逆に言われるかもしれないです。何とか今回ばかりは助けてください!できたら僕にメールで送っていただけますか。
皆さんありがとうございます。
追伸
先生は確か必ず  #include (stdio.h)で始まるとか言ってました。ご存知でしょうけど、一応申し上げておきます。


deldel  2005-07-28 23:52:44  No: 16676

ここは「Delphi Q & A 掲示板」です。


deldel  2005-07-29 00:38:40  No: 16677

Delphiで簡単に書くとこんなもんですかね・・・

var
  i: array [1..24] of integer;
  s: String;
  sData: String;
  iMax, iMin: integer;
begin
  if (StrToInt(Edit1.Text) mod 1111) = 0 then begin
    ShowMessage('1111で割り切れます!!');
    Exit;
  end;

  sData := Edit1.Text;

  while True do begin
    s := Format('%.4d', [StrToInt(sData)]); //0000〜9999

    i[1]  := StrToInt(s[1] + s[2] + s[3] + s[4]);
    i[2]  := StrToInt(s[1] + s[2] + s[4] + s[3]);
    i[3]  := StrToInt(s[1] + s[3] + s[2] + s[4]);
    i[4]  := StrToInt(s[1] + s[3] + s[4] + s[2]);
    i[5]  := StrToInt(s[1] + s[4] + s[2] + s[3]);
    i[6]  := StrToInt(s[1] + s[4] + s[3] + s[2]);
    i[7]  := StrToInt(s[2] + s[1] + s[3] + s[4]);
    i[8]  := StrToInt(s[2] + s[1] + s[4] + s[3]);
    i[9]  := StrToInt(s[2] + s[3] + s[1] + s[4]);
    i[10] := StrToInt(s[2] + s[3] + s[4] + s[1]);
    i[11] := StrToInt(s[2] + s[4] + s[1] + s[3]);
    i[12] := StrToInt(s[2] + s[4] + s[3] + s[1]);
    i[13] := StrToInt(s[3] + s[1] + s[2] + s[4]);
    i[14] := StrToInt(s[3] + s[1] + s[4] + s[2]);
    i[15] := StrToInt(s[3] + s[2] + s[1] + s[4]);
    i[16] := StrToInt(s[3] + s[2] + s[4] + s[1]);
    i[17] := StrToInt(s[3] + s[4] + s[1] + s[2]);
    i[18] := StrToInt(s[3] + s[4] + s[2] + s[1]);
    i[19] := StrToInt(s[4] + s[1] + s[2] + s[3]);
    i[20] := StrToInt(s[4] + s[1] + s[3] + s[2]);
    i[21] := StrToInt(s[4] + s[2] + s[1] + s[3]);
    i[22] := StrToInt(s[4] + s[2] + s[3] + s[1]);
    i[23] := StrToInt(s[4] + s[3] + s[1] + s[2]);
    i[24] := StrToInt(s[4] + s[3] + s[2] + s[1]);

    iMax := MaxIntValue(i);
    iMin := MinIntValue(i);

    sData := IntToStr(iMax - iMin);

    ListBox1.Items.Add(IntToStr(iMax) + ' - ' + IntToStr(iMin) + ' = ' + sData); //確認

    if sData = '6174' then begin
      ShowMessage(IntToStr(iMax) + ' - ' + IntToStr(iMin) + ' = 6174');
      Break;
    end;
  end;
end;


にしの  2005-07-29 00:58:10  No: 16678

私も書いてみました。
もちろん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;


cscs  2005-07-29 06:08:29  No: 16679

>おまけに授業もまともに出てなかったから余計わかりませんよ(号泣)
自業自得でしょう。

ここはDelphi掲示板ですが、少しボーナスでC#で書いてみます。
ソースは汚いですが仕様です。

using System;

class MainC
{
  public static void Main(string[] args) {
    int n = 0;
    
    while (true) {
      try {
        Console.WriteLine("1111で割れない10000未満の自然数を入力してください。");
        Console.Write("値:? ");

        int ans = 0;
        n = int.Parse(Console.ReadLine());
        
        if ( n % 1111 != 0 && n > 0 && n < 10000 ) {
          do {
            String buf = "000" + n;
            buf = buf.Substring(buf.Length - 4);

            char[] ArrMax = buf.ToCharArray();
            for (int i = 0; i < 3; i++) {
              for (int j = i + 1; j < 4; j++) {
                if ( ArrMax[i] < ArrMax[j] ) {
                  char w = ArrMax[i];
                  ArrMax[i] = ArrMax[j];
                  ArrMax[j] = w;
                }
              }
            }
            
            int max = int.Parse(new String(ArrMax));

            char[] ArrMin = buf.ToCharArray();
            for (int i = 0; i < 3; i++) {
              for (int j = i + 1; j < 4; j++) {
                if ( ArrMin[i] > ArrMin[j] ) {
                  char w = ArrMin[i];
                  ArrMin[i] = ArrMin[j];
                  ArrMin[j] = w;
                }
              }
            }
            int min = int.Parse(new String(ArrMin));

            ans = max - min;
            Console.WriteLine(max + " - " + min + " = " + ans);
            n = ans;
          } while ( ans != 6174);
          return;
        }
      } catch ( FormatException ) {
        return;
      }
    }
  }
}


cscs  2005-07-29 06:13:59  No: 16680

とまぁ、質問者の要求に答えていないプログラムになってましたね(汗


みんな  2005-07-29 06:29:50  No: 16681

> C言語
掲示板のタイトルをよく見てね。

> 極力無駄な書き込み等はしないようお願い致します。
みんな、管理人さんの書き込み読んでないのか?
https://www.petitmonte.com/bbs/answers?question_id=3058


勝手な人  2005-07-29 10:08:26  No: 16682

適当に流して見ていましたが、
質問主の文章を、見れば見るほど勝手な人に思えてきますね。
ましてや、プログラミング以前の問題で躓いているんだもん。
単位落とした方が、逆にいいのでは。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加