第一周刷题

部分题用 Java , 后面会转 C/C++ , 先把基础题拿全

详解在博客
更基础的就不放了

一 . 写出这个数

1

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String[] pinyin = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        Scanner input = new Scanner(System.in);
        String n = input.nextLine();
        int sum = 0;
        for (int i = 0; i < n.length(); i++) {
            sum += n.charAt(i) - '0';
        }
        String s = Integer.toString(sum);
        for (int i = 0; i < s.length(); i++) {
            if (i != s.length()-1) {
                System.out.print(pinyin[s.charAt(i)-'0'] + " ");
            } else {
                System.out.println(pinyin[s.charAt(i)-'0']);
            }
        }
    }
}

​ 这道题的意思就是所说 , 将输入的数进行相加 , 最后得出的数 因为是字符串用 charAt() 得到的是对应的ASCII 所需 '0', 在通过遍历pinyin这个数组得到对应下标的值

二 . 数组循环右移问题

2

#include<stdio.h>
#include<stdlib.h>

int main(){
    int N,M,i,temp;
    scanf("%d %d",&N,&M);
    int arr[N];
    M = M % N;
    for(i=0;i<N;i++){
        scanf("%d",&arr[i]);
    }
    for(i=N-M;i<N;i++){
        printf("%d ",arr[i]);
    }
    for(i=0;i<N-M;i++){
        if(i == (N-M)-1){
            printf("%d\n",arr[i]);
        }else{
            printf("%d ",arr[i]);
        }
    }
}

​ 数组存放数据 , 只是输出出来 , 所以直接输出N-M(N为长度 ,M为移动次数)之后的数, 再输出之前的数就行

三 . 成绩排名

3

#include<stdio.h>
#include<stdlib.h>

struct Student{
    char name[20];
    char id[20];
    int score;
};

int main(){
    int n,i;
    scanf("%d",&n);
    struct Student s[n];
    for(i=0;i<n;i++){
        scanf("%s %s %d",&s[i].name,&s[i].id,&s[i].score);
    }
    int min=0,max=0;
    for(i=0;i<n;i++){
        if(s[i].score >s[max].score){
            max = i;
        }
        if(s[i].score <s[min].score){
            min = i;
        }
    }
    printf("%s %s\n",&s[max].name,&s[max].id);
    printf("%s %s\n",&s[min].name,&s[min].id);
}

​ 先定义一个结构体来存放数据 , 通过循环写入每个结构体的数据 , 再通过去比较 score 来找出最大的和最小的 , 分别输出对应的 name 和 ID

四 . 试试手气(2022)

4

import java.util.Scanner;
public class Main {  
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n, m = 0;
        int[] a = new int[6];
        for (int i = 0; i < 6; i++) {
            a[i] = scanner.nextInt();
        }
        n = scanner.nextInt();
        for (int i = 0; i < 6; i++) {
            int temp = 0;
            for (int j = 6; j > 6 - n; j--) {
                if (a[i] == j) {
                    temp++;
                }
                m = j - temp;
            }
            if (i != 5) {
                System.out.print(m + " ");
            } else {
                System.out.println(m);
            }
        }
    }
}

​ 这道题的题意就是 , 根据你给出的点数 , 每一次输出每个骰子最大的点数 , 比如说一个骰子 , 给出的是4 一共扔两次 , 那第一次就是6 , 再者5 , 最终输出5 ,个骰子都一样 , 所以只需要一个嵌套循环找出每个骰子在第n次的最大值 , 并输出

​ 在Pat上第三个测试点内存超了

第二周刷题

一.

二 .

三 .

四 .

第三周刷题

一 . 跑步锻炼

image-20230401225609947

int[] months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year, month, day;
int ans = 6;//一月一号是周六
int cnt = 0;//公里数
for (year = 2000; year <= 2020; year++) {
    if (year%4 == 0 && year%100 != 0 || year%400 == 0) {
        months[2] = 29;
    } else {
        months[2] = 28;
    }
    for (month = 1; month <= 12; month++) {
        for (day = 1; day <= months[month]; day++) {
            cnt++;
            if (ans == 8) {
                ans = 1;
            }
            if (day == 1 || ans == 1) {
                cnt++;
            }
            ans++;
            if (year == 2020 && month == 10 && day == 1) {
                System.out.println(cnt);
                return;
            }
        }
    }
}

二 . 子串分值

image-20230402000801740

Scanner scan = new Scanner(System.in);
//在此输入您的代码...
String str = scan.next();
char chs[] = str.toCharArray();
int len = str.length();
int res = 0;
for(int i = 0;i < len;i++){
    int left = 0;
    int right = 0;
    char c = chs[i];
    for(int j = i - 1;j >=0 && chs[j] != c;j --){
        left++;
    }
    for(int j  = i + 1;j < len && chs[j] != c;j++){
        right++;
    }
    res += (left+1)*(right+1);
}
System.out.println(res);
scan.close();

三 . 子串分值和

image-20230401225524894

Scanner sc = new Scanner(System.in);
int[] arr = new int[26];
long res=0;
//初始化 arr数组全部设-1
Arrays.fill(arr, -1);

String str = sc.nextLine();
int willIndex = str.length();
for (int i = 0; i < str.length(); i++) {
    int lastIndex = arr[str.charAt(i) - 'a'];
    res += (long)(i - lastIndex) * (willIndex - i);
    arr[str.charAt(i) - 'a'] = i;
}
System.out.println(res);

四. 砝码

image-20230401234737851

Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
    arr[i] = scan.nextInt();
}

HashSet<Integer> set = new HashSet<>(); //使用存储有序不重复的 set 集合
LinkedList<Integer> queue = new LinkedList<>(); //存储产生新的砝码值(临时)
set.add(0); //赋予初始值,放 0 砝码的目的是为了存单个砝码的重量
for (int i = 0; i < arr.length; i++) { //每次都放入一个砝码就和 set 中的所有元素:相加和相减的值再存入队列中
    for (Integer nextValue : set) {
        //offer()向链表尾部添加
        queue.offer(arr[i] + nextValue); //向尾部添加相加的值
        queue.offer(Math.abs(arr[i] - nextValue)); //向尾部添加相减的值
    }
    while (! queue.isEmpty()) { //提取再放回set集合,以此反复
        set.add(queue.poll()); //.poll()剔除并删除第一个 .add()并添加给HashSet
    }
}
set.remove(0);
System.out.println(set.size()); //- 1 去除 0 重量砝码
scan.close();

五 . 外卖优先级

image-20230401235424186

e
最后修改:2023 年 04 月 02 日
如果觉得我的文章对你有用,请随意赞赏