ccf认证题目练习

20191201

在这里插入图片描述

#include "iostream"
using namespace std;
bool is_seven(int n){
    
    
    if(n%7==0) return 1;
    while(n){
    
    
        if(n%10==7) return 1;
        n/=10;
    }
    return 0;
}
int main(int argc, char* argv[])
{
    
    
    int n;
    cin>>n;
    int now = 1;
    int num_a=0,num_b=0,num_c=0,num_d=0;
    int num = 1;
    while(n){
    
    
        switch(now){
    
    
            case 1:
                if(is_seven(num)){
    
    
                    num_a++;
                }else{
    
    
                    n--;
                }
                break;
            case 2:
                if(is_seven(num)){
    
    
                    num_b++;
                }else{
    
    
                    n--;
                }
                break;
            case 3:
                if(is_seven(num)){
    
    
                    num_c++;
                }else{
    
    
                    n--;
                }
                break;
            case 4:
                if(is_seven(num)){
    
    
                    num_d++;
                }else{
    
    
                    n--;
                }
                break;
        }
        now++;
        num++;
        if(now == 5) now =1;
    }
    cout<<num_a<<endl<<num_b<<endl<<num_c<<endl<<num_d;
	return 0;
}

20191202

在这里插入图片描述
在这里插入图片描述
参考了https://blog.csdn.net/richenyunqi/article/details/103988931

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
// 由于unordered_map是采用哈希实现的,对于系统的类型int, string等,都已经定义好了hash函数,
// 所以如果我们引入新的自定义类型的话,系统并不知道如何去计算我们引入的自定义类型的hash值,
// 所以我们就需要自己定义hash函数,告诉系统用这种方式去计算我们引入的自定义类型的hash值
struct arrayHash
{
    
    
//我试过return 0,也AC了,是不是用例中没产生冲突呢?
    LL operator()(const array<LL, 2> &p) const {
    
     return p[0] * 1e9 + p[1]; }
};

int main()
{
    
    
    /*
    用map存储整个棋盘的所有棋子
    @array<LL,2> 用一个二维数组表示棋子横纵坐标
    @array<LL,2> 用一个二维数组表示棋子以下信息:
    @@LL 表示该棋子上下左右相邻点个数
    @@LL 表示该棋子对角线相邻点个数
    @array_hash 数组做为key的哈希函数
    */
    unordered_map<array<LL, 2>, array<LL, 2>, arrayHash> um;
    LL n;
    cin >> n;
    while (n--)
    {
    
    
        array<LL, 2> p;
        cin >> p[0] >> p[1];
        um.insert({
    
    p, {
    
    0, 0}}); //存储当前棋子
        for (auto &elemnent : um)
        {
    
    
            auto &q = elemnent.first; //q是每个元素的别名
            if ((q[0] == p[0] and abs(q[1] - p[1]) == 1) or (q[1] == p[1] and abs(q[0] - p[0]) == 1))
            {
    
    
                um[p][0]++;
                um[q][0]++;
                //上下左右的棋子数目加一
            }
            else if (abs(q[0] - p[0]) == 1 and abs(q[1] - p[1]) == 1)
            {
    
    
                um[p][1]++;
                um[q][1]++;
                //斜对角棋子数目加一
            }
        }
    }
    array<LL, 5> ans{
    
    };
    for (auto &element : um)
    {
    
    
        auto &nums = element.second;
        if (nums[0] == 4)
        {
    
    
            ans[nums[1]]++;
        }
    }
    for (auto nums : ans)
    {
    
    
        cout << nums << endl;
    }
    return 0;
}

20191203

水了50分

删了

测试用例:

8
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
4Zn(NO3)2+NH4NO3+3H2O=4Zn+10HNO3
CaCl2+2AgNO3=Ca(NO3)2+2AgCl

上面写的太乱了,重做了下,结果是超时,60分
是因为string用太多才超时吗?

#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int match_num(string s, int &pos)
{
    
     //匹配数组 返回数值 pos存储当前指针
    string num = "";
    while (isdigit(s[pos]))
    {
    
    
        num += s[pos];
        pos++;
    }
    return atoi(num.c_str());
}
string match_element(string s, int &pos)
{
    
    
    string ele = "";
    if (s[pos] >= 'A' and s[pos] <= 'Z')
    {
    
    
        ele += s[pos];
        pos++;
        if (s[pos] >= 'a' and s[pos] <= 'z')
        {
    
    
            ele += s[pos];
            pos++;
        }
    }
    return ele;
}

void sum(string s)
{
    
    
    int p = 0;
    int times = 1;
    //处理单个单词的函数
    //1.获取首字母的倍数
    if (isdigit(s[0]))
    {
    
    
        times = match_num(s, p);
    }
    //2.开始匹配
    string last;
    while (s[p])
    {
    
    
        if (isalpha(s[p]))
        {
    
    
            string ele = match_element(s, p);
            last = ele;
            mp[ele] += times;
        }
        else if (isdigit(s[p]))
        {
    
    
            int num = match_num(s, p);
            mp[last] -= times;
            mp[last] += times * num;
        }
        else if (s[p] == '(')
        {
    
    
            //假设括号嵌套 不会出现(OP)(OH)这样的情况
            int t = s.find_last_of(')') + 1;
            int tmp_t = t; //t值会变,保存下
            int num = match_num(s, t);
            num *= times;
            stringstream ss;
            ss << num;
            string tmp = ss.str();
            for (int i = p + 1; i < tmp_t - 1; i++)
            {
    
    
                tmp += s[i];
            }
            sum(tmp);
            p = t;
        }
    }
}
void sub(string s)
{
    
    
    int p = 0;
    int times = 1;
    //处理单个单词的函数
    //1.获取首字母的倍数
    if (isdigit(s[0]))
    {
    
    
        times = match_num(s, p);
    }
    //2.开始匹配
    string last;
    while (s[p])
    {
    
    
        if (isalpha(s[p]))
        {
    
    
            string ele = match_element(s, p);
            last = ele;
            mp[ele] -= times;
        }
        else if (isdigit(s[p]))
        {
    
    
            int num = match_num(s, p);
            mp[last] -= times * num;
            mp[last] += times;
        }
        else if (s[p] == '(')
        {
    
    
            //假设括号嵌套 不会出现(OP)(OH)这样的情况
            int t = s.find_last_of(')') + 1;
            int tmp_t = t; //t值会变,保存下
            int num = match_num(s, t);
            num *= times;
            stringstream ss;
            ss << num;
            string tmp = ss.str();
            for (int i = p + 1; i < tmp_t - 1; i++)
            {
    
    
                tmp += s[i];
            }
            sub(tmp);
            p = t;
        }
    }
}
void check(string s, void (*func)(string))
{
    
    
    if (s.size() == 0)
        return;
    string one = "";
    int p = 0;
    while (s[p] != '+')
    {
    
    
        one += s[p++];
        if (p == s.size())
        {
    
    
            func(one);
            return;
        }
    }
    func(one);
    check(s.substr(p + 1, s.size()), func);
}
int main()
{
    
    
    int n;
    cin >> n;
    cin.get();
    while (n--)
    {
    
    
        string s;
        getline(cin, s);
        auto p = s.find('=');
        check(s.substr(0, p), sum);
        // for(auto i : mp){
    
    
        //     cout<<i.first<<" "<<i.second<<endl;
        // }
        check(s.substr(p + 1, s.size()), sub);
        //  for(auto i : mp){
    
    
        //     cout<<i.first<<" "<<i.second<<endl;
        // }
        bool f = true;
        for (auto i : mp)
        {
    
    
            if (i.second != 0)
            {
    
    
                f = false;
                break;
            }
        }
        mp.clear();
        if (f == false)
        {
    
    
            cout << "N" << endl;
        }
        else
        {
    
    
            cout << "Y" << endl;
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Protocols7/article/details/104105459