csp201803

跳一跳

实现

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int res = 0, score = 0;
    int x;
    while (cin >> x && x)
    {
    
    
        if (x == 1) res ++, score = 0;
        else score += 2, res += score;
    }
    cout << res << endl;
    return 0;
}

碰撞的小球

实现

#include<bits/stdc++.h>
using namespace std;
int n,L,T;
const int N = 110;
struct ball
{
    
    
    int p,v;
}a[N];
int main()
{
    
    
    cin>>n>>L>>T;
    for(int i=0;i<n;i++)
    {
    
    
        cin>>a[i].p;
        a[i].v = 1;
    }
    while(T--)
    {
    
    
        for(int i=0;i<n;i++)
        {
    
    
            a[i].p += a[i].v;
            if(a[i].p==L || !a[i].p)
            {
    
    
                a[i].v = -a[i].v;
            }
        }
        for(int i=0;i<n;i++)
        {
    
    
            for(int j=i+1;j<n;j++)
            {
    
    
                if(a[i].p==a[j].p)
                {
    
    
                    a[i].v *= -1;
                    a[j].v *= -1;
                }
            }
        }
    }
    for(int i=0;i<n;i++)
    {
    
    
        cout<<a[i].p<<" ";
    }
}

URL映射

思路

用res数组存储所有的参数,为了区分没有参数和匹配失败的情况,初始化res数组是设置大小为1。匹配失败则将res数组清空返回,匹配成功则加入参数。

实现

来自acwing-yxc:https://www.acwing.com/activity/content/code/content/882106/

#include<bits/stdc++.h>
using namespace std;
const int N = 111;
int n,m;
struct Url
{
    
    
    string path,name;
}url[N];

string get_number(string& str)
{
    
    
    string res;
    for (auto c: str)
        if (c >= '0' && c <= '9')
            res += c;
        else
        {
    
    
            res.clear();
            return res;
        }

    // 去掉前导0
    int k = 0;
    while (k + 1 < res.size() && res[k] == '0') k ++ ;
    return res.substr(k);
}

vector<string> get(string& path,string& str)
{
    
    
    vector<string> res(1);
    int i,j;
    for(i=1,j=1;i<path.size() && j<str.size();)
    {
    
    
        int u = i+1,v = j+1;
        while(u<path.size() && path[u]!='/') u++;
        while(v<str.size() &&str[v]!='/') v++;
        string a = path.substr(i,u-i),b = str.substr(j,v-j);
        if(a=="<str>")
        {
    
    
            res.push_back(b);
            i = u+1,j = v+1;
        }
        else if(a=="<int>")
        {
    
    
            auto t = get_number(b);
            if(t.empty())
            {
    
    
                res.clear();
                return res;
            }
            res.push_back(t);
            i = u+1,j = v+1;
        }
        else if(a=="<path>")
        {
    
    
            res.push_back(str.substr(j));
            return res;
        }
        else if(a!=b)
        {
    
    
            res.clear();
            return res;
        }
        else i=u+1,j=v+1;
        
    }
    if (i - path.size() != j - str.size()) res.clear();
    return res;
}
void work(string& str)
{
    
    
    for(int i=0;i<n;i++)
    {
    
    
        auto res = get(url[i].path,str);
        if(res.size())
        {
    
    
            cout<<url[i].name;
            for(int j=1;j<res.size();j++)
            cout<<" "<<res[j];
            
            cout<<endl;
            return ;
        }
    }
    puts("404");
}
int main()
{
    
    
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
    
    
        cin>>url[i].path>>url[i].name;
    }
    while(m--)
    {
    
    
        string str;
        cin>>str;
        work(str);
    }
}

欢迎指正-

猜你喜欢

转载自blog.csdn.net/Tracy_yi/article/details/129198962