Niuke Brush Questions: The guest is like the cloud and the cut flower cloth strip (Java)

Table of contents

Topic 1: Customers Come Like Clouds

enter description

output description

problem solving ideas

the code

Topic 2: Cut flower strips

enter description

output description

problem solving ideas

 the code


Topic 1: Customers Come Like Clouds

Link: Kesiyunlai

NowCoder has opened a breakfast shop, and the customers of this shop have a strange habit: as long as they come to this shop for breakfast once, they will come every day; and, after eating breakfast in this shop for two days, everyone , I will bring a new friend to taste it every day.
As a result, the customers of this store have grown from one person at the beginning to hundreds of thousands of people: 1, 1, 2, 3, 5...
Now, NowCoder would like to ask you to help me to make statistics. In a certain period of time, he sold a total of How many breakfasts to serve (assuming only one breakfast per guest).

enter description

The test data includes multiple groups.
Each set of data contains two integers from and to (1≤from≤to≤80), which respectively represent the from day and to day of the store opening.
 

output description

Corresponding to each set of inputs, output how many breakfasts need to be cooked in the days from from to to (including two days from and to).

problem solving ideas

This question is to find the Fibonacci number, here enter two integers from and to (1≤from≤to≤80), so we can find the Fibonacci number from 1 to 80, and then traverse from form to to Finish summing Fibonacci numbers

Note: Considering that the 80th Fibonacci number will exceed the range of int, you need to use Long to receive

the code

import java.util.Scanner;

public class Main2 {
    /*
    * 题目:他们只要来这家店吃过一次早餐,就会每天都过来;并且,所有人在这家店吃了两天早餐后,接下来每天都会带一位新朋友一起来品尝。
    他们只要来这家店吃过一次早餐,就会每天都过来;并且,所有人在这家店吃了两天早餐后,接下来每天都会带一位新朋友一起来品尝.
    现在,NowCoder想请你帮忙统计一下,某一段时间范围那他总共卖出多少份早餐(假设每位客人只吃一份早餐)。
    *
*/
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        while(sc.hasNextInt()){// a和b范围在[1,80]之间
            int a = sc.nextInt();
            int b =sc.nextInt();
            Long[] arr = new Long[80];//先把80个斐波那契数全部建立
            arr[0]=1L;
            arr[1]=1L;
            for(int i=2;i<arr.length;i++){
                arr[i]=arr[i-1]+arr[i-2];
            }
            Long sum=0L;
                for(int i=a-1;i<b;i++){
                    sum+=arr[i];
                }
            System.out.println(sum);
        }
    }
}


Topic 2: Cut flower strips

Link: cut flowers

enter description


        The input contains multiple sets of data.

        Each set of data contains two strings s, t, which are the patterned cloth strips and small decorative strips that appear in pairs, and the cloth strips are represented by visible ASCII characters. How many visible ASCII characters are there, and the pattern of the cloth strips There are also many kinds of tricks. Ribbons and trims are no longer than 1000 characters long.


output description


        Corresponding to each set of inputs, output the maximum number of small decorative strips that can be cut from the patterned cloth. If there is no piece, then output 0, and each result occupies one line.
Example 1
input
abcde a3
aaaaaa aa
output
0
3

problem solving ideas

1. Find the number of string t contained in the string s

2. Here we can use the greedy algorithm to find out the position of the first string t from the string s.

        If the first string t is not found, the quantity 0 can be returned directly;

        If the position of the first string t is found, all characters before the first string t are subtracted from the string s, and the remaining characters are used as the new string s to continue recursively repeating the search method just now

Examples are as follows:

 the code

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    /*
    * 题目:从字符串A找出子字符串a的个数,不能重复累加
    * 思路:字符串A中找到一个就减少一个子字符串的字符数*/
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            System.out.println(func(s1,s2));
            }
        }
        public static int func(String s1,String s2) {
            int i = s1.indexOf(s2);//返回字符串s2在字符串s1第一个字符下标,若不存在则返回-1
            if(i == -1){
                return 0;
            }
            s1=s1.substring(i+s2.length(),s1.length());//减去第一个在s1字符串出现的s2,把后面剩余的字符串保留下来继续递归
            return 1+func(s1,s2);
        }

    }

 

Guess you like

Origin blog.csdn.net/qq_73471456/article/details/130416620