土办法不失为一种好办法
至少对简单的程序来说

Task 1: Histogram Printer

Histogram is a commonly used graphical representation of the distribution of numerical data in statistics. Given a series of numbers, write a complete computer program to print the histogram of the occurrences of each digit to the screen. For example, given a series of numbers:
1022473578476949426110832338899437170338449409627474102214022150928
Expected Outputs: (Separate each column using two empty spaces)

//package lab8;


public class task1 {
    public static void main(String[] args) {

        String input = "1022473578476949426110832338899437170338449409627474102214022150928";
        int hang=11,lie=10;
        int n0=0,n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0;
        for(int i =0;i<=input.length()-1;i++) {
            switch (input.charAt(i)) {
            case '0':n0++;break;
            case '1':n1++;break;
            case '2':n2++;break;
            case '3':n3++;break;
            case '4':n4++;break;
            case '5':n5++;break;
            case '6':n6++;break;
            case '7':n7++;break;
            case '8':n8++;break;
            case '9':n9++;break;
            default:
                break;
            }

            }
        for(int m =0;m<=hang-1;m++) {
            for(int n =0;n<=lie-1;n++) {
                if(m>=hang-n0&&n==0) {
                    System.out.print("[0] ");
                }else if(n==0) {
                    System.out.print("    ");
                }
                if(m>=hang-n1&&n==1) {
                    System.out.print("[1] ");
                }else if(n==1) {
                    System.out.print("    ");
                }
                if(m>=hang-n2&&n==2) {
                    System.out.print("[2] ");
                }else if(n==2) {
                    System.out.print("    ");
                }
                if(m>=hang-n3&&n==3) {
                    System.out.print("[3] ");
                }else if(n==3) {
                    System.out.print("    ");
                }
                if(m>=hang-n4&&n==4) {
                    System.out.print("[4] ");
                }else if(n==4) {
                    System.out.print("    ");
                }
                if(m>=hang-n5&&n==5) {
                    System.out.print("[5] ");
                }else if(n==5) {
                    System.out.print("    ");
                }
                if(m>=hang-n6&&n==6) {
                    System.out.print("[6] ");
                }else if(n==6) {
                    System.out.print("    ");
                }
                if(m>=hang-n7&&n==7) {
                    System.out.print("[7] ");
                }else if(n==7) {
                    System.out.print("    ");
                }
                if(m>=hang-n8&&n==8) {
                    System.out.print("[8] ");
                }else if(n==8) {
                    System.out.print("    ");
                }
                if(m>=hang-n9&&n==9) {
                    System.out.print("[9] ");
                }else if(n==9) {
                    System.out.print("    ");
                }
            }
            System.out.println();

        }
    }
}

Task 2: Eliminate Duplicates

Write a complete computer program that eliminates duplicate values in a given array of integer numbers (containing only non-negative numbers). Your program should print all distinct numbers to the screen in the order of their appearance in the list below. (Use an empty space to separate values)

//package lab8;

public class task_2 {
    public static void main(String[] args) {
        int[] input = { 96, 91, 75, 22, 77, 46, 73, 87, 66, 46, 19, 55, 65, 21, 83, 8, 73, 80, 90, 52, 72, 51, 2, 22, 4,
                8, 51, 66, 36, 82, 68, 61, 74, 64, 72, 24, 21, 46, 63, 23, 93, 44, 40, 24, 78, 15, 85, 61, 18, 40, 3,
                40, 27, 58, 81, 56, 73, 5, 15, 38, 83, 91, 19, 92, 79, 66, 45, 62, 45, 93, 66, 12, 25, 60, 65, 48, 18,
                60, 17, 21, 61, 74, 44, 65, 50, 88, 88, 43, 5, 7, 96, 3, 95, 18, 64, 74, 92, 88, 62, 52, 32, 96, 75, 78,
                11, 16, 41, 33, 5, 12, 26, 10, 90, 5, 77, 91, 69, 35, 14, 73, 54, 56, 74, 56, 48, 50, 7, 47, 44, 81, 13,
                15, 39, 65, 74, 32, 71, 58, 67, 81, 30, 52, 69, 99, 67, 83, 49, 34, 7, 31, 32, 58, 26, 62, 53, 56, 63,
                7, 59, 58, 68, 45, 45, 22, 49, 35, 28, 91, 62, 0, 37, 92, 17, 64, 12, 84, 91, 87, 47, 13, 51, 75, 8, 62,
                45, 63, 11, 46, 85, 34, 51, 50, 76, 46, 25, 64, 49, 9, 7, 33 };
    for(int i=0;i<=input.length-1;i++) {
        boolean flag=true;
        for(int n=0;n<=i-1;n++) {

            if(input[i]==input[n]) {
                flag=false;

                break;

            }

        }
        if(flag) {
        System.out.print(input[i]+" ");
        }
    }
  }
}
最后修改日期:2019年12月24日

作者

留言

头像

没注释,我看不懂:(

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。