Baidu Pinecone Elite Class--March Daily Exercises-1

This is the March daily practice question-1 of the Baidu Pine Cone Elite Class. The value of the question is quite high. There are only similar question types on the Internet. The solution is only an explanation of its thinking, and it is a C++ code style. Yes, I will not just refer to the solution, write the Java solution , and write some core code comments based on my own understanding . When learning algorithms and data structures, I have simply learned C++ and can deal with most programming problems. Java is used to develop projects, and now I try to use Java to solve problems. For *the questions that cannot be done with java or cannot be passed through operation, .the questions that are typed are partially passed with java. Welcome everyone to comment and correct, and make progress together.

1. Find 1

Topic : Brother Xiaoma fell in love with binary recently. He especially likes binary strings full of 1s, but there are usually 0s. So he gives you a binary string and asks you the number of substrings whose characters are all 1 (the result is modulo 10°+7).

/**
	输入格式:一个二进制字符串
	输出格式:所有字符都为1的子串个数
	样例1  			 样例2  		  样例2
	输入:0110111	    输入:101		输入:111111
	输出:9			输出:2		输出:21
	备注
	s[i] == '0' 或 s[i] == '1'
	1 <= s.length <= 10^5
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    String str = input.next();
      // code here
    int mod = 1000000007;
    int result = 0;
    int num = 0;
    for(int i = 0; i < str.length(); i++){
    
    
        if(str.charAt(i) == '1'){
    
    
            num++;
        }else{
    
    
            //规律:11 -> 1 + 2 = 3  1111 -> 1 + 2 + 3 + 4 = 10
            //也就是单纯的排列问题  1 + 2 + …… + n = (1 + n)*n / 2
            result = (result + num*(num + 1) / 2) % mod;
            num = 0;
        }
    }
    //考虑收尾,如果最后一次都为1,需要加上
    result = (result + num*(num + 1) / 2) % mod;
    System.out.println(result);
    input.close();
   }
}

2. Challenge soldiers and generals

Topic : The military parade is about to take place. The general wants to select people from n people under him to participate. He uses the following method to select people: n people stand in a circle, numbered 1-n counterclockwise, and now A and B are two people to choose. A counts k counterclockwise and stops, B counts m clockwise and stops, and the selected 1 or 2 people output until everyone outputs. For the first time, A starts from 1, B starts from N, and then AB starts from the next digit of the respective directions of the previous respective positions.

/**
	输入格式:可能有多组数据,每组一行输入三个整数n、k、m,输入000停止。
	输出格式:输出每轮选出的人,用,隔开,每个编号占3个格子(右对齐)。
	样例1  			 
	输入:10 4 3  0 0 0
	输出:4 8, 9 5, 3 1, 2 6, 10, 7		
	备注
	0<n<=20;k,m在int范围内,且为正数;组数少于20
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
    public static int solve(int p, int d, int t, int n, int[] a){
    
    
        while(t > 0){
    
    
            do{
    
    
                //转圈一个个找物核心公式:(p - c + d + n) % n + c
                //p -> 当前位置  c -> 当前位置  d -> 顺时针或逆时针循环  n -> 总人数,一个周期
                p = (p - 1 + d + n) % n + 1;
            }while(a[p] == 0);
            t--;
        }
        return p;
    }
    
   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    int[] a = new int[25];

    while(true){
    
    
        int n = input.nextInt();
        int k = input.nextInt();
        int m = input.nextInt();
        if(n==0 && k==0 && m==0){
    
    
            break;
        }else{
    
    
            for(int i = 1; i <= n; i++){
    
    
                a[i] = 1;
            }
            int person = n;
            int A = n;
            int B = 1;
            while(person > 0){
    
    
                A = solve(A, 1, k, n, a);
                B = solve(B, -1, m, n, a);
                if( A == B){
    
    
                    System.out.printf("%3d", A);
                    person--;
                }else{
    
    
                    System.out.printf("%3d%3d", A, B);
                    person -= 2;
                }
                a[A] = a[B] = 0;
                if(person > 0){
    
    
                    System.out.printf(",");
                }
            }
            System.out.print("\n");
        }
    }
      input.close();
   }
}

3. Water level line

Topic : There are k liters of water in the water dispenser, and the little code brother hopes to keep the water volume of the water dispenser between [1, r] within t days. Begin to choose to add y liters (can only be added once a day) or not. Given k, l, r, t, x, y, ask whether the water volume can be controlled at [l, r] within t days, output Yes, otherwise output No.

/**
	输入格式:仅一行为 k,l, r ,t, x,y
	输出格式:输出Yes或输出No
	样例1  			 
	输入:8 1 10 2 6 4
	输出:No		
	备注
	其中: 1<l≤k≤r ≤1e9,1 ≤t ≤1e9,1 ≤x ≤1e6,1≤y ≤1e9。
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    int[] a = new int[10];
    for(int i = 0; i <= 5; i++){
    
    
        a[i] = input.nextInt();
    }
    while(a[3] > 0){
    
    
        // 判断饮水机的水量的临界值即可,符合则加水
        if(a[0] + a[5] <= a[2]){
    
    
            a[0] += a[5];
        }
        // 判断饮水机的水量的临界值即可,符合则用掉水
        if(a[0] - a[4] > a[1]){
    
    
            a[0] -= a[4]; 
        }else{
    
    
            System.out.printf("No");
            return;
        }
        a[3]--;
    }
    System.out.printf("Yes");
      input.close();
   }
}

4. Brother Xiaoma's checkers game

Topic : Brother Xiaoma likes checkers. The checkers game is on a straight line, with a total of n positions ( 1 ~n ), and each position has 2 states: 0 means there is no chess piece, 1 means there is a chess piece. Brother Xiaoma's chess pieces can naturally pass through the positions without chess pieces. When there is a chess piece in front of you, Brother Xiaoma can skip it directly. When there are two or more chess pieces connected together, Xiaoma's chess pieces cannot jump over. At this time, it is necessary to spend energy and destroy some chess pieces in order to jump over. It is known that destroying a chess piece costs a bit of energy. Brother Xiaoma's chess pieces start from the 0th position. Now it is required that Brother Xiaoma need to spend at least how much energy to reach the end point (position n)?

/**
	输入格式:第1行包含一个正整数n ;第2行n个整数ai,表示棋盘的状态。
	输出格式:一个整数,输出最小耗费的能量数。
	样例1  			 
	输入:5  0 1 1 0 0
	输出:1
	备注
	其中: 1<=n<=10^5
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    int n = input.nextInt();
    int[] a = new int[100005];
    for(int i = 1; i <= n; i++){
    
    
        a[i] = input.nextInt();
    }
    int num = 0;
    //核心:a[i] == 1 && a[i + 1] == 1 -> 连续两个1只需摧毁后一个
    for(int i = 1; i <= n; i++){
    
    
        if(a[i] == 1 && a[i + 1] == 1){
    
    
            a[i + 1] = 0;
            num++;
        }
    }
    //若最后一位为0的话加起来不影响,若为1的话需要消耗能量破坏掉占据位置
    //结尾为1的例子:011111 -> 010111   -->   01010 1 -> 最后一位不能跳过需要销毁
    //结尾为0的例子:011110 -> 010110   -->   01010 0 -> 最后一位为空,加0不影响
    num += a[n];
    System.out.printf("%d", num);
    input.close();
   }
}

Five, Xiaomao and robots

Topic : Brother Xiaoma bought a new robot, but this robot can only do three actions because it is very cheap. Three actions: forward FD, backward BK and repeat REPEAT. Add numbers after FD to indicate how many steps forward; add numbers after BK to indicate how many steps back; add numbers and square brackets after REPEAT to repeat the command in square brackets. The numbers added by the three actions are all positive integers. (*)

The format of the command: 1. Combination of FD and BK commands; 2. Add REPEAT command and FD, BK combination in the REPEAT command, and REPEAT is at the end.

/**
	输入格式:一行字符串(长度不超过200) 。
	输出格式:表示最后得到的步数,表示离起点的距离(非负整数)。
	样例1  			 
	输入:REPEAT 5[ FD 50 REPEAT 10[FD 100]]
	输出:5250
*/
// java为实现,下方是我编写的各种可能的代码,都是报`Nosuchelementexception异常`--`无元素异常`,百度也没找到相关的解决办法,热烈欢迎各位的指正,共同进步~
import java.util.Scanner;
import java.util.*;

class Main {
    
    
    public static long order(){
    
    
        Scanner input = new Scanner(System.in);
        long result =  0;
        // String str = input.next();
        char firstNum = input.next().charAt(0);
        while(firstNum != '\0'){
    
    
            if(firstNum == ']'){
    
    
                break;
            }
            // String afterNum = str.substring(1);
            long num = input.nextInt();
            // String str1 = input.next();
            // long num = str1.charAt(0) - '0';
            if(firstNum == 'R'){
    
    
                // char k = str1.charAt(1);
                char k = input.next().charAt(0);
                // char k = (char)new BufferedReader(new InputStreamReader(System.in)).read();
                result += firstNum * order();
            }
            if(firstNum == 'F'){
    
    
                result += num;
            }
            if(firstNum == 'B'){
    
    
                result -= num;
            }
        }
        input.close();
       return result;
    }

   public static void main(String[] args) {
    
    
    // code here
    long result = Math.abs(order());
    System.out.printf("%ld", result);
   }
}


// c++题解
#include<bits/stdc++.h> 

using namespace std;
long long order(){
    
    
    string str;
    char firstNum, k;
    long long num, result = 0;
    while( cin >> firstNum ){
    
    
        if(firstNum == ']'){
    
    
            break;
        }
        cin >> str >> num;
        if(firstNum == 'R'){
    
    
            cin >> k;//也就是输入[
            result += num * order();
        }
        //原本打算用三目运算法的,发现这里不适合使用,firstName的值需要准确,测试输出发现FFRR,根据递归而减去了55
        // result = firstNum == 'F' ? result += num : result -= num;
        if(firstNum == 'F'){
    
    
            result += num;
        }
        if(firstNum == 'B'){
    
    
            result -= num;
        }
    }
    return result;
}

int main( )
{
    
    
    cout << abs(order());
    return 0;
}

6. Bank account

Topic : It is said that theft of bank accounts is not easy to attract attention if only the value below the decimal point is stolen, so you decide to try it. The bank has a total of n accounts and m transfers. For each transfer, you can steal funds (transfer amount - transfer amount) and increase the alert value of the transferred account by the same value. When the alert value of any account Value > 1, or the transfer cannot be realized (the balance of the transfer-out account is insufficient), or all m transfers are completed, you stop stealing, please calculate the total amount stolen. (*)

/**
	输入格式:第一行n,m,表示有n个账户,m条转账记录;第二行n个实数,表示每个账户的资金;接下来m行,每行有三个参数;整数α,整数y,实数z,分别表示转出账户,转入账户,和转账金额。输出盗取金额,保留两位小数。
	输出格式:表示最后得到的步数,表示离起点的距离(非负整数)。
	样例1  			 
	输入:5 5
		 2 2 2 2 2
		 1 2 1.5
		 2 1 1.5
		 1 2 1.5
		 2 1 1.5
		 1 2 1.5
	输出:2.00
备注: 1 <= n <= 1000, 1<= m <= 10000;
0< 每个账户初始资金 < 10; 1<= x, y <= n, x!= y; 0 < z < 100;
*/

//测试用例可以通过,运行时出现运行错误,调试了很久,没看出来是哪里出错,欢迎帮忙大家指正
import java.util.Scanner;
import java.util.*;

class Main {
    
    
   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    int n = input.nextInt();
    int m = input.nextInt();
    // ps:这里是double数组,不然double赋值给int会导致精度损失
    double count[] = new double[1005];
    double warn[] = new double[1005];
    for(int i = 1; i  <= n; i++){
    
    
        count[i] = input.nextInt();
        // System.out.println( count[i]);
        warn[i] = 0;
        // System.out.println( warn[i]);
    }
    int x, y;
    double z;
    double sum = 0.0d;
    for(int i = 1; i <= m; i++){
    
    
        x = input.nextInt();
        y = input.nextInt();
        z = input.nextDouble();
        if(count[x] < z){
    
    
            break;
        }
        // 根据输入 第一次x = 1 ; 第二次x = 2
        count[x] -= z;
        // System.out.println( count[x]);
        sum += z - Math.floor(z);
        count[y] += Math.floor(z);
        warn[y] += z - Math.floor(z);
        // System.out.println( z);
        // System.out.println(Math.floor(z));
        // System.out.println( count[y]);
        //  System.out.println( warn[y]);
        // System.out.println(sum);
        if(warn[y] > 1.0){
    
    
            break;
        }
    }
    System.out.println(String.format("%.2f", sum));
    input.close();
   }
}

7. Numbers

Topic : Input n, and output the number of numbers in which each digit of the natural number of 1-n contains only 0 or 1.

/**
	输入格式:输入一个整型数字n(1 ≤n ≤1e9)
	输出格式:输出一行一个整数表示答案
	样例1  			 
	输入:19
	输出:3
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
    static int sum = 0;
    public static void num(int m, int n){
    
    
        if(m > n){
    
    
            return;
        }else{
    
    
            sum++;
        }
        num(m * 10, n);//递归  n -> 1 , 10
        num(m * 10 + 1, n);// n = 0 , n -> 1 , 11
    }

   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    int n = input.nextInt();
    num(1 ,n);
    System.out.printf("%d",sum);
    input.close();
   }
}

Eight, the decoding of the string

Question : You are given a string consisting of [, ], numbers and uppercase letters, and you are now asked to decode it. The character string is in the form of [Dx], which means D consecutive Xs, for example, [4CB]or [2[2CB]]can be expressed. Once there are brackets, there will be numbers, and they are positive integers. There will be two sets of square brackets adjacent to each other, such as [4A][5B]. (.)

/**
	输入格式:AC[3FUN]
	输出格式:ACFUNFUNFUN
	样例1  			 
	输入:19
	输出:3
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
    static int len;
    static String str;
    public static void s(int num){
    
    
        // C++类似逻辑的代码可以通过
        // java部分通过,也就是正规的输入可以有相应的输出
        if(num == len || str.charAt(num) == ']'){
    
    
            return;
        }
        // 若是这种  ]]][4A][5B]  就无法正确输出
        // 下面是我尝试修改的代码,发现输出为ABBBBBABBBBBABBBBBABBBBBBBBBB
        // 感觉把s(num)和return去掉,问题不大,可是就报数组越界异常,希望哪位大佬帮忙解决
        // if(num == len){
    
    
        //     return;
        // }
        // if(str.charAt(num) == ']'){
    
    
        //    num++;
        //    s(num);
        //    return;
        // }
        if(str.charAt(num) == '['){
    
    
            num++;
            int sum = 0;
            while(str.charAt(num) >= '0' && str.charAt(num) <= '9'){
    
    
                sum = sum * 10 + str.charAt(num) - '0';//若是数字大于等于10时
                num++;
            }
            while(sum > 0){
    
    
                sum--;
                s(num);
            }
            int tmp = 1;//当]比[多一个时跳出循环
            while(tmp > 0){
    
    //循环字符串,跳过索引字母
                if(str.charAt(num) == '['){
    
    
                    tmp++;
                }else if(str.charAt(num) == ']'){
    
    
                    tmp--;
                }
                num++;
            }
            s(num);
        }else{
    
    
            while(str.charAt(num) >= 'A' && str.charAt(num) <= 'Z'){
    
    
                System.out.printf("%s", str.charAt(num));
                num++;
            }
            s(num);
        }
    }

   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    str = input.nextLine();
    len = str.length();
    s(0);
    input.close();
   }
}

Nine, Fibonacci, but it is a string

Title : Now there is a string group: the 0th item a0 = "IAKIOI"; the 1st item a1 = "WHENWILLSCORLLOFTAIWUCOMEOUT!!!"; the kth item after that is composed of k-2 item + k-1 item. Ask what is the cth character of the nth item string. (.)

/**
	输入格式:两个整数n,c意义如题
	输出格式:一个字符表示答案
	样例1  			 
	输入:5 6
	输出:I
	备注:数据范围:0≤n ≤80,1≤c≤第n项字符串的长度
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    
    static String a0 = "IAKIOI";
    static String a1 = "WHENWILLSCORLLOFTAIWUCOMEOUT!!!";
    static int len[] = new int[85];
    static int  n, c;
    // 运行部分通过,由于charAt(int)类型,传long无法取值
    public static void ch(int n, int c){
    
    
        if(n == 0){
    
    //百度没有找到怎么取长整形的字符串的字符,知道的朋友,望告知
            System.out.printf("%c", a0.charAt(c - 1));
            return;
        }
        if(n == 1){
    
    
            System.out.printf("%c", a1.charAt(c - 1));
            return;
        }
        //若取的字符c小于等于字符的前半截
        if(c <= len[n - 2]){
    
    
            ch(n-2, c);
        }else{
    
    //若取的字符c大于字符的前半截,c需要从后截0开始比较
            ch(n-1, c - len[n - 2]);
        }
    }
   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    n = input.nextInt();
    c = input.nextInt();
    len[0] = a0.length();
    len[1] = a1.length();
    for(int i = 2; i <= n; i++){
    
    
        len[i] = len[i - 2] + len[i - 1];
    }
    ch(n, c);
    input.close();
   }
}

Ten, the largest average

Question : Given an array with a length of n, find a subinterval whose length is greater than or equal to m so that the average value of the elements in this interval is the largest. (.)

/**
	输入格式:第一行输入整数n和m,数据间用空格隔开;接下来n行,每行输入一个整数ai。
	输出格式:输出一个整数,表示平均值的最大值乘以1000再向下取整之后得到的结果。
	样例1  			 
	输入:10 6
		6 
        4
        2
        10
        3
        8
        5
        9
        4
        1
	输出:6500
	备注:1<n≤100000,1 < m≤n,0≤ai≤1e7
*/

// 只通过一个用例,调试没看不出来错哪了
import java.util.Scanner;
import java.util.*;

class Main {
    
    
    static int n, m;
    static double  mid;
    static double a[] = new double[100000]; 
    static double sum[] = new double[100000];

    public static int check(double mid){
    
    
        double minn = 0;
        for(int i = 1; i <= n; i++){
    
    
            sum[i] = sum[i - 1] + a[i] - mid;
        }
        for(int i = m; i <= n; i++){
    
    
            minn = minn > sum[i] ? sum[i] : minn;
            if(sum[i] >= minn){
    
    
                return 1;
            }
        }
        return 0;
    }

   public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);
    // code here
    n = input.nextInt();
    m = input.nextInt();
    double l = 0, r = 0;
    for(int i = 1; i <= n; i++){
    
    
        a[i] = input.nextInt();
        r = r < a[i] ? a[i] : r;
    }

    while(r - l >= 0.001){
    
    
        mid = (l + r) / 2;
        if(check(mid) >= 0){
    
    
            l = mid;
        }else{
    
    
            r = mid;
        }
    }
    System.out.printf("%d", (int)Math.floor(r * 1000));
    input.close();
   }
}

11. Separation of numbers

Topic : Brother Xiaoma gives you a sequence of length n, find the number of solutions that divide the sequence into two left and right parts and the sum of the numbers in the two parts is equal.

/**
	输入格式:给定一整数n ;接下来有n个数a[i]。其中: 1≤n≤105,a[i]的绝对值不大于10000。
	输出格式:输出一行表示答案。
	样例1  			 
	输入:9
		1 5 -6 7 9 -16 0 -2 2	
	输出:3
*/
import java.util.Scanner;
import java.util.*;

class Main {
    
    

   public static void main(String[] args) {
    
    

    Scanner input = new Scanner(System.in);
    // code here
    int n = input.nextInt();
    int a[] = new int[100000]; 
    int sum[] = new int[100000]; 
    int num = 0;
    for(int i = 1; i <= n; i++){
    
    
        a[i] = input.nextInt();
        sum[i] = sum[i - 1] + a[i];
    }
    for(int i = 2; i <= n; i++){
    
    //前缀和
        if(sum[n] - sum[i - 1] == sum[i - 1]){
    
    
            num++;
        }
    }
    System.out.printf("%d", num);
    input.close();
   }
}

记录每一个学习瞬间

Guess you like

Origin blog.csdn.net/qq_51601665/article/details/129735064