牛客简单题(c++/python)

在这里插入图片描述

def foblq(n):#菲薄列切数列f(n) = f(n-1)+f(n-2)
    if(n <= 2):
        return 1
    a = 1
    b = 1
    for i in range(2,n):
        s = a + b
        a = b
        b = s
    return s

while True:
    try:
        n = int(input())
        print(foblq(n))
    except:
        break

在这里插入图片描述


while True:
    try:
        s = input()
        num_c = 0
        str_c =0
        k_c = 0
        other_c = 0
        for c in s:
            if c.isdigit():
                num_c += 1
            elif c.isalpha():
                str_c += 1
            elif c == ' ':
                k_c += 1
            else:
                other_c += 1

        print(str_c)       
        print(k_c)
        print(num_c)
        print(other_c)
    except:
        break

在这里插入图片描述

#include<iostream>
using namespace std;


struct ListNode
{
    int m_nKey;
    ListNode* m_pNext;
    ListNode():m_nKey(0),m_pNext(nullptr){};
    ListNode(int(x)):m_nKey(x),m_pNext(nullptr){};
};

class Solution{
public:
    ListNode* Knode(int n,ListNode* List,int k){//返回倒数第k个节点
        if(List == NULL){
            return nullptr;
        }
        int a = n-k+1;
        while(a--){
            List = List->m_pNext;
        }
        return List;
    }
};
int main(){
    int n,k,node;
    Solution s;
    while(cin >> n){//输入链表个数
        ListNode *head = new ListNode();
        ListNode *phead = head;
        int num = n;
        while(n--){//构造链表
            cin >> node;//输出链表的节点
            ListNode *next = new ListNode(node);
            head = head -> m_pNext = next;
            //head = next;
        }
        cin >> k;//倒数第k个节点
        ListNode * res = s.Knode(num,phead,k);
        if(res){
            cout << res->m_nKey << endl;
        }else{
            cout << 0 <<endl;
        }
    }
    return 0;
}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

while True:
    try:
        n = int(input())
        if(n < 3):
            print(-1)
        elif(n % 4 == 0):
            print(3)
        elif(n % 4 == 1 or n % 4 == 3):
            print(2)
        elif(n % 4 == 2):
            print(4)
    except:
        break

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>

int main(){
    string s;
    int n;
    while(cin >> s){
        cin >> n;
//         for(int i = 0; i < n;i++){
//             cout << s[i];
//         }
        cout << s.substr(0,n)<<endl;
    }
    
    return 0;
}
while True:
    try:
        s = input()
        n = int(input())
        print(s[:n])
    except:
        break

在这里插入图片描述

#include<iostream>
using namespace std;

int main(){
    int a[] = {31,28,31,30,31,30,31,31,30,31,30,31};//平年每一个月的天数,闰年第二个月29天
    int year = 0, month = 0,day = 0;
    while(cin >> year >> month >> day){
        int sum = 0;
        for(int i = 0; i < month;i++){
            sum += a[i-1];//最后一个月不加,后面直接加day
        }
        if((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month > 2){//闰年
            sum += 1;
            sum += day;
        }else{
            sum += day;
        }
        cout << sum << endl;
    }
    return 0;
}
while True:
    try:
        date = input().split()
        year,month,day = int(date[0]),int(date[1]),int(date[2])
        a = [31,28,31,30,31,30,31,31,30,31,30,31]
        sum = 0
        for i in range(month-1):
            sum += a[i]
        if (year % 400 == 0 or (year %4 == 0 and year % 100 !=0)) and month > 2:
            sum += day 
            sum += 1
        else:
            sum += day
        print(sum)
    except:
        break

在这里插入图片描述

while True:
    try:
        short_s = input()
        long_s = input()
        for i in short_s:
            if i not in long_s:
                print("false")
                break
        else:
            print("true")
    except:
        break
        
while True:
    try:
        short_s = input()
        long_s = input()
        if set(short_s) & set(long_s) == set(short_s):
            print("true")
        else:
            print("false")
    except:
        break
        
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;

int main(){
    string s1;
    string s2;
    while(cin >> s1 >> s2){
        set<char>set2;
        for(char c:s2){
           set2.insert(c);
        }
        bool res = true;
        for(char c:s1){
            if(set2.count(c) == 0){
                res = false;
                break;
            }
        }
        if(res){
            cout << "true"<< endl;
        }else{
            cout << "false" <<endl;
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值