一个java编程题,怎么求出数组中重复数字的出现次数,并按次数从大到下排序

2024年11月18日 20:28
有5个网友回答
网友(1):

#include  #include  #include  using namespace std; int count=0; void countNum(int a[],int start,int finish) { //cout<finish) return ; if(a[middle]==2) { count++; countNum(a,start,middle-1); countNum(a,middle+1,finish); //cout<<"get here"<2) { countNum(a,start,middle-1); }else

{

countNum(a,middle+1,finish);

}
//return count; } int main() {  int s[1000]; ifstream inf("1.txt"); int i=0;

while(!inf.eof())

{inf>>s[i];i++;} //int s[]={1,2,2,2,3}; int start=0;//,finish=NUM-1; countNum(s,start,i-1); cout<

扩展资料

#include

template

voidinsertion_sort(biIterbegin,biIterend)

{

typedeftypenamestd::iterator_traits::value_typevalue_type;

biIterbond=begin;

std::advance(bond,1);

for(;bond!=end;std::advance(bond,1)){

value_typekey=*bond;

biIterins=bond;

biIterpre=ins;

std::advance(pre,-1);

while(ins!=begin&&*pre>key){

*ins=*pre;

std::advance(ins,-1);

std::advance(pre,-1);

}

*ins=key;

}

}

参考资料:百度百科 排序

网友(2):

public static void main(String[] args) {
int array[]={5, 10, 10, 5, 2, 5, 3, 5, 10, 5, 2, 5, 5, 10, 1, 5, 1};
Arrays.sort(array);// 给数组排序
int count=0;
int tmp=array[0];
Map map=new HashMap();
for(int i=0; i < array.length; i++) {
if(tmp != array[i]) {
tmp=array[i];
count=1;
} else {
count++;
}
map.put(array[i], count);
}
map=sortByValue(map);
Set key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
Integer s = (Integer) it.next();
System.out.println(s+"出现了"+map.get(s));
}
}

public static Map sortByValue(Map map) {
List list=new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
// 将链表按照值得从小到大进行排序
public int compare(Object o1, Object o2) {
return ((Comparable)((Map.Entry)(o2)).getValue()).compareTo(((Map.Entry)(o1)).getValue());
}
});
Map result=new LinkedHashMap();
for(Iterator it=list.iterator(); it.hasNext();) {
Map.Entry entry=(Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}

网友(3):

/***求出一个数组中出现次数最多的数,如果有多个数出现次数相同,一同列出 */import java.util.TreeMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.Arrays;class ArrayLookup{public void getMaxCount(int[] iArr){int i = 0; //临时变量 int j = 0; //临时变量int count = 1; //记录相同数字出现次数int temp = 0; //记录数组中相同元素的值int max = 0; //记录出现次数最多的数 //数组排序Arrays.sort(iArr);TreeMap tm = new TreeMap();//将数字与其出现次数存入Map集合while(i> set = tm.entrySet();Iterator> it = set.iterator();while(it.hasNext()){Map.Entry me = it.next();System.out.println(me.getKey() + "出现 " + me.getValue() + " 次");}}}public class ArrayLookupMain {private int count;public static void main(String[] args){ int[] intArray = {2,3,4,2,3,4};//,2,2,2,2,5,2,12,6,8,4,3,7,7,5,3,2,5,8,9,8,8,4,5,4,6,8,9,0,5,3,3,2,4,4,7,8,5,34,5,6,5,3,34,6,7,8,9,0};ArrayLookup arr = new ArrayLookup();arr.getMaxCount(intArray); } }

网友(4):

for(i=0;i system.out.println("");
}

网友(5):

用增强FOR循环