蓝桥杯C++大学B组一个月冲刺记录2024/3/10

蓝桥杯C++大学B组一个月冲刺记录2024/3/10

规则:每日三题

由于周末没有acwing的每日一题,就清一下蓝桥杯辅导课上的练习题
(水题之王)

“如果我今晚就要死去,我微信零钱只有11快。我会选择续费我的apple music付费音乐包,最后听一遍周杰伦的《七里香》”

1.日期问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。
小明知道这些日期都在1960年1月1日至2059年12月31日。
令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。
更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

用函数去简化代码,就很简单了。
注意: 此类的输入使用scanf,比处理字符串简单

#include<iostream>
#include<cstdio>

using namespace std;

int res[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool check(int year,int month,int day){
    if(month == 0||month > 12||day == 0) return false;
    if(month != 2){
        if(day > res[month]) return false;
    }
    else{
        int t;
        if((year%100 != 0&&year % 4 == 0) || year % 400 == 0) t = 1;
        else t = 0;
        if(day > res[month] + t) return false;
    }


    return true;
}


void print(int year,int month,int day){
    printf("%d-",year);
    if(month < 10) cout << '0';
    printf("%d-",month);
    if(day < 10) cout << '0';
    printf("%d\n",day);
}

int main(){
    int a,b,c;
    scanf("%d/%d/%d",&a,&b,&c);

    for(int i = 19600101;i <= 20591231; ++i){

        int year = i / 10000;
        int month = (i / 100) % 100;
        int day = i % 100;

        if(check(year,month,day) == true){

            if(year % 100 == a && month == b && day == c){
                 print(year,month,day);
                 continue;
            }
            if(year % 100 == c && month == a && day == b){
                print(year,month,day);
                continue;
            }
            if(year % 100 == c && month == b && day == a){
                print(year,month,day);
                continue;
            }

        } 

    }

    return 0;   

}

2.回文日期

在日常生活中,通过年、月、日这三个要素可以表示出一个唯一确定的日期。
牛牛习惯用 8位数字表示一个日期,其中,前 4位代表年份,接下来 2位代表月份,最后 2位代表日期。
显然:一个日期只有一种表示方法,而两个不同的日期的表示方法不会相同。
牛牛认为,一个日期是回文的,当且仅当表示这个日期的 8位数字是回文的。
现在,牛牛想知道:在他指定的两个日期之间(包含这两个日期本身),有多少个真实存在的日期是回文的。
一个 8位数字是回文的,当且仅当对于所有的 i(1≤i≤8) 从左向右数的第 i个数字和第 9−i个数字(即从右向左数的第 i个数字)是相同的。

原本和上一个题的思路一样,通过列举八位日期,当满足合理日期的条件下,判断该八位是否回文。
但是判断回文的写法是O(m2)的,加上穷举时间复杂度达到了O(n*m2)就TLE了(7/11)。

转念一下:回文不就可以拿年份反转,去构成新的必定回文的日期。然后这个日期在所求日期内并且合理就答案+1。
这样做的时间复杂度仅O(n%10000)。

#include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

int res[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool check(int year,int month,int day){
    if(month == 0||month > 12||day == 0) return false;
    if(month != 2){
        if(day > res[month]) return false;
    }
    else{
        int t;
        if((year%100 != 0&&year % 4 == 0) || year % 400 == 0) t = 1;
        else t = 0;
        if(day > res[month] + t) return false;
    }

    return true;
}

int handle(int x){    
   int d = 0;
   while(x!=0){
    d = d * 10 + x % 10;
    x = x/10;
   } 
   return d;
}

int main(){
    int a,b,ans = 0;
    cin >> a >> b;
    
    for(int i = a/10000;i <= b/10000; ++i){
        
        int date = i * 10000 + handle(i);
        
        int year = date / 10000;
        int month = (date / 100) % 100;
        int day = date % 100;

        if(check(year,month,day) == true&&date <= b&&date >= a) ans ++;       
    }

    cout << ans << endl;

    return 0;   
    
}

3.错误票据

某涉密单位下发了某种票据,并要在年终全部收回。
每张票据有唯一的ID号。
全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。
因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。
你的任务是通过编程,找出断号的ID和重号的ID。
假设断号不可能发生在最大和最小号。

注意如何输入即可

#include<iostream>
#include<algorithm>

using namespace std;

const int M = 1e5 + 10;

int p[M],cnt = 0;
int n,x;

int main(){
    cin >> n;
    cnt = 0;
    while(cin >> x){
        cnt ++;
        p[cnt] = x;
    }
    
    sort(p + 1, p + cnt + 1);
    int a,b;
    for(int i = 2;i  <= cnt;++i){

        if(p[i] == p[i-1]) b = p[i];
        else if(p[i] >= p[i - 1] + 2) a = p[i] - 1;
        

    }

    cout << a << ' ' << b << endl;

    return 0;

}

4.移动距离

X星球居民小区的楼房全是一样的,并且按矩阵样式排列。
其楼房的编号为 1,2,3…当排满一行时,从下一行相邻的楼往反方向排号。
比如:当小区排号宽度为 6时,开始情形如下:
1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 …
我们的问题是:已知了两个楼号 m和 n,需要求出它们之间的最短移动距离(不能斜线方向移动)

把编号处理成排号和列号就行

#include<iostream>
#include<cmath>

using namespace std;

int m,n,v;
int ans;

typedef pair<int,int> PII;

PII handle(int m){
    PII t;
    t.first = m / v + 1;
    t.second = m % v;

    if(t.second == 0){
        t.first--;
        t.second = v;
    }
    return t;
}

int main(){

    cin >> v >> m >> n;
    
    PII x = handle(m);
    PII y = handle(n);

    if(x.first % 2 == y.first % 2){
        ans = abs(x.first - y.first) + abs(x.second - y.second);
    }
    else ans = abs(x.first - y.first) + abs(v - x.second - y.second + 1);


    cout << ans << endl;
    
    return 0;
    
}
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值