• hdu1280前m大的数


    坑爹啊!弄了半个小时,数组越界啊!

    前m大的数

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10719    Accepted Submission(s): 3746


    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
     
    Author
    Gardon
     
    Source
     
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<string.h>
     4 # include<algorithm>
     5 using namespace std;
     6 int cmp(const int a,const int b)
     7 {
     8     return a>b;
     9 }
    10 int a[3005],hash[3000*3000/2];
    11 int main()
    12 {
    13 
    14     int n,m;
    15     while(cin>>n>>m)
    16     {
    17         for(int i = 0;i < n;i++)
    18         {
    19             cin>>a[i];
    20         }
    21         int k = 0;
    22         for(int j = 0;j < n -1;j++)
    23         {
    24             for(int v = j+1;v < n;v++)
    25             {
    26                 hash[k++] = a[j]+a[v];
    27             }
    28         }
    29         sort(hash,hash+(n*(n-1))/2,cmp);
    30         for(int i = 0;i < m;i++)
    31         {
    32             if(!i)
    33             cout<<hash[i];
    34             else
    35             cout<<" "<<hash[i];
    36         }
    37         cout<<endl;
    38     }
    39 }
    View Code
     
  • 相关阅读:
    发现另一种简便的完全居中css写法 element.style { width: 500px; height: 200px; background: #eee; position: absolute; margin: auto; top: 0; left: 0; bottom: 0; right: 0; }
    子网掩码随笔
    C# MVC网站自动由HTTP转为HTTPS
    c++中的void*
    权利的游戏
    字符串
    字符串
    权利的游戏 S0803
    加权有向图
    加权无向图
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/4004371.html
Copyright © 2020-2023  润新知