1

学習問題は、すでに提供されている数の桁根を見つけることです。先生は私たちに 2638 という数字を教えてくれました。数字の根を見つけるには、各数字を別々に足す必要があります 2 + 6 + 3 + 8 = 19. 次に、結果の 19 を取り、それらの 2 つの数字を足し合わせます 1 + 9 = 10 . もう一度同じことを行います 1 + 0 = 1. 桁根は 1 です.

私の最初のステップは、変数 total を使用して数値 2638 を合計し、合計 19 を見つけることでした。次に、2 番目の while ループを使用して、% を使用して 2 桁を分離しようとしました。

基本的な整数演算 (+、-、​​、/) を使用して問題を解決する必要があります。
1.ネストされたwhileループを使用して問題を解決する必要があるか、または可能ですか?
2.私の計算は正しいですか?
3.ここに書いたように、Eclipseでは動作しません。while ループを正しく使用していますか?

import acm.program.*;


public class Ch4Q7 extends ConsoleProgram {
   public void run(){
      println("This program attempts to find the digit root of your number: ");

      int n = readInt("Please enter your number: "); 

      int total = 0;

      int root = total;


      while (n > 0 ){
        total = total + (n %10);
    n = (n / 10);

    }   

   while ( total > 0 ){
     root = total; 
     total = ((total % 10) + total / 10); 
    }


     println("your root should be " + root);
}

}

4

7 に答える 7

2

私はそれが実行されると思いますが、少し多すぎます:-)

total = ((total % 10) + total / 10); 

0 に収束できません。また、そのままのプログラムでは、非常に特殊なケースしか処理できません。他の人が指摘したように、これは再帰的に解決できますが、二重ループだけでも解決できます。あなたが試したような一連のループは機能しません。

これを試してください(入力変数がプログラムと同じである場合、実際には2つのループのプラグイン置換です):

    do {
        while (n > 0) {
            total = total + (n % 10);
            n = (n / 10);
        }
        n = total;
        total = 0;
    } while (n > 9);  // need at least 1 more loop

このループの後、n にはルート番号が含まれます。

于 2012-10-24T06:44:22.590 に答える
1

再帰的な解決策:

public static void main(String[] args)
{
    System.out.println(getDigitRoot(1));
    System.out.println(getDigitRoot(11));
    System.out.println(getDigitRoot(1924));
    System.out.println(getDigitRoot(2638));
}

public static int getDigitRoot(int n)
{
    if (n < 10)
    {
        return n;
    }
    int sum = 0;
    while (n > 0)
    {
        sum += n % 10;
        n = n / 10;
    }
    return getDigitRoot(sum);
}

出力:

1
2
7
1

私はこの再帰について 2 つの考えがあります。手動で解決するときに行っていることにより厳密に従うため、理にかなっていますが、反復的に実装することはそれほど難しくなく、通常どおり、よりスケーラブルです。ほとんどの場合、パフォーマンスが重要になる可能性は低く、再帰の問題を引き起こすほど大きな数値を処理する可能性は低いため、問題にはならないと思います。

于 2012-10-24T10:59:44.093 に答える
1

1桁の数字が得られるまで、最後の桁のみを合計できます。

public static int getRoot(int n) {
    int root=n;
    while ( (root=((root%10) + root/10))>9  );
    return root;
}  

または再帰で:

public static int recursionGetRoot(int n) {
    if(n<10)
        return n;
    return n%10 + recursionGetRoot(n/10);
}  
于 2012-10-24T07:03:03.570 に答える
0

ループを作成して、番号が 10 以上かどうか、つまり 1 桁以上かどうかを確認します。内側のループで数字の追加を行います。中間結果を印刷して、計算が正しいかどうかを確認します。つまり、抽出した数字と合計を印刷します。必要に応じて、これを後で削除できます。数が 10 未満の場合、ルートがあります。

于 2012-10-24T06:41:35.047 に答える
0

自分でテストしてみませんか?以下は、いくつかのテスト (実際の結果と期待される結果の比較など) を作成するための非常に簡単な方法です。これらのすべてのテストに合格すれば、計算は正しくなります。

まず、別のメソッドで数学を抽出する必要があります。これは、入力を受け取って出力を提供するものが必要なためです。

public void run(){
  println("This program attempts to find the digit root of your number: ");

  int n = readInt("Please enter your number: "); 

  int root = calculateRoot(n);    

  println("your total should be " + root);
}

public static int calculateRoot(int n) {
  int total = 0;
  int root = total;
  while (n > 0 ){
    total = total + (n %10);
    n = (n / 10);
  }   

  while ( total > 0 ){
    root = total; 
    total = ((total % 10) + total / 10); 
  }

  return root;
}

mainこれで、いくつかのテストを実行するメソッドを作成できます。

public static void main(String[] args) {

  if (0 == calculateRoot(0)) {
    System.out.println("0: OK");
  }

  if (1234 == calculateRoot(10)) {
    System.out.println("1234: OK");
  }

  // and so on

}

したがって、アルゴリズムを実装してクラスを実行し、出力ですべてのテストに問題がないことを確認するだけです。これは非常に単純化されており、後で特別なテスト ツールを使用しますが、一般的なアプローチは同じです。すべての実装がすべてのテストに合格するまで、いくつかのテスト ケースとコードを定義します。

于 2012-10-24T06:44:09.770 に答える
0

2.私の計算は正しいですか?Yes 3.ここに書いたように、Eclipseでは動作しません。while ループを正しく使用していますか? No 1.ネストされたwhileループを使用して問題を解決する必要があるか、または可能ですか?

はい、上記の fvu によって与えられた答えとしてネストされたループを使用することは可能です

do {
    while (n > 0) {
        total = total + (n % 10);
        n = (n / 10);
    }
    n = total;
    total = 0;
} while (n > 9); 

ネストされたループを使用する必要がありますか。No再帰を使用できます

public static void run(){
      System.out.println("This program attempts to find the digit root of your number: ");

      //int n = 234567; 

      int total = 23456;

    //  int root = total;


          total=Test.calctotal(total);

      System.out.println("Root:"+total);
}
public static int calctotal(int n)
{
    int total=0;
    System.out.println(n);
    if(n>9)
    {
        while (n > 0 ){
            total = total + (n %10);
            n = (n / 10);
        }  
        total=Test.calctotal(total);
    }
            else
                total=n;
    return total;
}
于 2012-10-24T07:00:29.977 に答える
0
  int root = 0;
  int t = 0;

  while (true)
  {
    root = root + (n %10);
    t = n/10;
    n = t;

    if(t == 0)
    {
        if(root < 10)
            break;

        n = root;
        root = 0;
    }
}   

またはさらに単純な

int root = 0;

while (n > 0)
{
    root = root + n%10;
    n = n/10;
}

if((root == 0) || (root%9 != 0))
    root = root%9;
else
    root = 9;        
于 2012-10-24T07:00:33.500 に答える