日期:2014-05-20 浏览次数:20669 次
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 不规则列表的数据起止判定 * @author jinxfei * */ public class BGZList { private static Random r=new Random(System.currentTimeMillis()); public static void main(String[] args) throws Exception{ List<Integer> targetList=createBGZList(); System.out.println("随机数组:\n"+targetList); System.out.println("请输入起始值:\n"); BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); int start=Integer.parseInt(r.readLine()); System.out.println("请输入终止值:\n"); int end=Integer.parseInt(r.readLine()); end-=start; int startIndex=0,startPos=0; int endIndex=0,endPos=0; for(int i=0; i<targetList.size();i++){ Integer cur=targetList.get(i); if (start>=0){ if (start>cur){ start-=cur; continue; } startIndex=i+1; startPos=start; start=-1; }else{ if (end>cur){ end-=cur; continue; } endIndex=i+1; endPos=end; break; } } System.out.println("开始于 "+startIndex+" 的 "+startPos+", 结束于 "+endIndex+" 的 "+endPos); } /** * 创建一个随机长度(10-110)的list,list中的每一个数字也都是随机的(5-55), * * @return */ private static List<Integer> createBGZList(){ List<Integer> result=new ArrayList<Integer>(); int listLength=r.nextInt(100)+10; for(int i=0; i<listLength;i++){ result.add(r.nextInt(50)+5); } return result; } }
------解决方案--------------------
写好了,可能不是很规范 ,但可以得到结果。考到java里面直接可以运行
package a;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static List<int[]> list = new ArrayList<int[]>();
public Test() {
int[] a = { 1, 2, 3 };
int[] b = { 4, 5, 6, 7, 8, 9, 10 };
int[] c = { 11, 12, 13, 14 };
list.add(a);
list.add(b);
list.add(c);
}
public int[] function(int x, int y) {
int[] returnBack = new int[4];
int[] a = get(x);
int[] b = get(y);
returnBack[0] = a[0];
returnBack[1] = a[1];
returnBack[2] = b[0];
returnBack[3] = b[1];
return returnBack;
}
public static int[] get(int x) {
int[] returnBack = new int[2];
int sum = 0;
int cout = 0;
for (int n = 0; n < list.size(); n++) {
sum = sum + (list.get(n)).length;
if (x <= sum && cout == 0) {
cout++;
returnBack[0] = n + 1;
for (int i = 0; i < (list.get(n)).length; i++) {
if (x == list.get(n)[i]) {
returnBack[1] = i + 1;
}
}
}
}
return returnBack;
}
public static void main(String[] args) {
Test test = new Test();
int[] aa = test.function(3, 14);
for (int i = 0; i < aa.length; i++) {
System.out.println(aa[i]);
}
}
}
------解决方案--------------------