0313星期天,翻了翻課本,把很早就布置的作業(但下周收)寫了,
出于個人感覺的難度排序,我先寫的作業四,然后是1-2-3,代碼風格是有漸變的,比如作業四中還在使用類似C++(非迭代器的)的回圈結構,作業一中就學會了使用for (String i : strarr)這樣的回圈,后續學了一些Hashmap以及ArrayList這種高級結構,
這個系列3比1先發,這完全符合實際情況??,
00 作業一
00-1 題目
1 (計算字串中單詞的出現次數)撰寫程式,從控制臺讀取100 個字串,然后計算每個 字串中單詞的出現的次數,
例如:
從控制輸入:
“hello Java”
“hello world”
….
輸出結果為:
hello 2
Java 1
World 1
00-2 解決
這道題,記得一年多以前,拿C語言做,是比較麻煩的,需要手動遍歷、以“ ”分割,然后窮舉查找次數,然后想辦法把查找結果輸出,
但對于C++和Java而言,就比較方便,因為提供了一些比較高級的封裝結構(C語言可能也有),比如HashMap和Interator,
先練習一下字串的一些操作:
String[] str = new String[]{"a b", "bc cd", "c e"};
String arr = String.join(" ", str);
String arr1[] = arr.split(" ");
for(int i = 0; i < arr1.length;i++){
System.out.print(arr1[i] + " ");
}
代碼:
補充:0314講了點例外處理,于是用了一用,也不知道合不合適,
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package homework1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
*
* @author zzrs123
*/
public class Homework1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = 3;//檢驗方便,這里用3方便,
//輸入100個字串
String strin;
String strarr[];
HashMap<String, Integer> map = new HashMap<String, Integer>();
while (n > 0) {
strin = br.readLine();
//讀入一行
strarr = strin.split(" ");
//利用空格分隔得到字串陣列
for (String i : strarr) {
if (!map.containsKey(i)){
map.put(i, 1);
}
//字典里沒有這個字串的話,就把它放進去
else{
map.put(i, map.get(i) + 1);
}
//如果有,就把鍵值+1
}
n--;
//繼續輸入
//這個思路其實要比把全部的字串輸入,放進一個字串陣列再處理好,自己最近總是有這個傾向,但并不總是要放進陣列才行,
}
Iterator<String> iterate = map.keySet().iterator();
//學過C++知道這是迭代器
while (iterate.hasNext()) {
strin = iterate.next();
System.out.println(strin + " " + map.get(strin));
}
}
}
01 作業二
01-1 題目
2 撰寫一個程式,使用隨機函式 random 產生1000個[0-99]的整數,
這些整數有可能可能存在重復,程式對這些資料進行過濾后,去掉重復出現的數,排序并計算整數的個數,
例如,隨機產生的資料為:1,10,43,10,2,1,98 輸出結果為:1,2,10,43,98 資料個數為 5
01-2 解決
這個題我想應用一下類似于C++中的vector之類的結構,如果在哈希表里沒有,就把放入動態陣列等待后續的排序和輸出,Java中的動態陣列有ArrayList,可以據此實作陣列動態擴容以及排序,下面我就使用到了這些內容:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package homework2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author zzrs123
*/
public class Homework2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//先產生1000個亂數從0到99
//1000太大了,試試500
Random num = new Random(500);
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0; i < 500; i++){
set.add(num.nextInt(100));
}
ArrayList<Integer> list1 = new ArrayList<Integer>();
for(int i : set){
list1.add(i);
}
//Collections.sort(list1);
list1.sort(Comparator.naturalOrder());
//兩種排序方式
//System.out.println(list1);
System.out.println("輸出結果為:" + list1);
System.out.println("資料個數:" + list1.size());
}
}
02 作業三
02-1 題目
3 豆機游戲,也稱為梅花瓶或者高爾頓瓶,它是一個用來制作統計實驗的設備,是用英國科 學家賽弗蘭克斯高爾頓的名字來命名的,它是一個三角形的均勻放置釘子(或鉤子)的直立 板子,如下圖所示(每個球都會選擇一個隨機路徑,然后進入一個槽中),
撰寫程式模擬豆機,程式應該提示用戶輸入球的個數以及機器的槽數,列印每個球的路 徑模擬它的下落,例如,在圖(a)中的球的路徑是 LLRRLLR,而在圖(c)中的球的路徑是 RLRRLRR,使用條形圖顯示槽中球的最終儲備量,下面是一個運行實體,
Enter the number of balla to drop:5
Enter the number of slots in the bean machine:8 (回車)
LRLRLRR 4
RRLLLRR 4
LLRLLRR 3
RRLLLLL 2
LRLRRLR 4
輸出:
0
0
0 0 0

02-2 解決
這道題看上去很難,但實際上思路很多,覺得無處下手只是覺得方法不好,暴力思路也不是不行(笑),
思路參考地址
這里我的解決方式其實跟老師的示例不太一樣,看示例應當要求的是輸出字串,但自己輸出的是vector<char>陣列,這點有待改善,
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package homework3;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Vector;
/**
*
* @author zzrs123
*/
public class Homework3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of balla to drop: ");
int num_Balls = input.nextInt();
System.out.print("Enter the number of slots in the bean machine:");
int num_slots= input.nextInt();
int[] slots = new int[num_slots];
//接下來需要根據亂數生成RL路徑
for(int i = 0; i < num_Balls; i++){
char RL;
if(Math.random() >= 0.5){
RL = 'R';
}
else{
RL = 'L';
}
//下面生成RL路徑,三種辦法
//這里利用字串拼接符號‘+’,化簡一下字符陣列的遍歷生成
//或者可以利用vector:
//ArrayList只能用于參考型別如String;
//同時統計一下R的數量
// String RLroad = "";
Vector charlist = new Vector();
int Rcount = 0;
for(int n: slots){
RL = Math.random() >= 0.5 ? 'R' : 'L';
charlist.add(RL);
if(RL == 'R'){
Rcount++;
}
}
System.out.println(charlist);//事實上輸出的是一個個字符的陣列
//轉換成字串
//找了好多轉換辦法都不行(在下面注釋著),只能遍歷了
for (Object target : charlist) {
System.out.print(target);
}
System.out.println();
//String target_str = new String(charlist);這樣寫不行,
// char temp[] = new char[num_slots];
// charlist.copyInto(temp);
// String target_str = String.valueOf(temp);
// System.out.println(target_str);
slots[Rcount]++;
// //char []RLroad = new char[nums_slots];
}
//畫圖需要從上往下畫,所以需要確定高度,即有槽中球的最大值,
int max = max(slots);
//畫
draw(max, slots);
}
/*槽里最多多少個球*/
public static int max(int[] slots) {
int max = 0;
for(int i = 0; i < slots.length; i++) {
if(slots[i] > max)
max = slots[i];
}
return max;
}
/*列印條形圖*/
public static void draw(int max, int[] slots) {
for(int i = max; i > 0; i--) {
for(int k = 0; k < slots.length; k++) {
if(slots[k] == i) {
System.out.print(" 0 ");
slots[k]--;
}
else
System.out.print(" ");
}
System.out.print(" ");
System.out.println("");
}
}
}
03 作業四
03-1 題目
4(最大行和最大列)撰寫一個程式,在一個 4*$的矩陣中資料填入0和1,列印該矩陣,找到第一個具有最多1的行和列,
03-2 解決
這個題目是我java實操的第一道題,這個代碼風格整個還是很像C的,思路很簡單,想辦法輸入4×4陣列,想辦法輸出這個陣列,然后分別就行列遍歷,找到1最多的行列,
不過有兩個容易犯的錯誤:
- 在行列遍歷找到1最多的行列時,完成一次內部遍歷時,每次的1的count都得賦0,再進入下一次遍歷;
- 找到一個比max還大的count,得把max更新,我寫的時候就卡在這里,輸出結果總是最后一行和列,推斷出最大值沒有更新,
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package homework4;
import java.util.Scanner;
/**
*
* @author zzrs123
*/
public class Homework4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in =new Scanner(System.in);
int arr[][] = new int [4][4];
for(int i = 0;i < 4 ;i++){
for(int j = 0; j < 4; j++){
arr[i][j]=in.nextInt();
}
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
System.out.print(arr[i][j]+" ");
//System.out.print(' ');
}
System.out.println();
}
int max=0;//行列最大值
int count = 0,num =0 ;//計數和行列標志
for(int i = 0; i < 4; i++){
count = 0;
for(int j = 0; j < 4; j++){
if(arr[i][j]==1)count++;
}
if(count>max){
max = count;
num = i;
}
}
System.out.println(num);
num = 0;
//System.out.println(tag);
max = 0;
for(int i = 0; i < 4; i++){
count = 0;
for(int j = 0; j < 4; j++){
if(arr[j][i]==1)count++;
}
if(count>max){
max = count;
num = i;
}
}
System.out.println(num);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443443.html
標籤:Java
