poj.org 部分答案(二)

2336

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <cstdio>
class Solution {
public:
    Solution() {
        std::cin >> n;
        number1 = new int[n];
        for (int i = 0; i < n; i++) scanf("%d", &number1[i]);
        std::cin >> m;
        number2 = new int[m];
        for (int i = 0; i < m; i++)  scanf("%d", &number2[i]);
    }
    int binarySearch(int arr[], int target, int left, int right) {
        if (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                return 1;
            }
            else if (arr[mid] < target) {
                return binarySearch(arr, target, mid + 1, right); // 修正更新边界值
            }
            else {
                return binarySearch(arr, target, left, mid - 1); // 修正更新边界值
            }
        }
        return 0;
    }
    void solve() {
        bool found = false; // 添加一个变量来标记是否找到匹配的数字对
        for (int i = 0; i < m; i++) {
            if (binarySearch(number1, 10000 - number2[i], 0, n - 1)) {
                std::cout << "YES"; // 添加换行符
                found = true; // 标记找到匹配的数字对
                break; // 找到即可跳出循环
            }
        }
        if (!found) {
            std::cout << "NO"; // 如果没有找到匹配的数字对,输出 "NO"
        }
    }
    int n, m;
    int* number1, * number2;
};

int main()
{
    Solution a;
    a.solve();
    return 0;
}

2503

cpp 复制代码
#include <iostream>
#include <cstring>
#include <string>
#include <map>

class Solution {
private:
    std::map<std::string, std::string> dictionary;

    // 解析输入行并更新字典的私有辅助函数
    void parseAndUpdateDictionary(const std::string& str) {
        std::string str1, str2;

        for (size_t i = 0; i < str.size(); i++) {
            if (str[i] == ' ') {
                str1 = str.substr(0, i);
                str2 = str.substr(i + 1);
                break;
            }
        }

        dictionary[str2] = str1;
    }

public:
    // 构造函数,初始化字典
    Solution() {}

    // 读取字典输入并初始化字典的公有方法
    void readDictionary() {
        std::string str;

        while (std::getline(std::cin, str) && !str.empty()) {
            parseAndUpdateDictionary(str);
        }
    }

    // 翻译外语单词的公有方法
    void translateWords() {
        std::string word;

        while (std::cin >> word) {
            if (!dictionary[word].empty()) {
                std::cout << dictionary[word] << std::endl;
            }
            else {
                std::cout << "eh\n";
            }
        }
    }
};

int main() {
    // 创建 Solution 对象
    Solution solution;

    // 读取字典输入并初始化字典
    solution.readDictionary();

    // 翻译外语单词
    solution.translateWords();

    return 0;
}
相关推荐
Ljubim.te17 分钟前
软件设计师——数据结构
数据结构·笔记
Eric.Lee202124 分钟前
数据集-目标检测系列- 螃蟹 检测数据集 crab >> DataBall
python·深度学习·算法·目标检测·计算机视觉·数据集·螃蟹检测
黑不溜秋的25 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光30 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
林辞忧33 分钟前
算法修炼之路之滑动窗口
算法
￴ㅤ￴￴ㅤ9527超级帅44 分钟前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
liuyang-neu1 小时前
力扣 简单 110.平衡二叉树
java·算法·leetcode·深度优先
penguin_bark1 小时前
LCR 068. 搜索插入位置
算法·leetcode·职场和发展
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
ROBIN__dyc1 小时前
表达式
算法