第五次CCF-CSP认证(含C++源码)

第五次CCF-CSP认证

第一道(easy)


题目链接

思路及AC代码

基本思路:
比如随便给一个数列:1 2 3 4 4 6 6 6
很明显输出的是5 我们在数有几段的时候有两种情况我们会加1
1.前面没有数字的时候,也就是第一段
2.前面数字和后面数字不一样的时候
如果我们用一次遍历就能输出正确答案,我们就需要做到以上两点,第二点很好搞,我们怎么处理第一段,如果我们不管他 在某些情况之下就会少输出一段,我觉得那干脆就一开始就默认有一段不就好了吗,具体到代码中就是让我们的count初始化为1,然后从数组第二位开始遍历就好了,这样就不用搞什么乱七八糟的东西,本身就是一道很简单的题目不是吗

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N =1010;
int n;
int s[N];
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int count=1;
    for(int j=1;j<n;j++)
    {
        if(s[j]!=s[j-1])
        {
            count++;
        }
    }
    cout<<count<<endl;
}

第二道(easy)


题目链接

思路及AC代码

有了昨天的帖子铺垫,这道是不是有些过于轻松写意了O(∩_∩)O哈哈~
这个帖子的第三题 如果忘了可以温一温
我觉得区别就是昨天那题需要我们通过getdate函数枚举出具体日期,不论是控制输入输出以及细节处理要求都比较高一些,这题是直接给你个年数天数,问你某年的第x天是几月几号,这样我们就暴力一下就可以,直到刚刚好枚举到第x天为止

solution 1

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int is_leapyear(int year)
{
    if(year%4==0 && year%100 ||year%400==0)
    {
        return 1;
    }else{
        return 0;
    }
}
// 获取每个月的天数
int daysInMonth(int year, int month) {
    int days[13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 ) {
        return days[2]+is_leapyear(year);
    }
    return days[month];
}
int main()
{
    int year,days;
    cin>>year>>days;
    for(int i=1;i<=12;i++)
    {
        for(int j=1;j<=daysInMonth(year,i);j++)
        {
            if(--days ==0)
            {
                printf("%d\n%d\n",i,j);
                return 0;
            }
            
        }
    }
    return 0;
}
                      

solution 2

我在题解里看到了比较简洁的代码粘过来了,其实思路是一样的 只不过更直接了

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int y,d;
    cin>>y>>d;
    if(y%4==0&&y%100!=0||y%400==0)a[2]=29;else a[2]=28;
    int x=0;
    while(d>0){
        if(d-a[x]<=0)break;
        else{
            d-=a[x];
            x++;
        }
    }
    cout<<x<<'\n'<<d;
}

作者:垫底抽風
链接:https://www.acwing.com/solution/content/132282/

第三道(mid)

题目链接

思路及AC代码(mid)

这题 题干看着挺吓人 我用AI解读了一下题目 我一开始自己做也读了半天才看懂是要干什么



我自己没有全部弄明白 在这里给出参考代码 跟Y总课上的代码是一样的 思路参考:yxc

cpp 复制代码
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <vector>

using namespace std;

int n, m;
unordered_map<string, string> vars;
vector<string> strs;

int main()
{
    cin >> n >> m;
    getchar(); //过滤cin读剩下的回车

    while(n -- )
    {
        string str;
        getline(cin, str);
        strs.push_back(str);
    }

    while(m -- )
    {
        string key, value;
        cin >> key; //变量名不含空格,可以直接cin读
        char c; //变量值含空格和双引号,用getchar()读
        while(c = getchar(), c != '\"'); //过滤第一个引号之前的内容
        while(c = getchar(), c != '\"') value += c;
        vars[key] = value; //存入哈希表
    }

    for(auto &str : strs) //遍历vetor中的每一行
    {
        for(int i = 0; i < str.size();)
            if(i + 1 < str.size() && str[i] == '{' && str[i + 1] == '{') //如果需要替换变量
            {
                int j = i + 3;
                string key;
                while(str[j] != ' ' || str[j + 1] != '}' || str[j + 2] != '}') //没有到结尾的话
                    key += str[j ++ ]; //读取模板中的变量
                cout << vars[key]; //哈希表替换模板中的值
                i = j + 3; //处理完之后要跳过空格和两个右括号
            }
            else cout << str[i ++ ]; //不需要替换变量,直接输出模板原内容
        cout << endl;
    }
    return 0;
}
相关推荐
tyler-泰勒20 分钟前
c++:迭代器的失效
开发语言·c++
决斗小饼干22 分钟前
震惊!C++程序真的从main开始吗?99%的程序员都答错了
c++
白晨并不是很能熬夜25 分钟前
【JVM】字节码指令集
java·开发语言·汇编·jvm·数据结构·后端·javac
辰尘_星启28 分钟前
【vscode】一键编译运行c/c++程序
c语言·c++·vscode·debug·cmake
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧32 分钟前
C语言_数据结构总结7:顺序队列(循环队列)
c语言·开发语言·数据结构·算法·visualstudio·visual studio
橘颂TA34 分钟前
每日一练之合并两个有序链表
数据结构·链表
LIUJH123334 分钟前
数据结构——单调栈
开发语言·数据结构·c++·算法
2301_807449201 小时前
字符串相乘——力扣
java·算法·leetcode
shylyly_1 小时前
list的模拟实现
数据结构·c++·链表·迭代器·list·list的模拟实现
ianozo2 小时前
数据结构--【栈与队列】笔记
数据结构·笔记