手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Java程序练习-DNA sorting

[日期:2011-10-03] 来源:Linux社区  作者:binfeihan [字体:大 中 小]
描述
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. }  
linux
相关资讯       Java 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款