0

だから、私はErmipsを決定するプログラムを書くことになっています。残りの部分はわかりましたが、番号を正しく逆にする方法がわかりません。私はそれを逆にするために配列を使うことになっています。

たとえば、番号357です。

mod演算子を使用して最後の桁を取得し、それを配列の最初のインデックスに配置します。

357%10 = 7

myArray[0] = 7

357/10 = 35残りのために

残りの35を使用して最初からやり直します。

35%10 = 3

myArray[1] = 3

35/10 = 3 for a remainder

など..。

基本的にこれをループする必要があるので、任意の長さの数値を実行して逆にすることができます。

次に、その配列を取得したら、配列を表示して逆に数値を生成します....753。

public class Reverse {
        public static void main(String[]args) {
        int n = 357;
        int MAX_NUMBERS = 20;
        int currentNumber = 0;
        int reverseNumber = 0;
        int remain = 0;
        int sum = 0;

                  int [] holdDigits = new int [MAX_NUMBERS];


        int exp = holdDigits.length;
        System.out.println("exp: " + exp);
        int index = 0;

                  //sum array
       int count = holdDigits.length;
       while (count > 0){
        holdDigits[index] = n%10;
        System.out.println(index + "index: " + holdDigits[index]);
        n = n/10;
        System.out.println("remainder: " + n);

        count--;

        index++;
        }

        while (index < holdDigits.length){
        reverseNumber += holdDigits[index]*Math.pow(10,count-exp);
        index--;
        System.out.println("sum so far: " + sum);
        }

    System.out.println("Number reversed: " + reverseNumber);
         }//end of main
    }//end of class

Yogendra Singhのおかげで、今では完全に理解できました。見てみな:

    public class Reverse2 {

    public static void main(String[]args) {


    int n = 76495;
    int MAX_NUMBERS = 20;
    int reverseNumber = 0;
    int index = 0;  

    //declare an array to hold the digits while reversing
    int [] holdDigits = new int [MAX_NUMBERS];

    //the exponent is the number of spaced used in the array
    int exp = holdDigits.length;  

    //while the number is greater than 0, use mod to put the right-most
    //digit in index 0, divide the remaining number and increase the index
    //to put it in the next open slot of the array.
    while (n > 0){
        holdDigits[index] = n%10;
        n = n/10;
        index++;
    }

    //decrease the index by one so it doesn't add the remaining zero as
    //a placeholder in the number
    index--;

    //count is the index because below, you subtract it, making the display
    //of the array reversed.
    int count= index;

    //while the index is greater than zero, by starting at the last filled 
    //slot of the array, the reverse number is added onto each time by 
    //multiplying the number times 10 to the power of whichever place it
    //is which happens to be the index. 
    //EXAMPLE: to turn 7 into 700, multiply by 7x10^3
    while (index >= 0 ){
        reverseNumber += holdDigits[count-index]*Math.pow(10,index);

        //lower the index to do the next number of the array
        index--;
    }

    System.out.println("Reversed number: " + reverseNumber);


    }//end of main


}//end of class
4

2 に答える 2

1

コードには次のような問題があります。

  1. 除算の余りが 0 になるまで最初のループを実行する
  2. 割り算で出た桁を数える
  3. while ループでポスト インクリメントされるため、最初のループの後でインデックスを 1 減らします。

修正されたサンプル コードは次のようになります。

    int exp = holdDigits.length;
    System.out.println("exp: " + exp);
    int index = 0;
    while (n > 0){
        holdDigits[index] = n%10;
        System.out.println(index + "index: " + holdDigits[index]);
        n = n/10;
        System.out.println("remainder: " + n);
        index++;
    }
    index--;
    int count= index;
    while (index >=0 ){
        reverseNumber += holdDigits[count-index]*Math.pow(10,index);
        index--;
        System.out.println("sum so far: " + sum);
    }
于 2012-10-31T02:46:46.640 に答える