Daily check-in 01 of the ladder competition (1-6 problem solutions)

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

foreword

In general, the Blue Bridge Cup is not a small blow, so this time, I will continue to check in every day, >=6 questions every day, and start basic training from PTA in order. Check in on the first day

Brush to death brush to death. Here I will do a simple order according to the difficulty of the questions.

L1-001 Hello World (5 分)

insert image description hereGet used to the format

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner Scanner = new Scanner(System.in);
        System.out.println("Hello World!");
    }
}
复制代码

L1-004 Calculate Celsius temperature (5 points)

insert image description here

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int c = scanner.nextInt();
        c = 5*(c-32)/9;
        System.out.println("Celsius = "+c);
    }
}
复制代码

L1-003 Single-digit statistics (15 points)

insert image description here

import java.util.Scanner;

public class Main {

    static int[] count;
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String InputString = scanner.next();
        count = new int[10];
        scanner.close();

        for (char c : InputString.toCharArray()) {
            count[c-'0']++;
        }
        for (int i = 0; i < count.length; i++) {
            if(count[i]!=0){
                System.out.println(i+":"+count[i]);
            }
        }

    }
}

复制代码

L1-005 Exam seat number (15 points)

insert image description here

important point

This topic is actually very simple. It is mainly a problem of input optimization. Scanner can't work. I don't have the ability to know it.


import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;


/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}

public class Main {

    static Student[] Students;


    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        int N = Reader.nextInt();
        Students = new Student[N+1];
        for (int i = 0; i < N; i++) {

            Student student = new Student(Reader.next(),Reader.nextInt(),Reader.nextInt());
            Students[student.id_temp] = student;

        }


        int M = Reader.nextInt();
        for (int i = 0; i < M; i++) {

            find();
        }

    }

    public static void find() throws IOException {
        int id_temp = Reader.nextInt();
        Student student = Students[id_temp];

        System.out.println(student.id_stu+" "+student.id_set);
    }


    static class Student{
        String id_stu;
        int id_temp;
        int id_set;

        public Student(){}

        public Student(String id_stu, int id_temp, int id_set) {
            this.id_stu = id_stu;
            this.id_temp = id_temp;
            this.id_set = id_set;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id_stu='" + id_stu + '\'' +
                    ", id_temp=" + id_temp +
                    ", id_set=" + id_set +
                    '}';
        }
    }
}

复制代码

L1-002 Printing Hourglass (20 points)

insert image description here

ideas

In fact, it is also simple to say. We mainly need to know that our main layer has several layers, and each layer is regular.

Since this hourglass is an arithmetic progression, then d=2 so the sum n(1+(n-1)*d +1)/2 = n^2 /2. However, it should be noted that the N we input here is the sum of two, the sum of the two identical sequences -1, because the first one overlaps.

(Even if there are too many, it doesn't matter if it is automatically rounded down)

Then there are several elements in each layer, which is also simple, arithmetic sequence!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String ch = sc.next();

        int  h = (int) Math.sqrt((n+1)/2);// 计算高度

        for (int i = 0; i < h; i++)    //打印上半层沙漏
        {
            for (int j = 0; j < i; j++)
                System.out.print(" ");
            for (int j = 0; j < 2 * (h - i) - 1; j++)
                System.out.printf("%c", ch.toCharArray()[0]);
            System.out.println();

        }

        for (int i = 2; i <= h; i++)    //打印下半层沙漏
        {
            for (int j = 0; j < h - i; j++)
                System.out.print(" ");
            for (int j = 0; j < 2 * i - 1; j++)
                System.out.printf("%c", ch.toCharArray()[0]);
            System.out.print("\n");
        }
        System.out.printf("%d", n - 2 * h * h + 1);
    }
}


复制代码

L1-006 Continuous Factor (20 points)

insert image description hereThis topic is very good, but I only know the brute force solution

ideas

Or the idea of ​​violence, the first is to decompose the divisor, here we start from 2. Then it wants to be incrementing, so 2 is its first divisor, so I started guessing sum = 1 from 2, 2,3,4,5,6 sum to multiply these numbers. Only when sum<=n, then I record the length of these factors and the first subscript. For example, the record above is 2, to 6. Since we are looking for it from small to large, it is definitely not the smallest length found at the beginning. So you have to keep looking forward and start from 3 next.

At this time, since we are looking for divisors, we can first reduce the complexity with Math.sqrt(N).

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		long i = 0, j = 0;
		long len = 0, start = 0, sum = 0;
		long n;
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		for (i = 2; i < Math.sqrt(n); i++) {
			sum = 1;
			for (j = i; sum <= n; j++) {
			// j表示当前因子,i表示首因子,sum表示目前乘积,开始暴力枚举
				sum = sum * j;
				if (n % sum == 0 && j - i + 1 > len) {
					start = i;
					len = j - i + 1;
				}
			}
		}
		if (start == 0) {
			start = n;
			len = 1;
		}
		System.out.println(len);
		for (int k = 0; k < len - 1; k++) {
			System.out.print(start + k + "*");
		}
		System.out.println(start + len - 1);
	}
}

复制代码

In terms of optimization, I think it should be possible to optimize with the idea of ​​KMP, because it is not possible to continue to enumerate from 3. For example, 630, the minimum is 4 5 6 7, the continuous is 5 6 7, that is, from 2 4 5 67 for the first time and then records 2 to 6 for the second time 3 4 5 6 7 records 5 to 7 and starts from 4 for the third time . So think about this.

Guess you like

Origin juejin.im/post/7084970288948969508