個人名片:
🐼作者簡介:一名大一在校生 🐻❄️個人首頁:小新愛學習.
🕊️系列專欄:零基礎學java ----- 重識c語言
🐓每日一句:路是自己選的,沒有輸赢,隻有值不值得
文章目錄
- 題目:AB1 【模闆】棧✨
- 描述✨
- 輸入描述✨:
- 輸出描述✨:
- 示例1✨
- 題解代碼✨:
- 題目:NC65 斐波那契數列🎉
- 描述🎉
- 輸入描述🎉:
- 傳回值描述🎉:
- 示例🎉:
- 解題代碼🎉:
- 🐯想要快速刷題,了解更多歡迎通路 : [牛客網](https://www.nowcoder.com/link/pc_csdncpt_xxaxx_c)
前言:🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊🎊
最近有許多小夥伴問小新有沒有好用的刷題網站,想提升一下自我能力
經過小新缜密的思考與親身體驗,推薦超實用的🎉點選直接通路牛客網🎉
裡面用豐富的大廠面試真題、各類算法由易到難層層遞進、專項題庫應有盡有,還不快來體驗體驗🎏🎏🎏
題目:AB1 【模闆】棧✨
描述✨
請你實作一個棧。
操作:
push x:将 加x\x 入棧,保證 x\x 為 int 型整數。
pop:輸出棧頂,并讓棧頂出棧
top:輸出棧頂,棧頂不出棧
輸入描述✨:
第一行為一個正整數 n\n ,代表操作次數。(1 \leq n \leq 100000)(1≤n≤100000)
接下來的 n\n ,每行為一個字元串,代表一個操作。保證操作是題目描述中三種中的一種。
輸出描述✨:
如果操作為push,則不輸出任何東西。
如果為另外兩種,若棧為空,則輸出 "error“
否則按對應操作輸出。
示例1✨
輸入:
6
push 1
pop
top
push 2
push 3
pop
複制
輸出:
1
error
3
題解代碼✨:
#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;
}
題目:NC65 斐波那契數列🎉
描述🎉
大家都知道斐波那契數列,現在要求輸入一個正整數 n ,請你輸出斐波那契數列的第 n 項。
斐波那契數列是一個滿足 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)
x=1,2
x>2
的數列
資料範圍:1\leq n\leq 401≤n≤40
要求:空間複雜度 O(1)O(1),時間複雜度 O(n)O(n) ,本題也有時間複雜度 O(logn)O(logn) 的解法
輸入描述🎉:
一個正整數n
傳回值描述🎉:
輸出一個正整數。
示例🎉:
輸入:4
傳回值:3
說明:
根據斐波那契數列的定義可知,fib(1)=1,fib(2)=1,fib(3)=fib(3-1)+fib(3-2)=2,fib(4)=fib(4-1)+fib(4-2)=3,是以答案為3。
示例2
輸入:1
傳回值:1
示例3
輸入:2
傳回值:1
解題代碼🎉:
/**
*
* @param n int整型
* @return int整型
*
* C語言聲明定義全局變量請加上static,防止重複定義
*/
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]等的情況
}
return a[n];
}
🐯想要快速刷題,了解更多歡迎通路 : 牛客網