ACM题解Day3| To Crash or not To Crash,Integer Prefix ,I don’t want to pay for the Late Jar

本文介绍了如何通过数据结构和逻辑模拟方法解决编程题目,包括两个C++代码示例:ToCrashornotToCrash和IntegerPrefix,以及模拟斐波那契数列的实现。作者分享了学习路径和ACM题解经验,鼓励读者关注并参与讨论。
摘要由CSDN通过智能技术生成

学习目标:

博主介绍: 27dCnc
专题 : 数据结构帮助小白快速入门算法
👍👍👍👍👍👍👍👍👍👍👍👍
☆*: .。. o(≧▽≦)o .。.:*☆

Github今日打卡
在这里插入图片描述

  • ACM题解

学习时间:

  • 周一至周五晚上 7 点—晚上9点
  • 周六上午 9 点-上午 11 点
  • 周日下午 3 点-下午 6 点

学习内容:

  1. To Crash or not To Crash
  2. Integer Prefix
  3. I don’t want to pay for the Late Jar
  4. Kernel of Love

内容详细:

To Crash or not To Crash

题目考点: 逻辑 模拟 小函数

在这里插入图片描述

解题思路

  1. 只有三行,三行判断
  2. 判断 = 会不会遇到 H
  3. 每行都判断,判断哪一行先遇到

详细代码
版本一:

/******begin******/
#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
using unl = __int128_t;
using ll = long long;
using namespace  std;

void slove() {
    string s1,s2,s3;
    cin>>s1>>s2>>s3;
    int a = 0 ,b = 0, c = 0;
    for (int i = 0;i < 10; i++) {
        if (s1[i] == '=') {  //如果第一行中有 = (车) 就向后进行遍历看后面是否为空,如果不为空输出那个不为空的物体(值)
            for (int j = i + 1; j < 10;j++) {
                if (s1[j] != '.') {
                    cout<<s1[j];
                    return ;
                }
            }
        }
        if (s2[i] == '=') {  //同理
            for (int j = i + 1; j < 10;j++) {
                if (s2[j] != '.') {
                    cout<<s2[j];
                    return ;
                }
            }
        }
        if (s3[i] == '=') {
            for (int j = i + 1; j < 10;j++) {
                if (s3[j] != '.' ) {
                    cout<<s3[j];
                    return ;
                }
            }
        }

    }
    cout << "You shall pass!!!";
}

signed main() {
    #if Run
        int _;cin>>_;while(_--) slove();
    #else
        slove();
    #endif
    return 0;
}

/*******end*******/

版本二: 优化

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
using unl = __int128_t;
using ll = long long;
using namespace  std;

 /*用小函数去判断*/
bool plan(string s3,int i) {
    for (int j = i + 1; j < 10;j++) {
        if (s3[j] != '.' ) {
        // 将结果输出
            cout<<s3[j];
            return true;
        }
    }
    return false;
}
/**/
void slove() {
    string s1,s2,s3;
    /*将行数输入*/
    cin>>s1>>s2>>s3;
    int a = 0 ,b = 0, c = 0;
    for (int i = 0;i < 10; i++) {
    /*从第一行开始判断*/
         if (s1[i] == '=') {
            if (plan(s1,i)) return ; 
        }
		/*只要一行没有满足条件就返回*/
        if (s2[i] == '=') {
            if (plan(s2,i)) return ;
        }

        if (s3[i] == '=') {
            if (plan(s3,i)) return ;
        }
    }
    cout << "You shall pass!!!";

}

signed main() {
    #if Run  //这里用于控制多行输入
        int _;cin>>_;while(_--) slove();
    #else
        slove();
    #endif
    return 0;
}

Integer Prefix

题目考点: 函数 模拟
在这里插入图片描述

思路
只要遇到数字就输出,不是数字停止遍历和输出
题目意思是输出最大的数组这些数全部都要在开头,而且不能出现数字字符之外的字符
注意 如果没有数字字符就输出 -1

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
using unl = __int128_t;
using ll = long long;
using namespace  std;


void slove() {
    string s; cin >> s;  //输入字符
    bool k = true;  //用于判断是否有数字字符
    for (int t = 0; t < s.size(); t++) {
        if (isdigit(s[t])) {  //用函数来判断是否是数字字符
            k = false;  //这个表示有
            cout << s[t];   ///如果是则边输出
        } else {
            break;
        }
    }
    if (k) cout << -1; //如果存在就不会执行
}

signed main() {
    #if Run
        int _;cin>>_;while(_--) slove();
    #else
        slove();
    #endif
    return 0;
}

I don’t want to pay for the Late Jar

题目考点: 模拟

在这里插入图片描述
题目解释

给你一个总时间S和每个餐厅吃午餐的时间ti和愿意支付的额外费用fi,如果ti<=S,那么他会得到fi的幸福值,否则他会得到fi-(ti-S)的幸福值,求他在一个餐厅吃饭能得到的最大幸福值。
Problem solving: 直接判断求最大值就行了,注意有负数,初始值不能附为0。

详细代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
 
int main(){
    ll d;
    cin>>d;
    ll n,s;
    for(ll j=0;j<d;j++){
        cin>>n>>s;
        ll f[n],t[n];
        for(int i=0;i<n;i++){
            cin>>f[i]>>t[i];
        }
        ll max= LLONG_MIN;
        for(ll i=0;i<n;i++){
            if(t[i]<=s){
                if(f[i]>max) max=f[i];
            }else{
                ll m=s+f[i]-t[i];
                if(m>max) max=m;
            }
        }
        cout<<"Case #"<<j+1<<": "<<max<<endl;
    }
 
    return 0;
}

case 情况难搞,于是就不搞函数,会更简单,或者,加给全局变量

Kernel of Love

题目考点: 模拟 斐波那契数列 gcd 规律总结

在这里插入图片描述

详细代码:

#include<bits/stdc++.h>
#define Run 1
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;
int a[N];



void slove() {
   int n; cin >> n;
   a[3] = 2; a[4] = 3;  // 用于初始化斐波那契数列
   for (int i = 5; i <= N; i++) {
    if ((i % 3 && (i + 1) % 3) || i % 3 == 0) {  //这个是满足幸福夫妻的情况
        a[i] = a[i - 1] + 1;  //动态规划的写法
    } else {
        a[i] = a[i - 1]; //不满足幸福父亲的情况
    }
   }
   cout << a[n] << endl; ///储存在数组种输出数组结果即可   
   //我现在也不太清楚
}

signed main() {
    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);
    #if Run
        int _;cin>>_;while(_--) slove();
    #else
        slove();
    #endif
    return 0;
}

学习产出:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇
  • 习的 vlog 视频 1 个

在这里插入图片描述

重磅消息:

GTP - 4 最新版接入服务他来了 点击链接即可查看详细

GTP - 4 搭建教程

🔥如果此文对你有帮助的话,欢迎💗关注、👍点赞、⭐收藏、✍️评论,支持一下博主~

  • 24
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值