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;
}
相关推荐
仙俊红1 小时前
LeetCode487周赛T2,删除子数组后的最终元素
数据结构·算法
-dzk-7 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
风筝在晴天搁浅7 小时前
hot100 78.子集
java·算法
Jasmine_llq7 小时前
《P4587 [FJOI2016] 神秘数》
算法·倍增思想·稀疏表(st 表)·前缀和数组(解决静态区间和查询·st表核心实现高效预处理和查询·预处理优化(提前计算所需信息·快速io提升大规模数据读写效率
超级大只老咪8 小时前
快速进制转换
笔记·算法
m0_706653238 小时前
C++编译期数组操作
开发语言·c++·算法
故事和你918 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
qq_423233908 小时前
C++与Python混合编程实战
开发语言·c++·算法
TracyCoder1239 小时前
LeetCode Hot100(19/100)——206. 反转链表
算法·leetcode
m0_715575349 小时前
分布式任务调度系统
开发语言·c++·算法