1

こんにちは、最初の 3 文字を掛け合わせてリンク リストをアルファベット順に並べ替えようとしています。最初の文字は 26^2、2 番目の文字は 26^1、3 番目の文字は 26^0 となります。プログラムを実行すると、「lala」と「francis」という名前に対して同じ金額が表示されます。誰かがコードの何が問題なのかを理解するのを手伝ってくれたら、大歓迎です!

LinkedListNode クラス: (getSum メソッドを含む)

public class LinkedListNode 
{ 

    public String data; 
    public LinkedListNode next; 
   public long sum; 


    public LinkedListNode(String data)  
   { 
        this.data = data; 
        this.next = null; 
        this.sum = getSum(data); 
    }//end node 

   public long getSum(String line) 
    { 
        int i; 
        long sum = 0; 
        String s = null; 
      char a; 

           for(i=0; i < 3; i++) 
         { 
            int j = 2; 
            a = line.charAt(i);      
            sum += Character.getNumericValue(a) * Math.pow(26, j); 
            //Return the value of the number 4 to be the power of 3 (4*4*4): Math.pow(4,3); 
            j--; 
           }//end for  

        return sum; 
    }//end getSum 

    public long getSum() 
    { 
      return sum; 
    }//end getSum 

    public String getData()  
   { 
        return data; 
    }//end getData 
    public void setData(String data)  
   { 
        this.data = data; 
    }//end setData 
    public LinkedListNode getNext()  
   { 
        return next; 
    }//end node 
    public void setNext(LinkedListNode next)  
   { 
        this.next = next; 
    }//end setNext 

}//end class node

LinkedList クラス: (リスト用の他のメソッドがあります)

public class LinkedList { 

    public LinkedListNode front; 

    public LinkedList() { 
        this.front = null; 
    } 

    public void insertBack(String data) 
   { 
        if(front == null){ 
            front = new LinkedListNode(data); 
        }else{ 
            LinkedListNode newNode = new LinkedListNode(data); 
            LinkedListNode current = front; 
            while(current.getNext() != null){ 
                current = current.getNext(); 
            } 
            current.setNext(newNode); 
        }        
    }//end insertBack 

   public void addAfter(LinkedListNode spot, String data) 
   { 
       LinkedListNode newNode; 

       newNode = new LinkedListNode(data); 

       newNode.next = spot.next; 
       spot.next = newNode; 
   }//end addAfter 

   public void addBefore(LinkedListNode spot, String data) 
   { 

   }//end addBefore    

   public void deleteAfter(LinkedListNode spot) 
   { 
       LinkedListNode nextNode; 

       nextNode = spot.next; 
       spot.next = nextNode.next; 
   }//end deleteAfter 


    public String showList() 
   { 
        int i = 0; 
        String retStr = "The nodes in the list are:\n"; 
        LinkedListNode current = front; 
        while(current != null){ 
            i++; 
            retStr += "Node " + i + " is: " + current.getData() + " and the sum is: " + current.getSum() + "\n"; 
            current = current.getNext(); 

        } 

        return retStr; 
    } 

   public LinkedListNode findTail() 
   { 
       LinkedListNode current = front; 
        while(current.getNext() != null) 
      { 
            current = current.getNext(); 
        } 
      return current; 
   }//end findTail 
}

ファイルクラス:

import java.util.Scanner; 
import java.io.*; 

public class fileIn 
{ 
   LinkedListNode front; 
   LinkedList myList = new LinkedList(); 
   String fname; 

   public static void main(String[] args) 
    {    
       fileIn f = new fileIn(); 
   }//end main 


   public fileIn() 
   { 
      getFileName(); 
      readFileContents(); 
      System.out.print(myList.showList()); 
   }//end namesLinkedList 

   public void readFileContents() 
    { 
        boolean looping; 
        DataInputStream in; 
        String line; 
        int j, len; 
        char ch; 

        /* Read input from file and process. */
        try 
        { 
            in = new DataInputStream(new FileInputStream(fname)); 

            looping = true; 
            while(looping) 
             { 
                /* Get a line of input from the file. */
                if (null == (line = in.readLine()))  
                { 
                    looping = false; 
                    /* Close and free up system resource. */
                    in.close(); 
                }//end if 
                else 
                { 
                myList.insertBack(line); 
                    j = 0; 
                    len = line.length();   
                }//end else 
            } /* End while. */

        } /* End try. */

        catch(IOException e)  
        { 
            System.out.println("Error " + e); 
        } /* End catch. */
    }//end readFileContents 

     public void getFileName() 
     { 
        Scanner in = new Scanner(System.in); 

        System.out.println("Enter file name please."); 
        fname = in.nextLine(); 

     }//end getFileName 

}//end class namesLinkedList
4

2 に答える 2

2
for (i = 0; i < 3; i++) { 
    int j = 2; 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, j); 
    j--; 
}

指数は常に であるため、同じ結果が得られます2。これにより、fra( 15×26 2 + 27×26 2 + 10×26 2 = 35,152 ) とlal( 21×26 2 + 10×26 2 + 21×26 2 = 35,152 ) は同じ値になります。どうしてこれなの?

変数jは、ループの外側ではなく内側で宣言されています。2最後のデクリメントは、各反復の最初からやり直すため、効果がありません。

宣言をループの外に移動する必要があります。

int j = 2; 

for (i = 0; i < 3; i++) { 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, j); 
    j--; 
}

jまたは、余分な変数を完全に置き換えて2 - i取り除くこともできます。

for (i = 0; i < 3; i++) { 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, 2 - i); 
}
于 2013-11-05T19:39:35.853 に答える
0

あなたの計算は間違っているようです。Character.getNumericValue(a) は、あなたが考えているように、0 から 25 までの値を返しません。最初の 3 文字に基づいてソートし、それを使用する場合は、カスタム Comparator クラスを作成するだけです。

編集: getNumericValue の仕組みについては間違っていましたが、数学はまだ間違っています (以下のコメントを参照)。

于 2013-11-05T19:44:49.460 に答える