luogu P2141 珠心算测验

传送门

今天是2019.6.11 距离NOIP2019还有150天

最近学校开始给准高三的我们加上晚自习一直到八点半

极大程度地压缩了我的课余时间

废话不多说 进入正题

这是一道普及-的数组题

我的思路很简单:

a[i]表示读入的数字

js[i]表示数字i作为加数出现的次数

第一遍读入的时候将js[a[i]]赋值为1

之后每两个数字相加(a[i] + a[j])的时候更新js[a[i] + a[j]]的值

然鹅(划重点)

这里更新的时候需要判断读入的数字中是否有a[i] + a[j]这个数

换句话说判断js[a[i] + a[j]]是否为1即可

这个地方害了我交了三遍才过

上代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<string>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#define enter puts("")
#define space putchar(' ')
using namespace std;
typedef long long ll;
ll read()
{
    ll op = 1, ans = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') op = 0;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        ans *= 10;
        ans += ch - '0';
        ch = getchar();
    }
    return op ? ans : -ans;
}
void write(ll x)
{
    if(x < 0)
    {
        x = -x;
        putchar('-');
    }
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
ll js[20005], n, a[105], maxn, ans;
int main()
{
    n = read();
    for(int i = 1;i <= n;i++)
    {
        a[i] = read();
        js[a[i]] = 1;
        maxn = max(maxn, a[i]);
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = i + 1;j <= n;j++)
        {
            if(js[a[i] + a[j]] == 1)js[a[i] + a[j]]++;
        }
    }
    for(int i = 1;i <= maxn;i++)
    {
        if(js[i] >= 2) ans++;
    }
    write(ans);
    enter;
    return 0;
}
原文地址:https://www.cnblogs.com/thx666/p/11006262.html