One dimension array of NOI / 1.6 programming basics (incomplete)

Posted by breckenridge on Sun, 13 Feb 2022 19:32:54 +0100

01: Number of identical numbers as specified

OpenJudge - 01: Number of identical numbers as specifiedhttp://noi.openjudge.cn/ch0106/01/

describe

Outputs the number of identical numbers in a sequence of integers as specified.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//The first behavior, a, represents the length of the sequence of integers
    int a = scanner.nextInt();
    int[] b = new int[a];
    for (int i = 0; i < a; i++) {
//Enter a whole number
        b[i] = scanner.nextInt();
    }
//For the specified integer c
    int c = scanner.nextInt();
//Counter
    int f=0;
    for (int i = 0; i < a; i++) {
        if (b[i]==c) f++;
    }
//Output Counter
    System.out.println(f);
}

02:Pottery picking apples

OpenJudge - 02: Pottery picking appleshttp://noi.openjudge.cn/ch0106/02/

describe

There is an apple tree in the courtyard of the Taotao family. Ten apples grow on it every autumn. When the apples are ripe, the pottery will run to pick them. Pottery has a bench 30cm high. When she can't pick the apples directly with her hands, she will step on the bench and try again.

Now that 10 apples are known to be on the ground and the maximum height that the pottery handle can reach when it is straightened, please help pottery figure out the number of apples she can pick. Suppose she touches an apple and it falls off.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//  Create an array of 10 int types
    int[] sum = new int[10];
    for (int i = 0; i < 10; i++) {
        sum[i] = scanner.nextInt();
    }
//  n denotes the maximum height that a pottery handle can reach when straightened
    int n = scanner.nextInt();
    int f=0;
    for (int i = 0; i < 10; i++) {
//  n+30 is the maximum height that pottery can reach when its handle is stretched out on a stool
        if (sum[i]<=n+30) f++;
    }
//  Export the number of apples that pottery can pick
    System.out.println(f);
}

03: Calculate book costs

OpenJudge - 03: Calculate book costshttp://noi.openjudge.cn/ch0106/03/

describe

Here is a unit price list for a book:
28.9 yuan/book of calculation overview
Data Structure and Algorithms 32.7 Yuan/Ben
Digital logic 45.6 yuan/book
C++ Programming Tutorial RMB 78/book
Artificial Intelligence 35 yuan/book
Computer architecture 86.2 yuan/book
Compilation Principle 27.8 Yuan/Ben
Operating System 43 Yuan/Ben
Computer network $56 per book
JAVA programming $65 per book
Given the quantity purchased for each book, the total fees payable are calculated programmatically.

Code

public static void main(String[] args) {
//  Price of imported books
    double txt[] = {28.9,32.7,45.6,78,35,86.2,27.8,43,56,65};
    Scanner scanner = new Scanner(System.in);
    double sum=0;
    for (int i = 0; i < 10; i++) {
//      Enter a to represent the principal number of purchases
        int a = scanner.nextInt();
        sum=sum+txt[i]*a;
    }
//  The output represents the total fees payable. One digit after the exact decimal point
    System.out.println(sum);
}

04:Reverse Array Replay

OpenJudge - 04: Array Reverse Replayhttp://noi.openjudge.cn/ch0106/04/

describe

Restore values in an array in reverse order. For example, the original order is 8,6,5,4,1. Change to 1,4,5,6,8 is required.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//  Number of elements in array n
    int n = scanner.nextInt();
//  Create an array with n elements
    int[] arr = new int[n];
    for (int i = 0; i < n; i++) {
//      Input Elements
        arr[i] = scanner.nextInt();
    }
    for (int i = arr.length; i > 0; i--) {
//      Output integers of an inverse-order array, separated by spaces between each two integers
        System.out.print(arr[i-1]+" ");
    }
}

05:Age and Diseases

OpenJudge - 05: Age and Diseaseshttp://noi.openjudge.cn/ch0106/05/

describe

A hospital needs to sort out the previous diagnostic records in order to find out if a disease is related to age.

The proportion of the total number of patients in the four age groups 0-18, 19-35, 36-60, 61 or more (including 61) was calculated.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//    Enter the number of past patients n
    int n = scanner.nextInt();
//    Create Array
    int[] old = new int[n];
    for (int i = 0; i < n; i++) {
//        Enter the age at which each patient was ill
        old[i] = scanner.nextInt();
    }
//    s for 0-18, m for 19-35, b for 36-60, j for 61 or more (including 61)
    int s=0,m=0,b=0,j=0;
//    Accumulator accumulator s, m, b, j
    for (int i = 0; i < n; i++) {
        if (old[i] >= 0 && old[i] <= 18) s++;
            else if (old[i] >= 19 && old[i] <= 35) m++;
                else if (old[i] >= 36 && old[i] <= 60) b++;
                    else if (old[i] >= 61) j++;
    }
//    % Output
    System.out.printf("%.2f%%\n",(float)s*100/n);
    System.out.printf("%.2f%%\n",(float)m*100/n);
    System.out.printf("%.2f%%\n",(float)b*100/n);
    System.out.printf("%.2f%%\n",(float)j*100/n);
}

06: Trees outside the school gate

OpenJudge - 06: Trees outside the school gatehttp://noi.openjudge.cn/ch0106/06/

describe

There is a row of trees on the L-length road outside the gate of a school, and the interval between each two adjacent trees is 1 meter. We can think of a road as a number axis, with one end at number axis 0 and the other end at L. Each integer point on the axis, that is, 0, 1, 2,..., L, has a tree.

There are areas on the road that need to be used to build the metro. These areas are represented by their starting and ending points on the axis. The coordinates of the start and end points of any region are known to be integers, and there may be overlapping parts between regions. Now you want to move the trees in these areas, including the two trees at the end of the area. Your task is to calculate how many trees there are on the road after all these trees have been removed.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//    Enter two integers, L for the length of the road and M for the number of areas
    int L = scanner.nextInt();
    int M = scanner.nextInt();
//    L+1 is a tree
    int s=L+1;
    boolean[] b = new boolean[s];
//    Start tree initial value set to 1
    for (int i = 0; i < s; i++) {
        b[i] = true;
    }
    for (int i = 0; i < M; i++) {
        int x = scanner.nextInt();
        int y = scanner.nextInt();
//        Prevent pits in titles and ensure left x right y
        if (x>y) {int t=x;x=y;y=t;}
//        Start cutting 1 to 0
        for (int j = x; j <= y; j++) {
//            When there are trees
            if (b[j] == true){
//                Cutting down trees
                b[j]=false;
//                count
                s--;
            }
        }
    }
//    Output the number of remaining trees on the road
    System.out.println(s);
}

07: Fun jumps

OpenJudge - 07: Fun Jumphttp://noi.openjudge.cn/ch0106/07/

describe

An interesting jump in a sequence of length n (n>0) currently occurs only when the absolute value of the difference between adjacent elements is sorted from exactly 1 to (n-1). For example, 1423 has an "interesting jump" because the absolute values of the differences are 3,2,1, respectively. Of course, any sequence that contains only a single element must have an "interesting jump". You need to write a program to determine if a given sequence has an "interesting jump".

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//    n stands for sequence length
    int n;
    n = scanner.nextInt();
//    An array represents the elements of a sequence
    int[] a = new int[n];
    for (int i = 0; i < n; i++) {
//        Save in Element
        a[i] = scanner.nextInt();
    }
//    b array represents the difference between two numbers
    int[] b = new int[n-1];
    for (int i = 0; i < n-1; i++) {
        b[i]=Math.abs(a[i+1]-a[i]);
    }
//    Bubble sort from smallest to largest
    for (int i = 0; i < n-1; i++) {
        for (int j = i+1; j < n-1; j++) {
            if (b[i] > b[j]){int t=b[i];b[i]=b[j];b[j]=t;}
        }
    }
    for (int i = 0; i < n-1; i++) {
//        If not equal to i+1, output Not Jolly
        if (b[i] != i+1) {
            System.out.println("Not jolly");
//            Conditions meet Direct End Procedure
            return;
        }
    }
//    Output Jolly
    System.out.println("Jolly");
}

08:Stone scissors cloth

describe

OpenJudge - 08: Stone Scissor Clothhttp://noi.openjudge.cn/ch0106/08/

Stone scissors are a common game of boxing. Stone is better than scissors, scissors are better than cloth, and stones are better than cloth. If two people punch the same way, they won or lost.

One day, Little A and Little B were playing with stone scissors. It is known that their fists are cyclical, such as: "Stone-cloth-stone-scissors-stone-cloth-stone-scissors...", which is a cycle of "stone-cloth-stone-scissors". Please ask, who wins more rounds than A and B after N rounds?

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//    N stands for N wheels
    int N = scanner.nextInt();
//    NA represents the fist cycle of Little A
    int NA = scanner.nextInt();
//    NB represents the fist cycle of Little B
    int NB = scanner.nextInt();

//    The law of NA fisting
    int[] a = new int[NA];
    for (int i = 0; i < NA; i++) {
        a[i] = scanner.nextInt();
    }
//    The Law of NB's Boxing
    int[] b = new int[NB];
    for (int i = 0; i < NB; i++) {
        b[i] = scanner.nextInt();
    }
//    t stands for subscript, s for number of wins
    int ta=0,tb=0,sa=0,sb=0;
    for (int i = 0; i < N; i++) {
//        Judging the winner or loser
        if (a[ta] == 0 && b[tb] == 2) sa++;
            else if (a[ta] == 0 && b[tb] == 5) sb++;
                else if (a[ta] == 2 && b[tb] == 0) sb++;
                    else if (a[ta] == 2 && b[tb] == 5) sa++;
                        else if (a[ta] == 5 && b[tb] == 0) sa++;
                            else if (a[ta] == 5 && b[tb] == 2) sb++;
//        t Subscript++.
        ta++;
        tb++;
//        Subscript returns to 0
        if (ta == NA) ta=0;
        if (tb == NB) tb=0;
    }
//    Output Win or Lose
    if (sa > sb) System.out.println("A");
        else if (sa < sb) System.out.println("B");
            else
        System.out.println("draw");
}

09:Vector Point Product Calculation

OpenJudge - 09: Vector Point Product Calculationhttp://noi.openjudge.cn/ch0106/09/

describe

In linear algebra and computational geometry, vector point product is a very important operation.

Given two n-dimensional vectors a=(a1, a2,..., a n) and b=(b1, b2,..., B n), find the dot product a.b=a1 b1+a2 b2+...+ Anbn.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
//    Enter an integer n representing the number of elements
    int n = scanner.nextInt();
    int[] an = new int[n];
    int[] bn = new int[n];
//    Enter the value of the element of the array an
    for (int i = 0; i < n; i++) {
        an[i] = scanner.nextInt();
    }
//    Enter the value of the element of the array bn
    for (int i = 0; i < n; i++) {
        bn[i] = scanner.nextInt();
    }

//    sum stands for accumulator
    int sum=0;
    for (int i = 0; i < n; i++) {
//        a1*b1+sum
        sum = an[i]*bn[i]+sum;
    }
//    Output Answer
    System.out.println(sum);
}

10:Large Integer Addition

OpenJudge - 10:Large Integer Additionhttp://noi.openjudge.cn/ch0106/10/

describe

Find the sum of two non-negative integers that do not exceed 200 bits.

Code

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String a = scanner.next();
    String b = scanner.next();
    int[] na = new int[a.length()];
    int[] nb = new int[b.length()];
    for (int i = 0; i < a.length(); i++) {
        na[i] =  a.charAt(i) - '0';
    }
    for (int i = 0; i < b.length(); i++) {
        nb[i] =  b.charAt(i) - '0';
    }

    int B,S,N;
    if (na.length > nb.length) {
        B = na.length;
        S = nb.length;
        N=1;
    }
        else if (na.length < nb.length){
            B = nb.length;
            S = na.length;
            N=2;
        }
            else {
            B = nb.length;
            S = na.length;
            N=3;
            }

    int sj;
    if (B == S){
        if (na[B-2] + nb[B-2] >= 10){
            if (na[B-1] + nb[B-1] + 1 >= 10){
                sj=B+1;
            }
            else {
                sj=B;
            }
        }
        else{
            if (na[B-1] + nb[B-1] >= 10){
                sj=B+1;
            }
            else {
                sj=B;
            }
        }
    }
    else {
        sj = B;
    }
    int[] sum = new int[sj];

    if (N == 1) {
        for (int i = 0; i < B; i++) sum[i] = na[i];
        for (int i = S - 1; i >= 0; i--) {
            sum[i+(B-S)] = sum[i+(B-S)] + nb[i];
            if (sum[i+(B-S)] + nb[i] >= 10) {
                sum[i+(B-S)] = sum[i] + nb[i] - 10;
                sum[i + (B-S-1)] = sum[i + (B-S-1)] + 1;
            }
        }
    }
    else if (N == 2){
        for (int i = 0; i < B; i++) sum[i] = nb[i];
        for (int i = S - 1; i >= 0; i--) {
            sum[i+(B-S)] = sum[i+(B-S)] + na[i];
            if (sum[i+(B-S)] + na[i] >= 10) {
                sum[i+(B-S)] = sum[i+(B-S)] + na[i] - 10;
                sum[i + (B-S-1)] = sum[i + (B-S-1)] + 1;
            }
        }
    }
    else {
        for (int i = 0; i < B; i++) sum[i+1] = nb[i];
        for (int i = S - 1; i >= 0; i--) {
            sum[i + 1] = sum[i + 1] + na[i];
            if (sum[i+1] + na[i] >= 10) {
                sum[i + 1] = sum[i + 1] - 10;
                sum[i] = sum[i] + 1;
            }
        }
    }

    for (int i = 0; i < sj; i++) {
        System.out.print(sum[i]);
    }
}

Topics: Java