62,610
社区成员
发帖
与我相关
我的任务
分享
import java.util.LinkedList;
import java.util.List;
public class Demo {
private int[] a;
private int[] b;
private float[] c;
private int answer1;
private int answer2_1;
private float[][] answer2_2;
public static void main(String[] args) {
Demo d = new Demo();
String x="236,7526,1.9|308,,3|1133,1133,1.6|4151,,3|4263,4263,1.8|4389,5779,3|4638,1123,2|5779,,3|5942,233,0.9|8116,,3|8116,8116,3|8116,8116,3";
d.getDataFromStr(x);
d.getAnswer1();
d.getAnswer2_1();
d.printResult();
}
private void printResult() {
System.out.println("\n"+"a!=b的组数:"+answer1);
System.out.println("a==b 且 c>1.5的组数:"+answer2_1);
for(float[] f : answer2_2){
System.out.println(f[0]+" "+f[1]+" "+f[2]);
}
}
private void getAnswer2_1() {
int sum = 0;
List<Integer> indexs = new LinkedList<Integer>();
for(int index = 0, length = a.length; index < length; index ++){
if(a[index] == b[index] && c[index] > 1.5f){
indexs.add(index);
sum ++;
}
}
answer2_1 = sum;
int[] input = new int[sum];
for(int i = 0 ; i < sum; i++)
input[i] = indexs.get(i);
for (int j = 1; j < sum; j++) {
int key = input[j];
int index = binarySearch(input, c[key], 0, j - 1);
for (int i = j; i > index ; i--)
input[i] = input [i-1];
input[index] = key;
}
getAnswer2_2(input);
}
private void getAnswer2_2(int[] input) {
int length = input.length;
answer2_2 = new float[length][3];
for(int index = 0; index < length; index ++){
answer2_2[index][0] = a[input[index]];
answer2_2[index][1] = b[input[index]];
answer2_2[index][2] = c[input[index]];
}
}
private void getAnswer1(){
int sum = 0;
for(int index = 0, length = a.length; index < length; index ++){
if(a[index] != b[index]){
sum ++;
System.out.print(a[index]+","+b[index]+"|");
}
}
answer1 = sum;
}
private void getDataFromStr(String str){
String num[] = str.replaceAll("[|]", ",").split(",");
int len = num.length / 3;
a = new int[len];
b = new int[len];
c = new float[len];
for(int index=0,length=num.length,position=0;index < length;index+=3,position++){
a[position] = Integer.parseInt(num[index]);
b[position] = Integer.parseInt(num[index+1].equals("")?"0":num[index+1]);
c[position] = Float.parseFloat(num[index+2]);
}
}
private static int binarySearch(int[] input, float target, int from, int to)
{
int range = to - from;
if (range > 0){
int mid = (to + from) / 2;
if (input[mid] > target)
return binarySearch(input,target,from,mid-1);
else
return binarySearch(input,target,mid+1,to);
}else{
if (input[from] > target)
return from;
else
return from + 1;
}
}
}
//236,7526|308,0|4151,0|4389,5779|4638,1123|5779,0|5942,233|8116,0|
//a!=b的组数:8
//a==b 且 c>1.5的组数:4
//8116.0 8116.0 3.0
//8116.0 8116.0 3.0
//4263.0 4263.0 1.8
//1133.0 1133.0 1.6