天梯赛每日打卡01(1-6题解)

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第10天,点击查看活动详情

前言

总得来说,蓝桥杯的打击不小,所以本次继续每日打卡,每日>=6题,按照顺序从PTA 开始基础训练。 打卡第一天

刷不死就往死里刷。这里会按照,我对题目的难易程度进行一个简单排序。

L1-001 Hello World (5 分)

在这里插入图片描述 适应一下格式呗

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 计算摄氏温度 (5 分)

在这里插入图片描述

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 个位数统计 (15 分)

在这里插入图片描述

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 考试座位号 (15 分)

在这里插入图片描述

注意点

这个题目其实很简单,主要还是输入优化的问题,Scanner 不行,没这个能力知道吧。


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 打印沙漏 (20 分)

在这里插入图片描述

思路

其实说来也简单,我们主要是要知道,我们那个主要有几层,每一层都是有规律的。

由于这个沙漏是一个等差数列,然后d=2 所以总和 n(1+(n-1)*d +1)/2 = n^2 /2。 不过这块要注意的是,我们这里的话输入的N是两个总和,两个一样的数列之和-1,因为第一个重合了。

(哪怕多了,自动往下取整也没啥)

然后是每一层有几个元素,那个也简单,等差数列呀!

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 连续因子 (20 分)

在这里插入图片描述 这个题目是挺好的,但是我只知道暴力解法

思路

还是暴力的思路,首先是分解约数,这里的话我们从2 开始。然后它要的是递增的,所以 2 是它的第一个约数,所以我就开始猜 sum = 1 从 2 开始 2,3,4,5,6 sum 去乘以这些数字。 当sum<=n 的时候才去,然后我去记录它的这些因子的长度和第一个下标。例如上面记录的就是 2,到 6 由于我们是从小到大去找,所以一开始找到的绝对不是最小长度的。 所以要一直往前找 于是接下来从3开始。

此时由于是找约数,所以我们可以先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);
	}
}

复制代码

优化的话我想应该可以去 用 KMP 的思想优化,因为它2不行是继续从3开始枚举的。例如630 最小是 4 5 6 7 连续的是 5 6 7 那就是从 2 4 5 67 第一次走 然后记录了2 到 6 第二次 3 4 5 6 7 记录 5 到 7 第三次 再从4 开始。 所以这块可以想想。

猜你喜欢

转载自juejin.im/post/7084970288948969508
1-6