ccf刷题记终极版03

这次明显没有上次那么顺利了,由于第三题大模拟居然被卡住了,导致整个心态崩了,完全没有心思去做四五问了,然事后分析,才发现原来只错了一个小小的函数,真的不是什么大问题,由此可见心态很重要啊,沉住气

201403-1 相反数

思路:水题,习惯了自己写个abs函数,然后强行统计一下就好了

#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 = 1e5+10;

bool ad[maxn];

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

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(ad,false,sizeof(ad));
        int num = 0;
        while(n--)
        {
            int temp;
            scanf("%d",&temp);
            if(ad[myabs(temp)])
            {
                num++;
            }
            else
            {
                ad[myabs(temp)] = true;
            }
        }
        printf("%d\n",num);
    }
    return 0;
}


201403-2 窗口

思路:第一眼看到的时候,感觉有点像贪心或者dp什么很复杂的问题,毕竟覆盖问题有很多这种的,然细心读题之后就会发现其实并没有这么复杂,由于判断点是否在已知对角坐标的矩形内部是一件很轻松的事,那么直接存储矩形的顺序,然后按顺序暴力判断一下就好了

#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 = 110;

typedef struct POS
{
    int id;
    int x1;
    int x2;
    int y1;
    int y2;
}Pos;

Pos pos[maxn];

int main()
{
    int n,m;
    while(cin >> n >> m)
    {
        for(int i=1;i<=n;i++)
        {
            cin >> pos[n-i].x1 >> pos[n-i].y1 >> pos[n-i].x2 >> pos[n-i].y2;
            pos[n-i].id = i;
        }
        while(m--)
        {
            int x,y;
            cin >> x >> y;
            int now = 0;
            for(;now<n;now++)
            {
                if(   x >= pos[now].x1
                   && x <= pos[now].x2
                   && (
                        (y >= pos[now].y1 && y <= pos[now].y2)
                    ||  (y >= pos[now].y2 && y <= pos[now].y1)
                       )
                   )
                {
                    break;
                }
            }
            if(now == n)
            {
                cout << "IGNORED" << endl;
            }
            else
            {
                Pos re = pos[now];
                cout << re.id << endl;
                for(int i=now;i>0;i--)
                {
                    pos[i] = pos[i-1];
                }
                pos[0] = re;
            }
        }
    }
    return 0;
}


201403-3 命令行参数

思路:这题其实到也还挺好上手的,典型的读取字符串+判断,注意一下命令的单字母格式很容易被忽视

而这次卡了我整场的居然是一个小函数的使用,substr第二个参数传的不是结束点坐标,而是串长,就很难受

#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 = 30;

bool in [maxn];
bool contain[maxn];

map<char,string> re;

string s;

/*bool legal(char a)
{
    if((a>='0'&&a<='9')||(a>='a'&&a<='z')||(a=='-'))
    {
        return true;
    }
    else
    {
        return false;
    }
}*/

int main()
{
    while(cin >> s)
    {
        memset(in,false,sizeof in);
        memset(contain,false,sizeof contain);
        int len = s.length();
        for(int i=0;i<len-1;i++)
        {
            in[s[i] - 'a'] = true;
            if(s[i+1] == ':')
            {
                contain[s[i]-'a'] = true;
                i++;
            }
        }
        if(s[len-1] != ':')
        {
            in[s[len-1]-'a'] = true;
        }
        int n;
        cin >> n;
        getchar();
        for(int i=1;i<=n;i++)
        {
            re.clear();
            string temp;
            getline(cin,temp);
            len = temp.length();
            int pos=0;
            while(pos<len&&temp[pos]!=' ')
            {
                pos++;
            }
            pos++;
            while(pos<len)
            {
                if(temp[pos] == '-')
                {
                    pos++;
                    if(pos>=len)
                    {
                        break;
                    }
                    char now = temp[pos];
                    if(now>'z'||now<'a')
                    {
                        break;
                    }
                    pos++;
                    if(pos>len)
                    {
                        break;
                    }
                    if(pos<len&&temp[pos]!=' ')
                    {
                        break;
                    }
                    if(in[now-'a'])
                    {
                        if(contain[now-'a'])
                        {
                            pos++;
                            int pos2 = pos;
                            //bool ok = true;
                            for(;pos2<len;pos2++)
                            {
                                if(temp[pos2] == ' ')
                                {
                                    break;
                                }
                               /* if(!legal(temp[pos2]))
                                {
                                    ok = false;
                                    break;
                                }*/
                            }
                            /*if(!ok)
                            {
                                break;
                            }*/
                            pos2--;
                            if(pos2<pos)
                            {
                                break;
                            }
                            string data = temp.substr(pos,pos2-pos+1);
                            re[now] = data;
                            pos = pos2+2;
                            continue;
                        }
                        else
                        {
                            re[now] = "*";
                            pos++;
                            continue;
                        }
                    }
                }
                break;
            }
            cout << "Case " << i << ":";
            map<char,string> :: iterator it;
            for(it = re.begin();it!=re.end();it++)
            {
                if(it->second == "*")
                {
                    cout << " -" << it->first;
                }
                else
                {
                    cout << " -" << it->first << " " << it->second;
                }
            }
            cout << endl;
        }
    }
    return 0;
}
/*
substr参数输错了
*/


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

猜你喜欢

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