歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java程序練習-DNA sorting

描述
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted).

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.

輸入
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
輸出
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.
樣例輸入
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
樣例輸出
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

參考代碼

  1. import java.io.BufferedInputStream;  
  2. import java.io.DataInputStream;  
  3. import java.util.Arrays;  
  4. import java.util.Comparator;  
  5. //import java.util.Scanner;   
  6. class DNA{  
  7.     String value;  
  8.     int level;  
  9. }  
  10. class DNAType implements Comparator<Object>{   
  11.     public int compare(Object arg0, Object arg1){  
  12.         DNA obj1 = (DNA) arg0;  
  13.         DNA obj2 = (DNA) arg1;  
  14.         return obj1.level - obj2.level;  
  15.     }  
  16. }  
  17. public class Main {  
  18.     public static void main(String[] args) throws Exception {  
  19.         int i;  
  20.         //Scanner cin = new Scanner(System.in);   
  21.         DataInputStream cin = new DataInputStream(new BufferedInputStream(System.in));  
  22.         //int col = cin.nextInt();   
  23.         //int row = cin.nextInt();   
  24.         //cin.nextLine();   
  25.         String s = new String();  
  26.         s = cin.readLine();  
  27.         String n[] = s.split(" ");  
  28.         int col = Integer.parseInt(n[0]);  
  29.         int row = Integer.parseInt(n[1]);  
  30.         DNA dna[] = new DNA[row];  
  31.         for(i = 0;i < row;++ i){  
  32.             String line = new String();  
  33.             //line = cin.nextLine();   
  34.             line = cin.readLine();  
  35.             dna[i] = new DNA();  
  36.             dna[i].value = line;  
  37.             dna[i].level = getLevel(line);  
  38.         }  
  39.         DNAType comp = new DNAType();  
  40.         Arrays.sort(dna,comp);  
  41.         for(i = 0;i < row;++ i){  
  42.             System.out.println(dna[i].value);  
  43.         }  
  44.     }  
  45.     private static int getLevel(String line) {  
  46.         int i,j,t = 0;  
  47.         for(i = 0;i < line.length();++ i){  
  48.             for(j = i + 1;j < line.length();++ j){  
  49.                 if(line.charAt(i) > line.charAt(j)){  
  50.                     ++ t;  
  51.                 }  
  52.             }  
  53.         }  
  54.         return t;  
  55.     }  
  56. }  
Copyright © Linux教程網 All Rights Reserved