Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 88227 | Accepted: 35442 |
Description
Input
Output
Sample Input
10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT
Sample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAATTTGGCCAAA
import java.util.Scanner; public class Poj1007 { private static DNA[] ret; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); ret = new DNA[m]; for (int i = 0; i < m; i++) { DNA dna = new DNA(); dna.value = sc.next(); dna.sortNum = calSortNum(dna.value); // ret[i] = dna; addToArray(dna); } for (int i = 0; i < m; i++) { System.out.println(ret[i].value); } } // 计算排序值 private static int calSortNum(String str) { if (str.length() == 1) { return 0; } int sortF = 0; char c = str.charAt(0); for (int i = 1; i < str.length(); i++) { if (c > str.charAt(i)) { sortF++; } } return sortF + calSortNum(str.substring(1)); } //插入排序 private static void addToArray(DNA dna) { for (int i = 0; i < ret.length; i++) { if (ret[i] != null) { if (dna.sortNum < ret[i].sortNum) { // 开始移动 for (int j = ret.length - 1; j > i; j--) { ret[j] = ret[j - 1]; } ret[i] = dna; break; } } else { ret[i] = dna; break; } } } //构造一个DNA类,它有两个属性,一个是它的内容字符串,一个是它的排序值 private static class DNA { String value; int sortNum; } }
原文地址:http://blog.csdn.net/bear_huangzhen/article/details/45716777