这是杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1280

 Problem Description:
  还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。 
  给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
 Input:
  输入可能包含多组数据,其中每组数据包括两行: 
  第一行两个数N和M, 
  第二行N个数,表示该序列。
 Output:
  对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。
 Sample Input:
  4 4
  1 2 3 4
  4 5
  5 3 6 4
 Sample Output:
  7 6 5 5
  11 10 9 9 8
  解题思路:使用两个for循环求和装到一个新数组中,再排序输出即可。值得注意的是:到不了5000000不能ac!通过这道题我记住了一句话--“反正内存不要钱,能开多大开多大!”。内存是一方面,还有就是输出格式(经常刷杭电oj上题的码友估计都知道.)。
  这是我的代码:
 #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[];
int b[];
int main()
{
int M,N;
while (cin >> N >> M) {
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int i;
for (i = ; i < N; i++)
cin >> a[i];
int k = ,j;
for (i = ; i < N; i++) {
for (j = i + ; j < N; j++) {
b[k++]=a[i]+a[j];
}
}
sort(b,b+k);
for(i = k - ; i > k - M; --i) {
cout << b[i] << " ";
}
cout << b[k - M];
cout << endl;
}
return ;
}

  欢迎码友评论,一起成长。

<hdu - 1280> 前M大的数 (注意其中的细节)的更多相关文章

  1. HDU 1280 前m大的数

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory L ...

  2. hdu 1280 前m大的数 哈希

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  3. HDU 1280 前m大的数【排序 / hash】

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. HDU 1280 前m大的数(排序,字符串)

      前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. HDU 1280 前m大的数 基数排序

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 题目大意: 给你N(N<=3000)个数(这些数不超过5000),要求输出他们两两相加后和最大的M( ...

  6. HDU 1280 前m大的数【哈希入门】

    题意:中文的题目= =将各种组合可能得到的和作为下标,然后因为不同组合得到的和可能是一样的, 所以再用一个数组num[]数组,就可以将相同的和都记录下来 #include<iostream> ...

  7. hdu 1280 前m大的数(排序)

    题意:排序 思路:排序 #include<iostream> #include<stdio.h> #include<algorithm> using namespa ...

  8. 杭电 1280 前m大的数

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  9. hdu---(1280)前m大的数(计数排序)

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. ASP.NET MVC:利用ASP.NET MVC4的IBundleTransform集成LESS

    ASP.NET MVC:利用ASP.NET MVC4的IBundleTransform集成LESS 背景 LESS确实不错,只是每次写完LESS都要手工编译一下有点麻烦(VS插件一直没有安装好),昨天 ...

  2. Centos 64位 Install certificate on apache 即走https协议

    Centos 64位 Install certificate on apache 即走https协议 一: 先要apache 请求ssl证书的csr 一下是步骤: 重要注意事项 An Importan ...

  3. linux netstat 命令详解

    linux netstat 命令详解 1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接 路由表  接口状态链接 多播成员等等. 2.参数含义介绍 -a (all)显示所 ...

  4. ubuntu安装 cober 笔记

    设置原始Ubuntu原始Root密码 安装Mysql 安装Jdk 拷贝Cober到Linux,启动服务(看日志) 设置原始Ubuntu原始Root密码 $ sudo passwd root 修改系统文 ...

  5. 本地存储 cookie,session,localstorage( 一)基本概念及原生API

    http://www.w3school.com.cn/html5/html_5_webstorage.asp http://adamed.iteye.com/blog/1698740 localSto ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. Map - leetcode [哈希表]

    149. Max Points on a Line unordered_map<float, int> hash 记录的是斜率对应的点数 unordered_map<float, i ...

  8. [MFC美化] SkinMagic使用详解3- 常见使用问题解答

    在SkinMagic使用过程中,经常遇到以下几个问题: 1. 静态加载皮肤文件时,资源文件IDR_SKIN_CORONA可能会报错:未声明的标识符 解决方法:添加头文件"Resource.h ...

  9. HDU 5887 Herbs Gathering

    背包,$map$,优化. 和普通背包一样,$map$加一个$erase$优化一下就可以跑的很快了. #pragma comment(linker, "/STACK:1024000000,10 ...

  10. IntelliJ IDEA “Finds duplicated code”提示如何关闭

    发现重复的代码这个提示真的很烦啊,我们怎么关闭他呢. 设置在这里: Settings -> Editor -> Inspections -> General -> Duplic ...