日期:2019.6.3
博客期:073
星期日
或许你在想求众数的基本方法:
遍历一遍找到每个数的出现次数,之后找到次数最多的那个!
经过我们的分析,可知这个实际上是在遍历过程中,找到次数最多的数!那我们就可以采取记录次数的方法!
1 package test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import my.javabean.basic.*; 7 import my.javabean.file.FileToDealWithJavaBean; 8 import my.javabean.sql.*; 9 import my.javabean.sqlpackage.MySqlPackage; 10 11 @SuppressWarnings("unused") 12 public class TestForMain { 13 public static void main(String[] args) { 14 MySqlPackage msp = FileToDealWithJavaBean.getToMySql("txt/tiezi.javabean"); 15 MySqlForReUnion msfru = msp.getMySql(); 16 List <JavaBean> ljb = msfru.GetFromSQL(); 17 int size = ljb.size(); 18 19 /* 20 int a[] = new int [100]; 21 for(int i=0;i<100;++i) 22 a[i] = 0; 23 for(int i=0;i<size;++i) 24 { 25 int answer = (Integer)ljb.get(i).javabean.get(1); 26 ++a[answer]; 27 } 28 int point = 0; 29 for(int i=0;i<100;++i) 30 if(a[point]<a[i]) 31 point = i; 32 System.out.println(point); 33 */ 34 /* 35 int point = 1; 36 int save = (Integer)ljb.get(0).javabean.get(1);; 37 for(int i=1;i<size;++i) 38 { 39 int answer = (Integer)ljb.get(i).javabean.get(1); 40 if(answer==save) 41 ++point; 42 else 43 { 44 --point; 45 } 46 if(point<=0) 47 { 48 point = 1; 49 answer = 1; 50 } 51 } 52 53 System.out.println(save); 54 */ 55 /* 56 double sum = 0; 57 for(int i=0;i<size;++i) 58 sum += (Integer)ljb.get(i).javabean.get(1); 59 sum = sum / size; 60 61 int left = (int)sum; 62 63 int right = left+1; 64 65 if(sum-left>right-sum) 66 System.out.println(right); 67 else 68 System.out.println(left); 69 */ 70 TestOnly(); 71 msfru.free(); 72 } 73 }
原文地址:https://www.cnblogs.com/onepersonwholive/p/10967073.html
时间: 2024-10-09 04:29:16