ccf刷题记终极版05

长时间未做题,代码能力下降已经成了不争的事实了,不知不觉,在一波忙碌以后,ccf和c4选拔都已经落下帷幕了

赶紧补一波赛前最后一场练习吧,就是去年12月的一场,不知为何,做练习的时候,感觉状态要比考试的时候好很多

 201712-1 最小差值

思路:这道就是完全的水题了,寻找最小差值,遍历一边即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 1e3+10;

int a[maxn];

int myabs(int k)
{
    if(k < 0)
    {
         k *= -1;
    }
    return k;
}

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
    }
    sort(a,a+n);
    int di = 1e5;
    for(int i=1;i<n;i++)
    {
        int temp = myabs(a[i]-a[i-1]);
        if(temp < di)
        {
            di = temp;
        }
    }
    cout << di << endl;
    return 0;
}


201712-2 游戏

思路:报数游戏,这题作为第二题,确实难度也也有点低了,起码比这次低多了,一个纯模拟直接解决

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 1e3+10;

bool in[maxn];

int main()
{
    int n,k,num;
    cin >> n >> k;
    memset(in,true,sizeof in);
    num = n;
    int now = 1;
    int pos = 1;
    while(num>1)
    {
        if(now%10==k||now%k==0)
        {
            in[pos] = false;
            //cout << pos << "*" << endl;
            num--;
        }
        pos++;
        now++;
        if(pos>n)
        {
            pos = 1;
        }
        while(!in[pos])
        {
            pos++;
            if(pos > n)
            {
                pos = 1;
            }
        }
    }
    cout << pos <<endl;
    return 0;
}

201712-3 Crontab

思路:时间表的判断,这题其实坑还是蛮多的,但感觉其实大多都是自己写出来的bug,像上面break可以直接跳出for的i++而continue却不能这些的,日期必须存成字符串类型而不能是int型,以及一些小细节,不过最终自行debug出300分还是算勉强接受吧,有没有看后两题,这个……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 20;

typedef struct COMMAND
{
    bool mm[60];
    bool hh[24];
    bool dd[32];
    bool mo[13];
    bool we[7];
    string co;
}Command;

Command command[maxn];

map <string,int> simm;
map <string,int> simw;

int n;
int minute,hour,day,month,year,week;
int smm,shh,sdd,smo,sye;
int emm,ehh,edd,emo,eye;

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

bool runyear(int y)
{
    if(y%400==0||(y%4==0&&y%100!=0))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void task()
{
    char nowtime[12];
    nowtime[0] = '0' + year/1000;
    nowtime[1] = '0' + year/100%10;
    nowtime[2] = '0' + year/10%10;
    nowtime[3] = '0' + year%10;
    nowtime[4] = '0' + month/10;
    nowtime[5] = '0' + month%10;
    nowtime[6] = '0' + day/10;
    nowtime[7] = '0' + day%10;
    nowtime[8] = '0' + hour/10;
    nowtime[9] = '0' + hour%10;
    nowtime[10] = '0' + minute/10;
    nowtime[11] = '0' + minute%10;

    for(int i=0;i<n;i++)
    {
        /*if(day == 18&&hour == 23 && minute == 30)
        {
             if(command[1].we[6])
            {
                cout << "yes" << endl;
            }
            else
            {
                cout << "no" << endl;
            }
            cout << week << "^&^" << endl;;
            cout << i << command[i].mm[minute] << command[i].hh[hour] << command[i].dd[day] << command[i].mo[month] <<command[i].we[week] << endl;
        }*/
        if(command[i].mm[minute]&&command[i].hh[hour]&&command[i].dd[day]&&command[i].mo[month]&&command[i].we[week])
        {
            for(int j=0;j<12;j++)
            {
                cout << nowtime[j];
            }
            cout << " " << command[i].co << endl;
            //exit(0);
        }
    }

    minute++;
    if(minute == 60)
    {
        minute = 0;
        hour++;
        if(hour == 24)
        {
            hour = 0;
            day++;
            if(day > stamon[month])
            {
                day = 1;
                month++;
                if(month > 12)
                {
                    month = 1;
                    year++;
                    if(runyear(year))
                    {
                        stamon[2] = 29;
                    }
                    else
                    {
                        stamon[2] = 28;
                    }
                }
            }
            week++;
            if(week == 7)
            {
                week = 0;
            }
            //cout << day << "$" << week << endl;
        }
    }
    return ;
}

int main()
{
    ios::sync_with_stdio(false);
    string s,t;
    cin >> n >> s >> t;
    simm.clear();
    simw.clear();
    simm["jan"] = 1;
    simm["feb"] = 2;
    simm["mar"] = 3;
    simm["apr"] = 4;
    simm["may"] = 5;
    simm["jun"] = 6;
    simm["jul"] = 7;
    simm["aug"] = 8;
    simm["sep"] = 9;
    simm["oct"] = 10;
    simm["nov"] = 11;
    simm["dec"] = 12;
    simw["sun"] = 0;
    simw["mon"] = 1;
    simw["tue"] = 2;
    simw["wed"] = 3;
    simw["thu"] = 4;
    simw["fri"] = 5;
    simw["sat"] = 6;
    for(int i=0;i<n;i++)
    {
        //initial
        memset(command[i].mm,false,sizeof command[i].mm);
        memset(command[i].hh,false,sizeof command[i].hh);
        memset(command[i].dd,false,sizeof command[i].dd);
        memset(command[i].mo,false,sizeof command[i].mo);
        memset(command[i].we,false,sizeof command[i].we);
        string temp;
        int len;
        int pos;
        int pos2;
        bool contin;

        //minutes
        cin >> temp;
        len = temp.length();
        pos = 0;
        contin = false;
        if(temp == "*")
        {
            memset(command[i].mm,true,sizeof command[i].mm);
        }
        else
        {
            for(int j=0;j<len;j++)
            {
                if(temp[j]>='0'&&temp[j]<='9')
                {
                    pos = pos * 10 + temp[j] - '0';
                }
                else if(temp[j] == ',')
                {
                    if(!contin)
                    {
                        command[i].mm[pos] = true;
                        pos = 0;
                    }
                    else
                    {
                        contin = false;
                        for(int k=pos2;k<=pos;k++)
                        {
                            command[i].mm[k] = true;
                        }
                        pos = 0;
                    }
                }
                else if(temp[j] == '-')
                {
                    contin = true;
                    pos2 = pos;
                    pos = 0;
                }
            }
            if(!contin)
            {
                command[i].mm[pos] = true;
            }
            else
            {
                contin = false;
                for(int k=pos2;k<=pos;k++)
                {
                    command[i].mm[k] = true;
                }
            }
        }

        //hours
        cin >> temp;
        len = temp.length();
        pos = 0;
        contin = false;
        if(temp == "*")
        {
            memset(command[i].hh,true,sizeof command[i].hh);
        }
        else
        {
            for(int j=0;j<len;j++)
            {
                if(temp[j]>='0'&&temp[j]<='9')
                {
                    pos = pos * 10 + temp[j] - '0';
                }
                else if(temp[j] == ',')
                {
                    if(!contin)
                    {
                        command[i].hh[pos] = true;
                        pos = 0;
                    }
                    else
                    {
                        contin = false;
                        for(int k=pos2;k<=pos;k++)
                        {
                            command[i].hh[k] = true;
                        }
                        pos = 0;
                    }
                }
                else if(temp[j] == '-')
                {
                    contin = true;
                    pos2 = pos;
                    pos = 0;
                }
            }
            if(!contin)
            {
                command[i].hh[pos] = true;
            }
            else
            {
                contin = false;
                for(int k=pos2;k<=pos;k++)
                {
                    command[i].hh[k] = true;
                }
            }
        }

        //day
        cin >> temp;
        len = temp.length();
        pos = 0;
        contin = false;
        if(temp == "*")
        {
            memset(command[i].dd,true,sizeof command[i].dd);
        }
        else
        {
            for(int j=0;j<len;j++)
            {
                if(temp[j]>='0'&&temp[j]<='9')
                {
                    pos = pos * 10 + temp[j] - '0';
                }
                else if(temp[j] == ',')
                {
                    if(!contin)
                    {
                        command[i].dd[pos] = true;
                        pos = 0;
                    }
                    else
                    {
                        contin = false;
                        for(int k=pos2;k<=pos;k++)
                        {
                            command[i].dd[k] = true;
                        }
                        pos = 0;
                    }
                }
                else if(temp[j] == '-')
                {
                    contin = true;
                    pos2 = pos;
                    pos = 0;
                }
            }
            if(!contin)
            {
                command[i].dd[pos] = true;
            }
            else
            {
                contin = false;
                for(int k=pos2;k<=pos;k++)
                {
                    command[i].dd[k] = true;
                }
            }
        }

        // month
        cin >> temp;
        len = temp.length();
        pos = 0;
        contin = false;
        if(temp == "*")
        {
            memset(command[i].mo,true,sizeof command[i].mo);
        }
        else
        {
            for(int j=0;j<len;j++)
            {
                if((temp[j]>='a'&&temp[j]<='z')||(temp[j]>='A'&&temp[j]<='Z'))
                {
                    string now;
                    for(int jj=0;jj<3;jj++)
                    {
                        if(temp[j]>='A'&&temp[j]<='Z')
                        {
                            now += temp[j] - 'A' + 'a';
                        }
                        else
                        {
                            now += temp[j];
                        }
                        j++;
                    }
                    pos = simm[now];
                    j--;
                    continue;
                }
                if(temp[j]>='0'&&temp[j]<='9')
                {
                    pos = pos * 10 + temp[j] - '0';
                }
                else if(temp[j] == ',')
                {
                    if(!contin)
                    {
                        command[i].mo[pos] = true;
                        pos = 0;
                    }
                    else
                    {
                        contin = false;
                        for(int k=pos2;k<=pos;k++)
                        {
                            command[i].mo[k] = true;
                        }
                        pos = 0;
                    }
                }
                else if(temp[j] == '-')
                {
                    contin = true;
                    pos2 = pos;
                    pos = 0;
                }
            }
            if(!contin)
            {
                command[i].mo[pos] = true;
            }
            else
            {
                contin = false;
                for(int k=pos2;k<=pos;k++)
                {
                    command[i].mo[k] = true;
                }
            }
        }

        //week
        cin >> temp;
        len = temp.length();
        pos = 0;
        contin = false;
        if(temp == "*")
        {
            memset(command[i].we,true,sizeof command[i].we);
        }
        else
        {
            for(int j=0;j<len;j++)
            {
                //cout << temp[j] <<"##"<<endl;
                if((temp[j]>='a'&&temp[j]<='z')||(temp[j]>='A'&&temp[j]<='Z'))
                {
                    string now;
                    for(int jj=0;jj<3;jj++)
                    {
                        if(temp[j]>='A'&&temp[j]<='Z')
                        {
                            now += temp[j] - 'A' + 'a';
                        }
                        else
                        {
                            now += temp[j];
                        }
                        j++;
                    }
                    pos = simw[now];
                    //cout << now << pos <<temp[j] << contin<< endl;
                    j--;
                    continue;
                }
                if(temp[j]>='0'&&temp[j]<='9')
                {
                    pos = pos * 10 + temp[j] - '0';
                }
                else if(temp[j] == ',')
                {
                    if(!contin)
                    {
                        command[i].we[pos] = true;
                       // cout << "***" << pos << endl;
                        pos = 0;
                    }
                    else
                    {
                        contin = false;
                        for(int k=pos2;k<=pos;k++)
                        {
                            command[i].we[k] = true;
                      //      cout << "***" << k << endl;
                        }
                        pos = 0;
                    }
                }
                else if(temp[j] == '-')
                {
                    contin = true;
                    pos2 = pos;
                    pos = 0;
                }
            }
            if(!contin)
            {
                command[i].we[pos] = true;
                //cout << "***" << pos << endl;
            }
            else
            {
                contin = false;
                for(int k=pos2;k<=pos;k++)
                {
                    command[i].we[k] = true;
                  //  cout << "***" << k << endl;
                }
            }
        }

        //command
        cin >> command[i].co;
    }

    smm = (int)(s[10] - '0')*10 + (int)(s[11] - '0');
    shh = (int)(s[8] - '0')*10 + (int)(s[9] - '0');
    sdd = (int)(s[6] - '0')*10 + (int)(s[7] - '0');
    smo = (int)(s[4] - '0')*10 + (int)(s[5] - '0');
    sye = (int)(s[0] - '0')*1000 + (int)(s[1] - '0')*100 + (int)(s[2] - '0')*10 + (int)(s[3] - '0');;

    emm = (int)(t[10] - '0')*10 + (int)(t[11] - '0');
    ehh = (int)(t[8] - '0')*10 + (int)(t[9] - '0');
    edd = (int)(t[6] - '0')*10 + (int)(t[7] - '0');
    emo = (int)(t[4] - '0')*10 + (int)(t[5] - '0');
    eye = (int)(t[0] - '0')*1000 + (int)(t[1] - '0')*100 + (int)(t[2] - '0')*10 + (int)(t[3] - '0');;


    minute = smm;
    hour = shh;
    day = sdd;
    month = smo;
    year = sye;

    int testday = 1;
    int testmonth = 1;
    int testyear = 1970;
    if(runyear(testyear))
    {
        stamon[2] = 29;
    }
    else
    {
        stamon[2] = 28;
    }
    week = 4;
    while(!(testyear==year&&testmonth==month&&testday==day))
    {
        testday++;
        if(testday > stamon[testmonth])
        {
            testday = 1;
            testmonth++;
            if(testmonth>12)
            {
                testmonth = 1;
                testyear++;
                if(runyear(testyear))
                {
                    stamon[2] = 29;
                }
                else
                {
                    stamon[2] = 28;
                }
            }
        }
        week++;
        if(week == 7)
        {
            week = 0;
        }
    }

    if(runyear(year))
    {
        stamon[2] = 29;
    }
    else
    {
        stamon[2] = 28;
    }
/*
cout << "***" << endl;
return 0;
*/
    //cout << sye << "-" << smo << "-" << sdd << " " << shh << ":" << smm << "*" << week << endl;
    //cout << eye << "-" << emo << "-" << edd << " " << ehh << ":" << emm << endl;
    while(   year <  eye ||
            (year == eye && (month <  emo ||
                            (month == emo && (day <  edd ||
                                             (day == edd && (hour <  ehh ||
                                                            (hour == ehh && minute < emm)
                                                             )
                                              )
                                              )
                             )
                             )
            )
         )
    {
        task();
    }
//cout << week << "#" << endl;
    return 0;
}

/*
3 201711170032 201711222352
0 7 * * 1,3-5 get_up
30 23 * * Sat,Sun go_to_bed
15 12,18 * * * have_dinner
*/

/*字符串读取整数*/
/*未读入命令*/
/*未计算星期*/
/*星期计算错误*/
/*for循环break会执行++*/


发布了97 篇原创文章 · 获赞 89 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Owen_Q/article/details/79616342