who knows the truth。
Fork me on GitHub
返回顶部

java数组练习

JAVA程序设计

1.

1

 (计算字符串中单词的出现次数)编写程序,从控制台读取 100 个字符串,然后计算每 

个字符串中单词的出现的次数。 

例如:从控制输入“hello Java” 

“hello world” 

…. 

输出结果为:hello 2 

Java 

1 

World 

1

分析:

有100个字符串,每个字符串有若干个单词,这里以空格分隔。

首先可以想到利用split(python也有,多半多数程序设计语言都有)方法将字符串分隔得到数组。

再利用arraylist或Map等与迭代器遍历每个字符串分割得到的数组

计数利用arraylist或map的方法

关于Arraylist

关于map接口

此外也有hashmap

集合框架与迭代器

(当实际写的时候,我发现还是得用map,自带value计数)

image-20210311221514948

image-20210311221030844

image-20210311221606429

参考

总结:hashmap+iterator真香

package count;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;

public class count {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = 100;//输入100个字符串
		String s;
		String str_list[];
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		while (n > 0) {
			s = br.readLine();//读入一行
			str_list = s.split(" ");//利用空格分隔得到字符串数组
			for (String i : str_list) {
				if (!map.containsKey(i)) {
					map.put(i, 1);
				} else {
					map.put(i, map.get(i) + 1);
				}
			}
			n--;
		}
		Iterator<String> iter = map.keySet().iterator();
		while (iter.hasNext()) {
			s = iter.next();
			System.out.println(s + " " + map.get(s));
		}

	}
}

2.编写一个程序,使用随机函数 random 产生 1000 个【0-99】的整数,这些整数有可能可 

能存在重复,程序对这些数据进行过滤后,去掉重复出现的数,排序并计算整数的个数。 

例如,随机产生的数据为:1,10,43,10,2,1,98 

输出结果为:1,2,10,43,98 

数据个数为 5

分析:如果用第一题的hashmap可以做,出现n次设定对应的value为n。value大于1的只输出一次。

或者可以用arraylist或者hashset。hashset只会记录一次数据,但不能排序。

可以将set转为list后排序。

public class test {
	public static void main(String[] args) {
		Random ran = new Random(10);
//		ArrayList<Integer> list = new ArrayList<Integer>();
//		for (int i = 0; i < 1000; i++) {
//			list.add(ran.nextInt(100));
//		}
//		for (int i = 0; i < list.size(); i++) {
//			for (int j = 0; j < list.size(); j++) {
//				if ((list.get(i) == list.get(j)) && (i != j)) {
//					list.remove(j);
//					j--;
//				}
//			}
//		}
//		list.sort(Comparator.naturalOrder());
//		System.out.println(list);
//		System.out.println(list.size());
		HashSet<Integer> set = new HashSet<Integer>();
		for (int i = 0; i < 1000; i++) {
			set.add(ran.nextInt(100));
		}
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (int i : set) {
			list.add(i);
		}
		Collections.sort(list);
		System.out.println("输出结果为:"+list);
		System.out.println("数据个数 "+list.size());
	}

}
3 豆机游戏,也称为梅花瓶或者高尔顿瓶,它是一个用来制作统计实验的设备,是用英国科 

学家赛弗兰克斯高尔顿的名字来命名的。它是一个三角形的均匀放置钉子(或钩子)的直立 

板子,如下图所示(每个球都会选择一个随机路径,然后进入一个槽中)。 

编写程序模拟豆机。程序应该提示用户输入球的个数以及机器的槽数。打印每个球的路 

径模拟它的下落。例如,在图(a)中的球的路径是 LLRRLLR。而在图(c)中的球的路径是 

RLRRLRR。使用条形图显示槽中球的最终储备量。下面是一个运行实例。 

Enter the number of balls to drop:5 

Enter the number of slots in the bean machine:8 (回车) 

LRLRLRR 

RRLLLRR 

LLRLLRR 

RRLLLLL 

LRLRRLR 

0

0 

0 0 0

分析:

20210312134055

一开始做的时候,不知道如何根据路径求出下落的位置。

参考

这个也不错

分析如图,利用随机数确定每次下落的路径,L为左,R为右。

层数=槽数-1

若R的个数为n,则落到第n+1个槽口

public class test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of balls to drop:\n");
		int num_ball = input.nextInt();// 球的个数
		System.out.print("Enter the number of slots in the bean machine:\n");
		int slots = input.nextInt();// 槽口数
		input.close();
		int slot[] = new int[slots];// 记录每个槽口的球的个数
		int cent;
		for (int i = 0; i < num_ball; i++) {
			cent = 0;
			for (int j = 0; j < slots - 1; j++) {
				if (Math.random() < 0.5)
					System.out.print('L');
				else {
					System.out.print('R');
					cent++;
				}
			}
			++slot[cent];
			System.out.println();
		}
		int max = slot[0];
		for (int i = 0; i < slots; i++) {
			if (slot[i] > max)
				max = slot[i];
		}
		for (int i = 0; i < max; i++) {
			for (int j = 0; j < slots; j++) {
				if (slot[j] >= max - i)
					System.out.print(0);
			}
			System.out.println();
		}
	}
}
4(最大行和最大列)编写一个程序,在一个 4*$的矩阵中数据填入 0 和 1,打印该矩阵,找 到第一个具有最多 1 的行和列。

分析:二维数组

对每行与每列分析就行

public class test {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int matrix[][] = new int[4][4];
		for (int i = 0; i < 4; i++) {
			System.out.println("输入第" + (i + 1) + "行数据");
			for (int j = 0; j < 4; j++) {
				matrix[i][j] = scanner.nextInt();
			}
		}
		scanner.close();
		for (int i[] : matrix) {
			for (int num : i) {
				System.out.print(num + " ");
			}
			System.out.println();
		}
		int max_row = 0, max_col = 0;
		int c_max_count;
		int r_count, c_count, r_max_count = c_max_count = 0;
		for (int i = 0; i < 4; i++) {
			c_count = r_count = 0;
			for (int j = 0; j < 4; j++) {
				if (matrix[i][j] == 1)
					r_count++;
				if (matrix[j][i] == 1)
					c_count++;
			}
			if (r_count > r_max_count) {
				r_max_count = r_count;
				max_row = i;
			}
			if (c_count > c_max_count) {
				c_max_count = c_count;
				max_col = i;
			}
		}
		System.out.println("最大行为:" + (max_row + 1));
		System.out.println("最大列为:" + (max_col + 1));
	}

}
posted @ 2021-03-12 13:30  no_sense  阅读(295)  评论(0编辑  收藏  举报
欢迎来到kanashimi的博客
pic
kanashimi
真理将引领我们