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;
}
相关推荐
wu_ye_m8 分钟前
学习c语言第34天 用函数每次输出+1,链式访问,int和void
c语言·学习·算法
Lucky_ldy9 分钟前
数据结构从入门到精通:链表的分类
数据结构·链表
牢姐与蒯14 分钟前
c++数据结构之c++11(二)
开发语言·c++
星马梦缘18 分钟前
算法设计与分析 作业三 答案与解析
算法·线性规划·二分图匹配·多元最短路·流网络·bellmanford·匈牙利树算法
lcj251118 分钟前
【stack、queue、deque、priority_queue】C++ 栈 / 队列 / 优先级队列全解析!手撕实现 + 二叉树层序遍历(附源码)
开发语言·c++·笔记
微风欲寻竹影19 分钟前
Java数据结构——二叉树相关OJ题目详解
java·数据结构
兵哥工控19 分钟前
高精度微秒延时函数实现顺控工控项目实例
c++·mfc·硬件高精度计时器
微风欲寻竹影20 分钟前
Java数据结构——二叉树(Binary Tree)详解
java·数据结构·算法
j_xxx404_21 分钟前
Linux线程池硬核解析:从固定线程池、单例线程池到线程安全、死锁与锁模型|附源码
linux·运维·服务器·c++·安全·ai
想吃火锅100521 分钟前
【leetcode】3.无重复字符的最长字串js版
算法·leetcode·职场和发展