您的位置:首页 > 其它

POJ-1007

2013-04-26 19:55 190 查看
哎,这是手贱了,用java写一点都不爽,可能是我不会用java吧T_T。
这题由于数据范围小,水水就过了。
import java.util.*;

public class Main {
public static void main(String[] args) {

Astruct[] s = new Astruct[105];

Scanner in = new Scanner(System.in);

int m = in.nextInt();
int n = in.nextInt();

for (int i=0; i<n; i++)
{
s[i] = new Astruct(in.next());
for (int j=0; j<m-1; j++)
for (int l=j+1; l<m; l++)
if (s[i].str.charAt(j) > s[i].str.charAt(l))
s[i].count++;
}

sort(0, n-1, s);
for (int i=0; i<n; i++)
System.out.println(s[i].str);

}

public static void sort(int l, int r, Astruct[] s)
{
int mid = s[(l+r) / 2].count;
int i = l, j = r;
while (i <= j)
{
while (s[i].count < mid) i++;
while (s[j].count > mid) j--;
if (i <= j)
{
if (s[i].count != s[j].count)
s[i].swap(s[j]);
i++; j--;
}
}
if (i<r) sort(i,r,s);
if (l<j) sort(l,j,s);
}
}

class Astruct
{
public Astruct(String ss)
{
str = ss;
count = 0;
}
public Astruct() {}

public void swap(Astruct a)
{
int temp = count; count = a.count; a.count = temp;
String ss = str; str = a.str; a.str = ss;
}

public String str;
public int count;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: