[CC++ cattle must brush every day]— cattle brush questions series

Hits: 0

Personal business card:

🐼About the author: A freshman student
🐻‍❄️Personal homepage: Xiaoxinai [learning.]
🐼Personal WeChat: hmmwx53
🕊️Series of columns: Zero basic learning of java —– Re-learn c language
🐓Daily sentence: The road is You choose, there is no winner or loser, only if it’s worth it

Article directory

Foreword: 🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊

Recently, many friends have asked Xiaoxin if there is a useful website for brushing questions. I want to improve my ability.
After Xiaoxin’s careful thinking and personal experience, I recommend the super practical 🎉Click [to visit Nioke.com directly🎉There] are
rich manufacturers There are real interview questions, various algorithms from easy to difficult, and special question banks are available. Come and experience it soon🎏🎏🎏

Subject: AB1 [Template] Stack✨

Description ✨

Please implement a stack.
Operation:
push x: push the addition x\x onto the stack, ensuring that x\x is an int integer.
pop: output the top of the stack, and let the top of the stack pop out of the stack
top: output the top of the stack, and the top of the stack is not on the stack

Enter a description ✨:

The first line is a positive integer n\n, representing the number of operations. (1 \leq n \leq 100000)(1≤n≤100000)
The next n\n, each line is a [string] , representing an operation. The guarantee operation is one of three in the title description.

Output description ✨:

If the action is push, nothing is output.
If it is the other two, if the stack is empty, output “error”,
otherwise output according to the corresponding operation.

Example 1✨

Input:
6
push 1
pop
top
push 2
push 3
pop
copy
Output:
1
error
3

Problem solving code ✨:

#include<stdlib.h>
#include<string.h>
#define MAX 100000
typedef struct Stack{
    int a[MAX];
    int topp;
}*LStack;
void inistack(LStack s){
    s->topp=0;
}
void push(LStack s,int x){
    s->a[s->topp]=x;
    s->topp++;
}
int pop(LStack s){
    if(s->topp==0){
        return -1;
    }
    return s->a[--s->topp];

}
int top(LStack s){
    if(s->topp==0){
        return -1;
    }
    int num=s->a[--s->topp];
    s->topp++;
    return num;
}
int main(){
    LStack s=(LStack)malloc(sizeof(struct Stack));
    inistack(s);
    int n=0;
    scanf("%d",&n);
    while(n--){
        char *str=(char*)malloc(6*sizeof(char));

        scanf("%s",str);
        if(!strcmp(str,"push")){
            int num=0;
            scanf("%d",&num);
            push(s,num);
        }
        if(!strcmp(str,"pop")){
            int num1=pop(s);
            if(num1==-1){
                printf("error\n");
                continue;
            }
            printf("%d\n",num1);
        }
        if(!strcmp(str,"top")){
            int num2=top(s);
            if(num2==-1){
                printf("error\n");
                continue;
            }
            printf("%d\n",num2);
        }

    }




    return 0;
}

Title: NC65 [Fibonacci] Sequence 🎉

Description 🎉

Everyone knows the Fibonacci sequence, and now you are required to input a positive integer n, please output the nth item of the Fibonacci sequence.
The Fibonacci sequence is a sequence that satisfies fib(x)=\left{

$$\begin{array}{rcl} 1 & {x=1,2}\ fib(x-1)+fib(x-2) &{x>2}\ \end{array}$$

\right.fib(x)={

1

fib(x−1)+fib(x−2)

Sequence of x=1,2
x>2 ​Data range: 1\leq n\leq 401≤n≤40Requirements : space complexity O(1)O(1), time complexity O(n)O(n) , this problem also has a time complexity O(logn)O(logn) solution

Enter a description 🎉:

a positive integer n

Return value description 🎉:

Output a positive integer.

Example 🎉:

Input: 4
Return value: 3
Description:
According to the definition of Fibonacci sequence, fib(1)=1, fib(2)=1, fib(3)=fib(3-1)+fib(3-2 )=2,fib(4)=fib(4-1)+fib(4-2)=3, so the answer is 3.

Example 2
Input: 1
Return Value: 1

Example 3
Input: 2
Return Value: 1

Problem solving code🎉:

/**
 * 
 * @param n int integer
 * @return int integer
 *
 * Please add static when declaring and defining global variables in C language to prevent repeated definitions
 */
int Fibonacci(int n ) {
    // write code here
      int a[50];
    a[1]=1,a[2]=1;
    for(int i=3;i<n+1;i++)
    {
        a[i]=a[i- 1 ]+a[i- 2 ]; //A[-1], etc. must be avoided here
    }
    return a[n];
}

=========================================================================================================================================

Leave a Reply

Your email address will not be published.