zoukankan      html  css  js  c++  java
  • 【算法题】CCF CSP第二题练习(更新中)

    /*
    试题编号:201912-2
    试题名称:回收站选址
    题目描述:
    通过无人机航拍我们已经知晓了n处尚待清理的垃圾位置,其中第i(1<=i<=n)处的坐标为(xi,yi),保证所有的坐标均为整数。
    我们希望在垃圾集中的地方建立些回收站。具体来说,对于一个位置(x,y)是否适合建立回收站,我们主要考虑以下几点:
    ·(x,y)必须是整数坐标,且该处存在垃圾;
    ·上下左右四个邻居位置,即(x,y+1)、(x,y-1)、(x+1,y)和(x-1,y)处,必须全部存在垃圾;
    ·进一步地,我们会对满足上述两个条件的选址进行评分,分数为不大于4的自然数,表示在(x±1,y±1)四个对角位置中有几处存在垃圾。
    现在,请你统计一下每种得分的选址个数。
    */
    
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <algorithm>
    
    using namespace std; 
    
    class data
    {
    public:
        int x,y,n = -1;
    };
    
    int main()
    {
        int n, x, y, cnt1, cnt2, out[5]{0};
        vector<data> dat;
        cin >> n;
        dat.reserve(n);
        for (int i = 0; i < n; ++i)
        {
            data d;
            cin >> d.x >> d.y;
            dat.push_back(d);
        }
        
        for (auto& i: dat)
        {
            cnt1 = 0;
            cnt2 = 0;
            for (auto j: dat)
            {
                if(i.x == j.x && abs(i.y - j.y) == 1)
                    cnt1++;
                else if(i.y == j.y && abs(i.x - j.x) == 1)
                    cnt1++;
                else if(abs(i.x - j.x) == 1 && abs(i.y - j.y) == 1)
                    cnt2++;
            }
            if (cnt1 == 4)
            {
                i.n == cnt2;
                out[cnt2]++;
            }
        }
        cout << out[0] << endl << out[1] << endl << out[2] << endl << out[3] << endl << out[4];
        return 0;
    }
    /*
    试题编号:    201909-1
    试题名称:    小明种苹果
    题目描述
    小明在他的果园里种了一些苹果树。为了保证苹果的品质,在种植过程中要进行若干轮疏果操作,也就是提前从树上把不好的苹果去掉。
    第一轮疏果操作开始前,小明记录了每棵树上苹果的个数。每轮疏果操作时,小明都记录了从每棵树上去掉的苹果个数。
    在最后一轮疏果操作结束后,请帮助小明统计相关的信息。
    */ #include <iostream> #include <vector> using namespace std; int main() { int turns, treeNum, appleNum, T{0}, D{0}, E{0}; cin >> treeNum; vector<int> drop(treeNum, 0); for (int i = 0; i < treeNum; ++i) { vector<int> vt; cin >> turns; vt.resize(turns+1); vt[0] = turns; for (int j = 1; j <= turns; ++j) cin >> vt[j]; appleNum = vt[1]; for (int k = 2; k <= turns; ++k) { if (vt[k] <= 0) { appleNum += vt[k]; } else { drop[i] += appleNum - vt[k]; appleNum = vt[k]; } } T += appleNum; } for (int i = 0; i < treeNum; ++i) { if (drop[i] > 0) D++; if (drop[i] > 0 && drop[(i+1)%treeNum] > 0 && drop[(i+treeNum-1)%treeNum] > 0) E++; } cout << T << ' ' << D << ' ' << E; return 0; }
    /*
    试题编号:201903-2
    试题名称:二十四点
    【题目描述】
    定义每一个游戏由4个从1-9的数字和3个四则运算符组成,保证四则运算符将数字两两隔开,不存在括号和其他字符,
    运算顺序按照四则运算顺序进行。其中加法用符号+表示,减法用符号-表示,乘法用小写字母x表示,除法用符号/表示。
    在游戏里除法为整除,例如2/3=0,3/2=1,4/2=2。 老师给了你n个游戏的解,请你编写程序验证每个游戏的结果是否为24。
    */ #include <iostream> #include <stack> using namespace std; void culculate(stack<int> &nums, stack<char> &ops, bool tag) { int num2{nums.top()}; nums.pop(); int num1{nums.top()}; nums.pop(); int result; char op{ops.top()}; ops.pop(); switch(op) { case '+': result = num1 + num2; break; case '-': tag ? result = num1 - num2 : result = num2 - num1; break; case 'x': result = num1 * num2; break; case '/': result = num1 / num2; break; } nums.push(result); } inline int priority(char op) { if (op == '+' || op == '-') return 1; else return 2; } int main() { string input; int n, result; stack<char> ops, ops2; stack<int> nums, nums2; cin >> n; for (int i = 0; i < n; ++i) { cin >> input; for (int i = 0; i < 7; ++i) { if (i % 2 == 0) { nums.push(input[i] - '0'); if (!ops.empty() && priority(ops.top()) == 2) culculate(nums, ops, true); } else ops.push(input[i]); } while(!ops.empty()) { ops2.push(ops.top()); ops.pop(); } while(!nums.empty()) { nums2.push(nums.top()); nums.pop(); } while(!ops2.empty()) culculate(nums2, ops2, false); result = nums2.top(); nums2.pop(); if (result == 24) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
  • 相关阅读:
    NBUT 1120 Reimu's Teleport (线段树)
    NBUT 1119 Patchouli's Books (STL应用)
    NBUT 1118 Marisa's Affair (排序统计,水)
    NBUT 1117 Kotiya's Incantation(字符输入处理)
    NBUT 1115 Cirno's Trick (水)
    NBUT 1114 Alice's Puppets(排序统计,水)
    188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
    187 Repeated DNA Sequences 重复的DNA序列
    179 Largest Number 把数组排成最大的数
    174 Dungeon Game 地下城游戏
  • 原文地址:https://www.cnblogs.com/joeyzhao/p/12286337.html
Copyright © 2011-2022 走看看