import java.text.DecimalFormat; import java.util.*;
public class MathAlgorithm { private final static double dmax = 999; private final static double dmin = Double.MIN_VALUE; private final static int n = 100;
public static void main(String[] args) { Random random = new Random(); double[] x = new double[n]; for (int i = 0; i < n; i++) { x[i] = Double.valueOf(Math.floor(random.nextDouble() * (dmax - dmin))); System.out.println(x[i]); } DecimalFormat df = new DecimalFormat("#,##0.00"); double dV = getVariance(x); System.out.println("方差=" + df.format(dV)); double dS = getStandardDiviation(x); System.out.println("标准差=" + df.format(dS)); int[] intArr={5,10,15,8,6}; System.out.println(Arrays.toString(intArr)+" 中位数:"+median(intArr)); int[] intArr2={5,10,15,8,6,7}; System.out.println(Arrays.toString(intArr2)+" 中位数:"+median(intArr2)); int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 1, 2, 2, 3, 4, 5};
List<Integer> modalNums = getModalNums(arr); System.out.println("众数:"+modalNums); float[] arr2 = {0.1f, 1.1f, 2.1f, 3.1f, 4.1f, 5.1f, 6.1f, 7.1f, 8.1f, 9.1f, 10.1f, 1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 4.1f, 5.1f};
List<Float> modalNums2 = getModalNums(arr2);
System.out.println("众数:"+modalNums2); }
public static double getVariance(double[] x) { int m = x.length; double sum = 0; for (int i = 0; i < m; i++) { sum += x[i]; } double dAve = sum / m; double dVar = 0; for (int i = 0; i < m; i++) { dVar += (x[i] - dAve) * (x[i] - dAve); } return dVar / m; }
public static double getStandardDiviation(double[] x) { int m = x.length; double sum = 0; for (int i = 0; i < m; i++) { sum += x[i]; } double dAve = sum / m; double dVar = 0; for (int i = 0; i < m; i++) { dVar += (x[i] - dAve) * (x[i] - dAve); } return Math.sqrt(dVar / m); }
public static int median(int []nums){ if(nums.length==0) return 0; int start=0; int end=nums.length-1; int index=partition(nums, start, end); if(nums.length%2==0){ while(index!=nums.length/2-1){ if(index>nums.length/2-1){ index=partition(nums, start, index-1); }else{ index=partition(nums, index+1, end); } } }else{ while(index!=nums.length/2){ if(index>nums.length/2){ index=partition(nums, start, index-1); }else{ index=partition(nums, index+1, end); } } } return nums[index]; } private static int partition(int nums[], int start, int end){ int left=start; int right=end; int pivot=nums[left]; while(left<right){ while(left<right&&nums[right]>=pivot){ right--; } if(left<right){ nums[left]=nums[right]; left++; } while(left<right&&nums[left]<=pivot){ left++; } if(left<right){ nums[right]=nums[left]; right--; } } nums[left]=pivot; return left; }
public static float median(float []nums){ if(nums.length==0) return 0; int start=0; int end=nums.length-1; int index=partition(nums, start, end); if(nums.length%2==0){ while(index!=nums.length/2-1){ if(index>nums.length/2-1){ index=partition(nums, start, index-1); }else{ index=partition(nums, index+1, end); } } }else{ while(index!=nums.length/2){ if(index>nums.length/2){ index=partition(nums, start, index-1); }else{ index=partition(nums, index+1, end); } } } return nums[index]; } private static int partition(float nums[], int start, int end){ int left=start; int right=end; float pivot=nums[left]; while(left<right){ while(left<right&&nums[right]>=pivot){ right--; } if(left<right){ nums[left]=nums[right]; left++; } while(left<right&&nums[left]<=pivot){ left++; } if(left<right){ nums[right]=nums[left]; right--; } } nums[left]=pivot; return left; }
public static List<Integer> getModalNums(int[] arr) { int n = arr.length;
if (n == 0) { return new ArrayList<Integer>(); }
if (n == 1) { return Arrays.asList(arr[0]); }
Map<Integer, Integer> freqMap = new HashMap<>(); for (int i = 0; i < n; i++) { Integer v = freqMap.get(arr[i]); freqMap.put(arr[i], v == null ? 1 : v + 1); }
List<Map.Entry<Integer, Integer>> entries = new ArrayList<>(freqMap.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<Integer, Integer>>() { @Override public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) { return e2.getValue() - e1.getValue(); } });
List<Integer> modalNums = new ArrayList<>(); modalNums.add(entries.get(0).getKey());
int size = entries.size(); for (int i = 1; i < size; i++) { if (entries.get(i).getValue().equals(entries.get(0).getValue())) { modalNums.add(entries.get(i).getKey()); } else { break; } }
return modalNums; }
public static List<Float> getModalNums(float[] arr) { int n = arr.length;
if (n == 0) { return new ArrayList<Float>(); }
if (n == 1) { return Arrays.asList(arr[0]); }
Map<Float, Integer> freqMap = new HashMap<>(); for (int i = 0; i < n; i++) { Integer v = freqMap.get(arr[i]); freqMap.put(arr[i], v == null ? 1 : v + 1); }
List<Map.Entry<Float, Integer>> entries = new ArrayList<>(freqMap.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<Float, Integer>>() { @Override public int compare(Map.Entry<Float, Integer> e1, Map.Entry<Float, Integer> e2) { return e2.getValue() - e1.getValue(); } });
List<Float> modalNums = new ArrayList<>(); modalNums.add(entries.get(0).getKey());
int size = entries.size(); for (int i = 1; i < size; i++) { if (entries.get(i).getValue().equals(entries.get(0).getValue())) { modalNums.add(entries.get(i).getKey()); } else { break; } }
return modalNums; } }
|