JAVA初学——week4 课本数组部分编程练习和综合题**(两个星号的)题

7.2 计算数字的出现次数

import java.util.Scanner;
import java.util.Arrays;
public class CountTheNumberOfOccurrencesOfTheNumber {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int maxn = 1000 ;

        int[] a = new int [maxn];
        int[] b = new int [maxn];
        int x;
        int k=0;
        for(int i = 0;i <= 100;++ i){
            b[i] = 0;
        }
        System.out.print("Enter the integers between 1 and 100 :");
        for(int i = 0;; ++ i){
            x = input.nextInt();
            if(x==0){
                break;
            }
            if(b[x]==0){
                a[k]=x;
                k++;
            }
            b[x]++;
        }
        Arrays.sort(a,0,k);
        for(int i=0;i<k;++i){
            if(b[a[i]]>1){
                System.out.println(a[i]+" occurs "+b[a[i]]+" time");
            }else{
                System.out.println(a[i]+" occurs "+b[a[i]]+" time");
            }
        }
    }
}

7.5打印不同的数

import java.util.Scanner;
import java.util.Arrays;
public class PrintDifferentNumbers {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter ten numbers :");
        int maxn = 100000;
        int num = 0;
        int[] a = new int[maxn];
        int[] b = new int[maxn];
        for(int i = 0;i < maxn; ++ i){
            b[i] = 0;
        }
        for(int i = 0;i < 10; ++ i){
            int x = input.nextInt();
            if(b[x]==0){
                a[num] = x;
                num ++;
                b[x] ++;
            }
        }
        System.out.println("The number of distinct number is "+num);
        System.out.print("The distinct numbers are : ");
        for(int i = 0;i < num;++ i){
            System.out.print(a[i]);
            if(i<num) System.out.print(" ");
        }
    }
}

7.17对学生排序

import java.util.Scanner;
import java.util.Arrays;
public class SortStudents {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入学生个数: ");
        int n = input.nextInt();
        int maxn = 1000;
        String[] name = new String[maxn];
        int[] cj = new int[maxn];
        for(int i = 0;i < n;++ i){
            System.out.print("学生姓名和成绩:");
            name[i] = input.next();
            cj[i] = input.nextInt();
        }
        for(int i = 0;i < n - 1; ++ i){
            for(int j = 0;j < n - i;++j){
                if(cj[j] < cj[j+1]){
                    int tool = cj[j+1];
                    cj[j+1] = cj[j];
                    cj[j] = tool;
                    String tool2 = name[j+1];
                    name[j+1] = name[j];
                    name[j] = tool2;
                }
            }
        }
        for(int i = 0;i < n; ++ i){
            System.out.println("第"+(i+1)+"名"+name[i]+"得了"+cj[i]+"分");
        }
    }
}

7.18冒泡排序

import java.util.Scanner;
import java.util.Arrays;
public class BubbleSort {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int maxn = 10000;
        double[] a = new double[maxn];
        System.out.print("输入数字:");
        for(int i = 0;i < 10;++i){
            a[i] = input.nextDouble();
        }
        for(int i = 0;i < 10 - 1; ++ i){
            for(int j = 0;j < 10 - i - 1;++j){
                if(a[j] > a[j+1]){
                    double tool = a[j+1];
                    a[j+1] = a[j];
                    a[j] = tool;
                }
            }
        }
        for(int i = 0;i < 10;++i){
            System.out.print(a[i]);
            if(i<10){
                System.out.print(" ");
            }
        }
    }
}

7.19 是否排好序了 ?

import java.util.Arrays;
import java.util.Scanner;
public class IsItInOrder {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter list : ");
        int maxn = 10000;
        int[] a = new int[maxn];
        int n = input.nextInt();
        for(int i = 0;i < n;++ i){
            a[i] = input.nextInt();
        }
        boolean flag = true;
        for(int i = 0;i < n-1;++i){
            if(a[i] > a[i+1]){
                flag = false;
                break;
            }
        }
        if(flag){
            System.out.println("The list is already sorted");
        }else{
            System.out.println("The list is not sorted");
        }
    }
}

7.23 游戏 : 储物柜难題

import java.util.Scanner;
import java.util.Arrays;
public class GameLlockerProblem {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int maxn = 10000;
        int[] a = new int[maxn];
        for(int i = 1;i <= 100;++i){
            a[i] = -1;
        }
        for(int i = 1;i <= 100;++i){
            for(int j = i;j <=100;j = j + i){
                a[j] = a[j] *(-1);
            }
        }
        System.out.print("打开的柜子有:");
        for(int i = 1;i <= 100;++i){
            if(a[i] == 1){
                System.out.print(i+" ");
            }
        }
    }
}

7.24优惠券收集人网題

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class CouponCollector {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int maxn = 10000;
        String[] hs = new String[maxn];
        int[] tool = new int[maxn];
        String[] sz = new String[maxn];
        for(int i = 0;i < 4;++ i){
            tool[i] = 0;
        }
        for(int i = 0;i < 4;++ i){
            if(i==0) hs[i] = "Spades";
            else if(i==1) hs[i] = "Clubs";
            else if(i==2) hs[i] = "Hearts";
            else if(i==3) hs[i] = "Diamonds";
        }
        for(int i = 1;i<=13;++i){
            if(i == 1) sz[i] = "A";
            else if(i==2) sz[i] = "2";
            else if(i==3) sz[i] = "3";
            else if(i==4) sz[i] = "4";
            else if(i==5) sz[i] = "5";
            else if(i==6) sz[i] = "6";
            else if(i==7) sz[i] = "7";
            else if(i==8) sz[i] = "8";
            else if(i==9) sz[i] = "9";
            else if(i==10) sz[i] = "10";
            else if(i==11) sz[i] = "Jack";
            else if(i==12) sz[i] = "Queen";
            else if(i==13) sz[i] = "King";
        }
        Random r = new Random();
        int n = 0;
        int m = 0;
        while(n<4){
            int a = 1 + r.nextInt(13);
            int b = r.nextInt(4);
            if(tool[b] == 0){
                System.out.println(sz[a]+" of "+hs[b]);
                tool[b]++;
                n++;
            }
            m++;
        }
        System.out.println("Number of picks : "+m);
    }
}

7.31 合并两个有序列表

import java.util.Scanner;
import java.util.Arrays;
public class MergeTwoOrderedTables {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int maxn = 100;
        System.out.print("Enter list1 : ");
        int[] a = new  int[maxn];
        int[] b = new  int[maxn];
        a[0] = input.nextInt();
        for(int i = 1; i <= a[0] ; ++i){
            a[i] = input.nextInt();
        }
        System.out.print("Enter list2 : ");
        b[0] = input.nextInt();
        for(int i = 1;i <= b[0];++i){
            b[i] = input.nextInt();
        }
        System.out.print("The merged list is ");
        int[] c = merge(a,b);
        for(int i = 1;i <= a[0] + b[0];++i) {
            System.out.print(c[i] + " ");
        }
    }
    public static int[] merge(int[] list1,int[] list2){
        int[] s = new int[2000];
        for(int i = 1;i <= list1[0]+list2[0];++ i){
            if(i<=list1[0]) s[i] = list1[i];
            else s[i] = list2[i-list1[0]];
        }
        Arrays.sort(s,1,list1[0]+list2[0]+1);
        return s;
    }
}

7.32 划分列表

import java.util.Arrays;
import java.util.Scanner;
public class PartitionList {
    static int n;
    static int b;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter list: ");
        int maxn = 10000;
        n = input.nextInt()-1;
        int [] a = new int[maxn];
        b = input.nextInt();
        for(int i = 0;i < n;++ i){
            a[i] = input.nextInt();
        }
        System.out.print("After the partition,the list is ");
        partition(a);
    }
    public static void partition(int[] list){
        for(int i = 0;i < n;++i){
            if(list[i] <= b){
                System.out.print(list[i]+" ");
            }
        }
        System.out.print(b+" ");
        for(int i = 0;i < n;++i){
            if(list[i] > b){
                System.out.print(list[i]+" ");
            }
        }
    }
}

7.34对字符串中的字符排序

import java.util.Scanner;
import java.util.StringTokenizer;
public class SortCharactersInAString {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int maxn = 10000;
        String a = input.nextLine();
        int n = a.length();
        System.out.println(sort(a));
    }
    public static String sort(String s){
        int n = s.length();
        char[] b = s.toCharArray();
        for(int i = 0;i < n-1;++ i){
            for(int j = 0;j < n-1-i;++j){
                if(b[j] > b[j+1]){
                    char tool = b[j+1];
                    b[j+1] = b[j];
                    b[j] = tool;
                }
            }
        }
        String s1 = String.valueOf(b);
        return s1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值