模拟、排序(归并排序)算法

本文介绍了模拟和排序算法中的两个具体问题:错误票据的检查和回文日期的判断,分别提供了暴力求解和优化解法,并展示了归并排序的基本思路和题解。
摘要由CSDN通过智能技术生成

一、模拟

例题

1、错误票据

题目信息

在这里插入图片描述

思路

先对数组进行排序,然后遍历数组,如果出现两个一样的,就是重号,如果连续的两个数之间相差大于1就是断号

题解
#include <bits/stdc++.h>
#define int long long 
#define endl '\n'
#define maxsize 10010
using namespace std;

int n;
int a[maxsize];

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    int m=0;
    string line;
    stringstream ss;
    getline(cin,line);   //因为getline可以读入换行,消除前面一个输入的换行
    for(int i=0;i<n;i++)
    {
        getline(cin,line);
        ss<<line;
        while(ss>>a[m]) m++;
        ss.clear();
    }
    sort(a,a+m);
    int res1,res2;
    for(int i=1;i<m;i++)
    {
        if(a[i]==a[i-1]) res1=a[i];
        if(a[i]-a[i-1]>1) res2=a[i]-1;
    }
    cout<<res2<<" "<<res1<<endl;
    return 0;
}

stingstream的用法可以参考以下文章:
stringstream用法总结

2、回文日期

题目信息

在这里插入图片描述

思路
方法一:暴力做法

枚举date1到date2的所有数字:
(1)判断是否是回文数
(2)判断这个回文数是否符合日期

方法二:优化解法

(1)枚举回文数
只要枚举前4个,后4个将前4个颠倒即可形成一个回文数
所以枚举的范围为1000~9999
(2)判断数字是否在范围中
判断形成的回文数是否在刚刚输入的两个日期中
(3)判断是否符合日期

题解
方法一:暴力求解
#include <bits/stdc++.h>
#define int long long 
#define endl '\n'
using namespace std;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string date1,date2;
    cin>>date1>>date2;
    stringstream ss1,ss2;
    int d1,d2;
    ss1<<date1;
    ss2<<date2;
    ss1>>d1;
    ss2>>d2;
    int count=0;
    for(int i=d1;i<=d2;i++)
    {
        int tmp=i;
        int res=0;
        while(tmp>0)
        {
            res=res*10+tmp%10;
            tmp /=10;
        }
        if(i==res){
            int date=i%100;
            int month =(i%10000)/100;
            int year=i/10000;
            if((year%4==0&&year%100!=0)||year%400==0)
            {
                if(month==2)
                {
                    if(date>=1&&date<=29) count++;
                }
            }else{
                if(month==2)
                {
                    if(date>=1&&date<=28) count++;
                }
            }
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                if(date>=1&&date<=31) count++;
            }else if(month==4||month==6||month==9||month==11)
            {
                if(date>=1&&date<=30) count++;
            }
        }
    }
    cout<<count<<endl;
    return 0;
}
方法二:优化解法
#include <bits/stdc++.h>
#define int long long 
#define endl '\n'
using namespace std;

//优化解法
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string date1,date2;
    stringstream ss1,ss2;
    int d1,d2;
    cin>>date1>>date2;
    ss1<<date1;
    ss2<<date2;
    ss1>>d1;
    ss2>>d2;
    int count=0;
    for(int i=1000;i<=9999;i++)
    {
        int tmp=i;
        int res=i;
        while(tmp>0)
        {
            res=res*10+tmp%10;
            tmp /=10;
        }
        if(res>=d1&&res<=d2)
        {
            int date=res%100;
            int month =(res%10000)/100;
            int year=res/10000;
            if((year%4==0&&year%100!=0)||year%400==0)
            {
                if(month==2)
                {
                    if(date>=1&&date<=29) count++;
                }
            }else{
                if(month==2)
                {
                    if(date>=1&&date<=28) count++;
                }
            }
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                if(date>=1&&date<=31) count++;
            }else if(month==4||month==6||month==9||month==11)
            {
                if(date>=1&&date<=30) count++;
            }
        }
    }
    cout<<count<<endl;
    return 0;
}

二、排序

例题

1、归并排序

题目信息
思路
题解
  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值