package shuzu_practice;
public class Practice3 {
/*
需求3:
有如下数组: {1,2,3,3,4,4,4,5,5,5,5,6,6,6,6,6}
统计数组中每个元素出现的数量
打印格式要求如下: 不能重复打印元素
数字1在数组中出现了1次
数字2在数组中出现了1次
数字3在数组中出现了2次
数字4在数组中出现了3次
数字5在数组中出现了4次
数字6在数组中出现了5次
不允许使用集合
*/
static int index = 0;
public static void main(String[] args) {
int[] oldArr = {1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6};
int[] quchong = quchong(oldArr);
for (int i = 0; i < index; i++) {
int count = 0;
for (int j = 0; j < oldArr.length; j++) {
if (oldArr[j] == quchong[i]) {
count++;
}
}
System.out.println("数字" + quchong[i] + "在数组中出现了" + count + "次");
}
}
public static int[] quchong(int[] oldArr) {
int[] newArr = new int[oldArr.length];
newArr[0] = oldArr[0];
index = 1;//定义新数组的长度
for (int i = 1; i < oldArr.length; i++) {
boolean l = false;
for (int j = 0; j < index; j++) {
if (oldArr[i] == newArr[j]) {
l = true;
}
}
if (!l) {
newArr[index] = oldArr[i];
index++;
}
}
return newArr;
}
}
数组算法小题三
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
面试最常见算法3—数组
面试常见算法—数组篇
算法 面试常见算法 数组 矩阵 JAVA -
指针趣味小题
这小段代码帮助理解下指针和对象的大小,& 和*的作用#include <stdio.h
#include ios #define -
C语言小题1
1.有一组特殊的数列,其定义如下
输入数据为一个数组的下标