新增判读方案

This commit is contained in:
caixiang
2022-07-13 16:41:43 +08:00
parent facfe5c7a2
commit 970359823b
18 changed files with 1100 additions and 14 deletions

View File

@@ -0,0 +1,101 @@
package com.cnbm.common.spc.math;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class Math {
public static void main(String[] args) {
// try(Scanner input = new Scanner(System.in);) {
// System.out.print("Enter ten numbers: ");
// double[] numbers = new double[10];
// for (int i = 0; i < numbers.length; i++)
// numbers[i] = input.nextDouble();
//
// System.out.println("The mean is "+getMean(numbers)+
// "\nThe stand deviation is "+getStandDeviation(numbers));
// }
//8.1 10.6 9.8 9.0 9.4 9.3 10.2 11.7
double[] d = new double[8];
d[0] = new Double(8.1) ;
d[1] = new Double(10.6);
d[2] = new Double(9.8);
d[3] = new Double(9.0);
d[4] = new Double(9.4);
d[5] = new Double(9.3);
d[6] = new Double(10.2);
d[7] = new Double(11.7);
;
System.out.println("均值: "+getMean(d)+",,标准差:"+StandardDiviation(d)+",极差:"+range(d));
}
//平均值
public static double getMean(double...numbers) {
return format(getSum(numbers) / numbers.length);
}
//极差
public static double range(double[] in) {
if (in == null) {
throw new NumberFormatException();
}
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
for (int i = 0; i < in.length; i++) {
max = java.lang.Math.max(max, in[i]);
min = java.lang.Math.min(min, in[i]);
}
return format(max - min);
//return Mutil.subtract(max, min);
}
public static double format(double value) {
DecimalFormat df = new DecimalFormat("0.00");//创建一个df对象传入0.00表示构造一个保留小数点后两位的df对象
df.setRoundingMode(RoundingMode.HALF_UP);//设置规则,这里采用的也是四舍五入规则
return Double.parseDouble(df.format(value));//返回value在返回之前使用df对象的格式化方法将数据格式化
}
//方差s^2=[(x1-x)^2 +...(xn-x)^2]/n 或者s^2=[(x1-x)^2 +...(xn-x)^2]/(n-1)
public static double Variance(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 format(dVar/m);
}
//标准差σ=sqrt(s^2)
public static double StandardDiviation(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 format(java.lang.Math.sqrt(dVar/(m-1)));
//return Math.sqrt(dVar/m);
}
//前n项和
public static double getSum(double...numbers) {
double sum = 0.0;
for (double i : numbers) {
sum += i;
}
return sum;
}
}

View File

@@ -0,0 +1,67 @@
package com.cnbm.common.spc.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @Desc: ""
* @Author: caixiang
* @DATE: 2022/7/12 14:23
*/
public class DataUtils {
public static void main(String[] args) {
ArrayList<Integer> arrs = new ArrayList<>();
for(int i=0;i<100;i++){
arrs.add(i);
}
List<List<Integer>> lists = fixedGroup(arrs, 10);
System.out.println();
}
public static Date getBeforeDate(Integer number){
Date date = new Date();//获取当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");//格式化一下
Calendar calendar1 = Calendar.getInstance();//获取对日期操作的类对象
//两种写法都可以获取到前三天的日期
// calendar1.set(Calendar.DAY_OF_YEAR,calendar1.get(Calendar.DAY_OF_YEAR) -3);
//在当前时间的基础上获取前三天的日期
calendar1.add(Calendar.DATE, 0-number);
//add方法 参数也可传入 月份,获取的是前几月或后几月的日期
//calendar1.add(Calendar.MONTH, -3);
Date day = calendar1.getTime();
return day;
}
/**
* 将一组数据固定分组每组n个元素
*
* @param source 要分组的数据源
* @param limit 每组n个元素
* @param <T>
* @return
*/
public static <T> List<List<T>> fixedGroup(List<T> source, int limit) {
if (null == source || source.size() == 0 || limit <= 0)
return null;
List<List<T>> result = new ArrayList<List<T>>();
int remainder = source.size() % limit;
int size = (source.size() / limit);
for (int i = 0; i < size; i++) {
List<T> subset = null;
subset = source.subList(i * limit, (i + 1) * limit);
result.add(subset);
}
if (remainder > 0) {
List<T> subset = null;
subset = source.subList(size * limit, size * limit + remainder);
result.add(subset);
}
return result;
}
}