CSP历年复赛题-P2141 [NOIP2014 普及组] 珠心算测验

原题链接:https://www.luogu.com.cn/problem/P2141

题意解读:在一个互不相同的数组中,枚举两个不同数之和,和也在数组中,统计不同的和的个数。

解题思路:

用数组、哈希表分别记录每一个数

枚举每两个不同的数,求和,如果和在哈希表中也存在,则ans++,并且在哈希表中将和移除

最后输出ans

100分答案:

#include <bits/stdc++.h>
using namespace std;

int n, ans;
int a[105], h[20005];

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        h[a[i]] = 1;
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = i + 1; j <= n; j++)
        {
            if(h[a[i] + a[j]] == 1) 
            {
                ans++;
                h[a[i] + a[j]]--;
            }
        }
    }
    cout << ans;

    return 0;
}

 

posted @   五月江城  阅读(136)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示