我的评论

re: 接口测试的两种方法[未登录] 小飞侠 2013-09-10 16:45  
不错的文章
re: Java中文&编码问题小结 小飞侠 2006-03-14 18:08  
楼主总结的很全啊 看了以后感触很深~~非常感谢!
re: ImageLayers(入围赛750分真题) 小飞侠 2005-12-12 11:49  
哈,是的:)

把自己的排序,改成Arrays.sort(split) :)

嘻嘻,今天要比赛了,祝福各位,祝福emu, 也祝福我,小飞侠:)嘻嘻
re: SongRenamer (入围赛250分真题) 小飞侠 2005-12-11 22:35  
public class test {
public static String[] rename(String[] artists, String[] albums, String[] tracks, String[] titles, String format) {
StringBuffer[] ret;
String[] rets;
int i, n, j;

n = format.length();
ret = new StringBuffer[artists.length];
//初始化
for (i = 0; i < ret.length; i++)
ret[i] = new StringBuffer();

for (j = 0; j < artists.length; j++) {
for (i = 0; i < n; i++) {
switch(format.charAt(i)) {
case 'A' :
ret[j].append(artists[j]);
break;
case 'B' :
ret[j].append(albums[j]);
break;
case 'T' :
ret[j].append(tracks[j]);
break;
case 'S' :
ret[j].append(titles[j]);
break;
default :
ret[j].append(format.charAt(i));
break;
}
}
}

rets = new String[ret.length];
for (i = 0; i < ret.length; i++) {
rets[i] = ret[i].toString();
}

for (i = 0; i < rets.length; i++) {
System.out.println(rets[i]);
}

return rets;
}

public static void main(String args[]) {
String[] artists = {"Ignored"},
albums = {"Unnoticed"},
tracks = {"000"},
titles = {"Uncredited"};
String format = "()-(). ";

rename(artists, albums, tracks, titles, format);
}
}
re: ImageLayers(入围赛750分真题) 小飞侠 2005-12-11 17:55  
public class test {
public static String[] content(String[] macro) {
String[] mystr;
int cur, i,j, n, begin, end;
n = macro.length;
cur = 0;
mystr = new String[n];
//第一步操作,获取merge的String数组
for (i = 0; i < n; i++) {
if (macro[i].startsWith("OPEN")) {
mystr[cur] = macro[i].substring(5);
}

if (macro[i].startsWith("MERGE")) {
begin = Integer.parseInt(macro[i].substring(6, macro[i].indexOf("-")));
end = Integer.parseInt(macro[i].substring(macro[i].indexOf("-")+1));

//实现合并操作
cur = begin;
for (j = begin + 1; j <= end; j++) {
mystr[cur] = mystr[cur].concat(" ").concat(mystr[j]);
mystr[j] = null;
}
}
cur++;
}

for (i = 0; i < n; i++) {
System.out.println(mystr[i]);
}

//下面将原始的合并后的字符串数组,排序: 排序后,返回真正的数组
mystr = sorts(mystr);

System.out.println("sort ===");
for (i = 0; i < mystr.length; i++) {
System.out.println(mystr[i]);
}

return mystr;
}

public static String[] sorts(String[] mystr) {
String[] ret;
int i, j, n, cnt, cur=0;

n = mystr.length;
cnt = 0;

//获取实际的数组单元
for(i = 0, n = mystr.length; i < n; i++) {
if (mystr[i] != null)
cnt++;

}

ret = new String[cnt];

for (i = 0; i < n; i++) {
//对单元进行排序
if (mystr[i] != null) {
ret[cur++] = sort(mystr[i]);
}

}


System.out.println(cnt);
return ret;
}

//单元排序, 最简单的冒泡排序
public static String sort(String str) {
String[] a;
int i, j,n, flag;
String temp;

if (str.indexOf(" ") < 0) {
return str;
}

//开始排序
a = str.split(" ");
n = a.length;
for (i = 1; i < n; i++) {
flag = 0;
for (j = 0; j < (n - i) ; j++) {
if (a[j].compareTo(a[j+1]) > 0) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = 1;
}
}

if (flag == 0) {
break;
}
}

//merge
str = a[0];
for (i = 1; i < n; i++) {
str = str.concat(" ").concat(a[i]);
}

return str;
}

public static void main(String args[]) {
String[] macro = {"OPEN sky",
"OPEN clouds",
"OPEN ground",
"MERGE 0-1",
"OPEN grass",
"MERGE 0-2",
"OPEN trees",
"OPEN leaves",
"OPEN birds",
"MERGE 1-2",
"MERGE 0-1"};

content(macro);

}
}
re: Crop(入围赛250分真题) 小飞侠 2005-12-11 17:52  
public class test {
public static String[] crop(String[] image, String[] crops) {
String[] result, crop;
int row0, row1, col0, col1, x0, y0, x1, y1, n, i;

row0 = col0 = 0;
row1 = image.length - 1;
col1 = image[0].length() - 1;
for (i = 0, n = crops.length; i < n; i++) {
crop = crops[i].split(" ");
x0 = Integer.parseInt(crop[0]);
y0 = Integer.parseInt(crop[1]);
x1 = Integer.parseInt(crop[2]);
y1 = Integer.parseInt(crop[3]);

//边界判断
if (x0 < 0 || x0 > x1 || x1 > row1)
continue;
if (y0 < 0 || y0 > y1 || y1 > col1)
continue;

row1 = row0 + x1;
col1 = col0 + y1;
row0 += x0;
col0 += y0;

}

//输出
result = new String[row1 - row0 + 1];
for (i = 0, n = result.length; i < n ; i++) {
result[i] = image[i+row0].substring(col0, col1+1);
}

for (i = 0, n = result.length; i < n ; i++) {
System.out.println(result[i]);
}
return result;
}

public static void main(String args[]) {
String[] image, crops, result;

image = new String[4];
crops = new String[2];

image[0] = new String("X.X.X.X.X.X.X.X");
image[1] = new String(".X.X.X.X.X.X.X.");
image[2] = new String(".X.X.X.X.X.X.X.");
image[3] = new String("X.X.X.X.X.X.X.X");


crops[0] = new String("1 1 2 3");
crops[1] = new String("0 0 1 1");

crop(image, crops);

}

}
re: HouseParty(赛前模拟题) 小飞侠 2005-12-10 17:48  
以上仅仅是按照自己的想法,做了一个大概的程序流程,尚未进行任何优化,但是思想很简单, 首先遵循三个原则, 门最大使用度,窗最大使用度, 尽可能接近正方形. 然后,在约束条件和三大原则的指导下,先初始化房子(即可能的最小面积),之后,根据剩下的原始材料进行添窗加墙. 最后剩下的就是无法使用的材料.

其实,这个方法还是很麻烦,如果不需要考虑房子怎么建造,而仅仅是需要一个最大面积,还有有更简单的方法, 思想就是,只要满足约束条件,剔除多余的原材料,剩下的都是等单元,将剩下的所有,管他是门是窗是墙,加起来,求一个尽可能正方的结果,完.

re: HouseParty(赛前模拟题) 小飞侠 2005-12-10 17:34  
//--接上
public static int getArea(int x, int y) {
return x * y;
}

public static int maxArea(int wall, int window, int door) {
int[] res = new int[3];
StringBuffer[] room;
int i, j;

System.out.println("总材料, wall="+wall+";window"+window+";door"+door);

if (door > 4) door = 4;
res[0] = wall;
res[1] = window;
res[2] = door;

if (door == 0) {
System.out.println("Room can not be inited");
return 0;
}

//第一步,初始化房间,原则尽可能多的使用门.
room = initRoom(res);

if (room[0].length() == 0) {
System.out.println("Room can not be inited");
return 0;
}

//测试初始化情况,输出图形,和输出剩下的元素.
System.out.println("初始化房子:");
for(i=0; i < 4; i++) {
System.out.println(room[i]);
}
System.out.println("剩余的材料:");
System.out.println("wall="+res[0]);
System.out.println("window="+res[1]);
System.out.println("door="+res[2]);


//第二步,初始化成功后,开始有条件约束的添加窗和门.
if (res[0] < 2 ) {
return getArea(room[0].length(), room[1].length());
}

//获取当前条件下,能使用的窗的总数,
if (res[1] > 0) {
if ( res[1] % 2 == 0 ) {
//如果窗户是偶数
if (res[0] < res[1]) {
res[1] = res[0];
}
} else {
//如果窗户是奇数
if (res[0] < res[1] + 2) {
res[1] = res[0] - 2;
}
}
}


//获取了能使用的窗总数后,判断现在的窗的奇偶.
j = room[0].length() >= room[1].length() ? 1 : 0;
if (res[1] % 2 == 0){
while(res[1] > 0 && res[0] >= 2) {
room[j].append("W-");
room[j+2].append("W-");
res[0] -= 2;
res[1] -= 2;
j = 1 - j;
}
} else {
while(res[1] > 1 && res[0] >= 2) {
room[j].append("W-");
room[j+2].append("W-");
res[0] -= 2;
res[1] -= 2;
j = 1 - j;
}
//余一
if (res[0] >= 3) {
room[j].append("W-");
room[j+2].append("--");
res[0] -= 3;
res[1] -= 1;
j = 1 - j;
}
}

//最后,如果剩余的墙,按照偶数个添加.
if (res[0] % 2 == 0){
while(res[0] > 0) {
room[j].append("-");
room[j+2].append("-");
res[0] -= 2;
j = 1 - j;
}
} else {
while(res[0] > 1) {
room[j].append("-");
room[j+2].append("-");
res[0] -= 2;
j = 1 - j;
}
//余一, 不处理

}


//测试初始化情况,输出图形,和输出剩下的元素.
for(i=0; i < 4; i++) {
System.out.println(room[i]);
}
System.out.println("剩余的材料:");
System.out.println("wall="+res[0]);
System.out.println("window="+res[1]);
System.out.println("door="+res[2]);


return getArea(room[0].length(), room[1].length());
}

public static void main(String args[]) {
int wall, door, window, mymax;

wall = 6;
window = 23;
door = 13;
mymax = maxArea(wall, window, door);

System.out.println("最大面积="+mymax);
}
}

re: HouseParty(赛前模拟题) 小飞侠 2005-12-10 17:33  
public class test {
//初始化房间,扣除使用的原材料,返回初始化房间数组
public static StringBuffer[] initRoom(int bres[]) {
StringBuffer[] myroom = new StringBuffer[4];

//数组内元素的初始化
myroom[0] = new StringBuffer();
myroom[1] = new StringBuffer();
myroom[2] = new StringBuffer();
myroom[3] = new StringBuffer();

if (bres[2] == 4) {
if (bres[0] >= 8) {
//初始化4个门的room
myroom[0].append("-D-");
myroom[1].append("-D-");
myroom[2].append("-D-");
myroom[3].append("-D-");
bres[0] -= 8;
bres[2] -= 4;
return myroom;
}
}

if (bres[2] >= 3) {
if (bres[1] >= 1) {
if (bres[0] >= 8) {
//初始化3个门的room
myroom[0].append("-D-");
myroom[1].append("-D-");
myroom[2].append("-D-");
myroom[3].append("-W-");
bres[0] -= 8;
bres[1] -= 1;
bres[2] -= 3;
return myroom;
}
} else {
if (bres[0] >= 9) {
myroom[0].append("-D-");
myroom[1].append("-D-");
myroom[2].append("-D-");
myroom[3].append("---");
bres[0] -= 9;
bres[2] -= 3;
return myroom;
}
}
}

if (bres[2] >= 2) {
if (bres[0] >= 6) {
//初始化4个门的room
myroom[0].append("-D-");
myroom[1].append("-");
myroom[2].append("-D-");
myroom[3].append("-");
bres[0] -= 6;
bres[2] -= 2;
return myroom;
}
}

if (bres[2] >= 1) {
if (bres[1] >= 1) {
if (bres[0] >= 6) {
//初始化3个门的room
myroom[0].append("-D-");
myroom[1].append("-");
myroom[2].append("-W-");
myroom[3].append("-");
bres[0] -= 6;
bres[1] -= 1;
bres[2] -= 1;
return myroom;
}
} else {
if (bres[0] >= 7) {
myroom[0].append("-D-");
myroom[1].append("-");
myroom[2].append("---");
myroom[3].append("-");
bres[0] -= 7;
bres[2] -= 1;
return myroom;
}
}
}

return myroom;
}

-->>未完待续
public class CursorPosition {
public static int getPosition(String commonds, int ncount) {
//获取开始查询的位置
int fromIndex1, fromIndex2, fromIndex, position;

fromIndex1 = commonds.lastIndexOf((int)'E');
fromIndex2 = commonds.lastIndexOf((int)'H');
if (fromIndex1 > fromIndex2) {
fromIndex = fromIndex1 >0 ? fromIndex1 : 0;
position = ncount;
} else {
fromIndex = fromIndex2 > 0 ? fromIndex2 : 0;
position = 0;
}

//取字符串的字符,开始进行统计
for (int i = fromIndex + 1; i < commonds.length(); i++) {
System.out.println(commonds.charAt(i));
switch(commonds.charAt(i)) {
case 'L' :
position = (position == 0) ? 0 : --position;
break;
case 'R' :
position = (position == ncount) ? ncount : ++position;
break;
default :
break;
}
}

return position;
}
}
public class MatrixTool{
public String[] convert(String s) {
int slen, col;
slen = s.length();
col = (int)Math.sqrt(slen);
if ( slen % col != 0) {
String[] ret = new String[0];
return ret;
}

String[] ret = new String[col];
for (int i = 0; i < col; i++) {
ret[i] = s.substring(i*col, (i+1)*col);
}

return ret;
}
}

posts - 2, comments - 1, trackbacks - 0, articles - 2

Copyright © 小飞侠