• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
c++ 中的 set

set (集合) 中的元素是排序好的,而且是不重复的。

例题:hdu 4989

题目大意:求一组数列中任意两个不重复元素和,再求不重复和的和。

#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
const int INF = 0x3f3f3f3f;
const int moder = 1e9 + 7;
const int maxn = 1550;

int main()
{
    ll sum = 0;
    int n;
    set<int>s;
    while(~scanf("%d",&n))
    {
        sum = 0;
        int a[n];
        s.clear();
        for(int i=0;i < n;i++)
        {
            scanf("%d",&a[i]);
        }

        for(int i=0;i < n;i++)
        {
            for(int j=i+1;j < n;j++)
            {
                s.insert(a[i]+a[j]);
            }
        }

        set<int>::iterator it;   //利用迭代器访问,使用方法和指针差不多。
        for(it=s.begin();it != s.end();it++)
        {
            sum += *it;
        }
        printf("%lld\n",sum);

    }
    return 0;
}

——

posted on 2018-02-12 14:36  村雨sup  阅读(249)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3