5.1
import java.util.Scanner;
class Demo05_01{
public static void main(String[] args){
int[] arr=new int[101];
Scanner scanner=new Scanner(System.in);
System.out.print("Enter numbers:");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
arr[num]++;
}
for(int i=0;i<arr.length;i++){
if(arr[i]!=0){
System.out.println(i+" occurs "+arr[i]+(arr[i]>1?" times":" time"));
}
}
}
}
5.2
import java.util.*;
class Demo05_02{
public static void main(String[] args){
func2();
}
public static void func2(){
int[] arr=new int[0];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter numbers:");
for(int i=0;i<10;i++){
int num=scanner.nextInt();
if(!contains(arr,num)){
arr=copyOf(arr,arr.length+1);
arr[arr.length-1]=num;
}
}
System.out.println(Arrays.toString(arr));
}
public static boolean contains(int[] arr,int key){
for(int i=0;i<arr.length;i++){
if(arr[i]==key){
return true;
}
}
return false;
}
public static int[] copyOf(int[] arr,int newLen){
int[] newArr=new int[newLen];
for(int i=0;i<arr.length;i++){
newArr[i]=arr[i];
}
return newArr;
}
}
5.3
import java.util.Scanner;
class Demo05_03{
public static void main(String[] args) {
System.out.print("Enter a list:");
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
if (isSorted(arr)) {
System.out.print("The list is already sorted");
}
else {
System.out.print("The list is not sorted");
}
}
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i-1] > arr[i]) {
return false;
}
}
return true;
}
}
5.4
import java.util.*;
class Demo05_04{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the number of balls to drop:");
int balls=scanner.nextInt();
System.out.print("Enter the number of slots in the bean machine:");
int slots=scanner.nextInt();
int[] arr=new int[slots];
for(int i=0;i<balls;i++){
String path=getPath(slots);
System.out.println(path);
arr[getR(path)]++;
}
System.out.println(Arrays.toString(arr));
show(arr);
}
public static void show(int[] arr){
int w=arr.length;
int h=0;
for(int i=0;i<arr.length;i++){
if(arr[i]>h){
h=arr[i];
}
}
for(int i=h-1;i>=0;i--){
for(int j=0;j<w;j++){
if(i<arr[j]){
System.out.print("O");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
public static int getR(String path){
int count=0;
for(int i=0;i<path.length();i++){
if(path.charAt(i)=='R'){
count++;
}
}
return count;
}
public static String getPath(int slots){
Random random=new Random();
String path="";
for(int j=0;j<slots-1;j++){
if(random.nextInt(2)==0){
path+="L";
}else{
path+="R";
}
}
return path;
}
}
5.5
import java.util.Scanner;
class Demo05_05{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] list1=new int[0];
int[] list2=new int[0];
System.out.print("Enter list1(End with 0): ");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
list1=copyOf(list1,list1.length+1);
list1[list1.length-1]=num;
}
System.out.print("Enter list2(End with 0): ");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
list2=copyOf(list2,list2.length+1);
list2[list2.length-1]=num;
}
System.out.println(equals(list1,list2));
}
public static boolean equals(int[] list1,int[] list2){
if(list1.length!=list2.length){
return false;
}
for(int i=0;i<list1.length;i++){
if(list1[i]!=list2[i]){
return false;
}
}
return true;
}
public static int[] copyOf(int[] arr,int newLen){
int[] newArr=new int[newLen];
for(int i=0;i<arr.length;i++){
newArr[i]=arr[i];
}
return newArr;
}
}
5.6
import java.util.Scanner;
class Demo05_06{
public static void main(String[] args){
int[] arr = new int[0];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of values: ");
int n=scanner.nextInt();
System.out.print("Enter the values: ");
while(n>0){
int num=scanner.nextInt();
if(num==0){
break;
}
arr=copyOf(arr,arr.length+1);
arr[arr.length-1]=num;
n--;
}
System.out.print(fourDigit(arr));
}
public static boolean fourDigit(int[] arr) {
for(int i=0;i<arr.length;){
int count=1;
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j]){
count++;
}else{
break;
}
}
if(count>=4){
return true;
}
i+=count;
}
return false;
}
public static int[] copyOf(int[] arr,int newLen){
int[] newArr=new int[newLen];
for(int i=0;i<arr.length;i++){
newArr[i]=arr[i];
}
return newArr;
}
}
5.7
import java.util.*;
class Demo05_07{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] list1=new int[0];
int[] list2=new int[0];
System.out.print("Enter list1(End with 0): ");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
list1=copyOf(list1,list1.length+1);
list1[list1.length-1]=num;
}
System.out.print("Enter list2(End with 0): ");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
list2=copyOf(list2,list2.length+1);
list2[list2.length-1]=num;
}
System.out.println(Arrays.toString(merge(list1,list2)));
}
public static int[] merge(int[] list1,int[] list2){
if(list1==null&&list2==null){
return null;
}
if(list1==null){
return list2;
}
if(list2==null){
return list1;
}
int[] list3=new int[list1.length+list2.length];
int p1=0;
int p2=0;
int p3=0;
while(true){
if(p1==list1.length&&p2==list2.length){
break;
}
if(p1<list1.length&&p2==list2.length){
list3[p3++]=list1[p1++];
}else if(p1==list1.length&&p2<list2.length){
list3[p3++]=list2[p2++];
}else{
if(list1[p1]<=list2[p2]){
list3[p3++]=list1[p1++];
}else{
list3[p3++]=list2[p2++];
}
}
}
return list3;
}
public static int[] copyOf(int[] arr,int newLen){
int[] newArr=new int[newLen];
for(int i=0;i<arr.length;i++){
newArr[i]=arr[i];
}
return newArr;
}
}
5.8
import java.util.*;
class Demo05_08{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
Random random=new Random();
String[] words={"naruto","kakashi","sasuke","banana","java","program"};
while(true){
String word=words[random.nextInt(words.length)];
boolean[] status=new boolean[word.length()];
int miss=0;
while(true){
String ciphertext=getCipherText(word,status);
System.out.print("Enter a letter in word "+ciphertext+" >");
char letter=scanner.nextLine().charAt(0);
if(isContainsLetter(word,letter)){
if(!changeWordStatus(word,status,letter)){
System.out.println("\t "+letter+" is already in the word");
}
}else{
System.out.println("\t "+letter+" is not in the word");
miss++;
}
if(isFinish(status)){
System.out.println("The word is "+word+". You miss "+miss+" time");
break;
}
}
System.out.print("Do you want to guess another word?Enter y or n:");
String choice=scanner.nextLine();
if(choice.equals("n")){
System.out.println("Welcome!Thank you! FUCK PROGRAM!");
break;
}
}
}
public static boolean isFinish(boolean[] status){
for(int i=0;i<status.length;i++){
if(!status[i]){
return false;
}
}
return true;
}
public static boolean changeWordStatus(String word,boolean[] status,char letter){
for(int i=0;i<word.length();i++){
if(word.charAt(i)==letter){
if(status[i]){
return false;
}else{
status[i]=true;
}
}
}
return true;
}
public static boolean isContainsLetter(String word,char letter){
for(int i=0;i<word.length();i++){
if(word.charAt(i)==letter){
return true;
}
}
return false;
}
public static String getCipherText(String word,boolean[] status){
String ciphertext="";
for(int i=0;i<status.length;i++){
if(status[i]){
ciphertext+=word.charAt(i);
}else{
ciphertext+="*";
}
}
return ciphertext;
}
}
5.9
import java.util.*;
class Demo05_09{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("Enter numbers:");
double[][] matrix=new double[3][4];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
matrix[i][j]=scanner.nextDouble();
}
}
for(int col=0;col<matrix[0].length;col++){
System.out.println(sumColumn(matrix,col));
}
}
public static double sumColumn(double[][] m,int col){
double sum=0;
for(int row=0;row<m.length;row++){
sum+=m[row][col];
}
return sum;
}
}
5.10
class Demo05_10{
public static void main(String[] args){
int[][] m={
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10,11,12},
{13,14,15,16}
};
int sum=0;
for(int i=0;i<m.length;i++){
sum+=m[i][i];
}
System.out.println(sum);
int subsum=0;
for(int i=0;i<m.length;i++){
subsum+=m[i][m.length-1-i];
}
System.out.println(subsum);
}
}
5.11
class Demo05_11{
public static void main(String[] args){
double[][] A={
{1,2,3},
{4,5,6},
{7,8,9}
};
double[][] B={
{0,2.0,4.0},
{1,4.5,2.2},
{1.1,4.3,5.2}
};
double[][] C=new double[A.length][B[0].length];
for(int i=0;i<C.length;i++){
for(int j=0;j<C[i].length;j++){
double sum=0;
for(int k=0;k<B.length;k++){
sum+=A[i][k]*B[k][j];
}
C[i][j]=sum;
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
}
5.12
import java.util.*;
class Demo05_12{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter size:");
int size=scanner.nextInt();
int[][] m=new int[size][size];
Random random=new Random();
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
m[i][j]=random.nextInt(2);
System.out.print(m[i][j]+" ");
}
System.out.println();
}
checkRow(m);
checkCol(m);
checkDiagonal(m);
checkSubDiagonal(m);
}
public static void checkSubDiagonal(int[][] m){
int sum=0;
for(int i=0;i<m.length;i++){
sum+=m[i][m.length-1-i];
}
if(sum==m.length||sum==0){
System.out.printf("副主对角线全相等且是%d\n",sum==0?0:1);
}
}
public static void checkDiagonal(int[][] m){
int sum=0;
for(int i=0;i<m.length;i++){
sum+=m[i][i];
}
if(sum==m.length||sum==0){
System.out.printf("主对角线全相等且是%d\n",sum==0?0:1);
}
}
public static void checkRow(int[][] m){
for(int i=0;i<m.length;i++){
int sum=0;
for(int j=0;j<m[i].length;j++){
sum+=m[i][j];
}
if(sum==m.length||sum==0){
System.out.printf("第%d行全相等且是%d\n",i+1,sum==0?0:1);
}
}
}
public static void checkCol(int[][] m){
for(int j=0;j<m.length;j++){
int sum=0;
for(int i=0;i<m.length;i++){
sum+=m[i][j];
}
if(sum==m.length||sum==0){
System.out.printf("第%d列全相等且是%d\n",j+1,sum==0?0:1);
}
}
}
}
5.13
import java.util.*;
class Demo05_13{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter row ,col:");
int row=scanner.nextInt();
int col=scanner.nextInt();
int[][] m=new int[row][col];
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
m[i][j]=scanner.nextInt();
}
}
System.out.println(isConsecutiveFour(m));
}
public static boolean isConsecutiveFour(int[][] m){
for(int i=0;i<m.length;i++){
for(int j=0;j<m[i].length;j++){
if(j<=m[i].length-4){
boolean flag=true;
for(int c=j+1;c<=j+3;c++){
if(m[i][j]!=m[i][c]){
flag=false;
break;
}
}
if(flag){
return true;
}
}
if(i<=m.length-4){
boolean flag=true;
for(int r=i+1;r<=i+3;r++){
if(m[i][j]!=m[r][j]){
flag=false;
break;
}
}
if(flag){
return true;
}
}
if(i<=m.length-4&&j<=m[0].length-4){
boolean flag=true;
for(int r=i+1,c=j+1;r<=i+3;r++,c++){
if(m[i][j]!=m[r][c]){
flag=false;
break;
}
}
if(flag){
return true;
}
}
if(i>=3&&j<=m[0].length-4){
boolean flag=true;
for(int r=i-1,c=j+1;c<=j+3;r--,c++){
if(m[i][j]!=m[r][c]){
flag=false;
break;
}
}
if(flag){
return true;
}
}
}
}
return false;
}
}
5.14