弱弱的问道acm水题,不知道哪里不对……(本人很菜)

welon123 2010-10-02 04:23:09
http://acm.hdu.edu.cn/showproblem.php?pid=2523
Problem Description
给你N个整数,x1,x2...xn,任取两个整数组合得到|xi-xj|,(0<i,j<=N,i!=j)。
现在请你计算第K大的组合数是哪个(一个组合数为第K大是指有K-1个不同的组合数小于它)。


Input
输入数据首先包含一个正整数C,表示包含C组测试用例.
每组测试数据的第一行包含两个整数N,K。(1<N<=1000,0<K<=2000)
接下去一行包含N个整数,代表x1,x2..xn。(0<=xi<=2000)


Output
对于每组测试数据,请输出第K大的组合数,每个输出实例占一行。


Sample Input
3
3 2
4 0 7
4 2
1 2 3 4
2 1
2 9


Sample Output
4
2
7
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
int c, n, k, i, j, temp;
int a[1001];
vector <int> b;
cin >> c;
while(c--){
cin >> n >> k;
for(i = 0; i < n; i++)
cin >> a[i];
for(i = 0; i < n; i++)
for(j =i + 1; j < n; i++)
{
temp = abs(a[i] - a[j]);
b.push_back(temp);
}
sort(b.begin(), b.end());
cout << b[b.size() - k] << endl;
b.clear();
}
return 0;
}


...全文
117 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
for(j =i + 1; j < n; i++)
===========================
i++???
shenchenman 2010-10-02
  • 打赏
  • 举报
回复
基本没有问题,就是这里:
for(i = 0; i < n; i++)
for(j =i + 1; j < n; i++)
{
第二个循环i换成j就好了
baihacker 2010-10-02
  • 打赏
  • 举报
回复

//注意:(一个组合数为第K大是指有K-1个不同的组合数小于它)。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

bool them[2048];
int data[1024];
int main()
{
int cas;scanf("%d", &cas);
for (int caseID = 1, n, k; cas--; ++caseID)
{
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) scanf("%d", &data[i]);
memset(them, 0, sizeof them);
for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j)
them[abs(data[i] - data[j])] = 1;
int i = 0, id = 0;
for (; id < k; ++i) if(them[i]) if (++id == k) break;
printf("%d\n", i);
}
return 0;
}

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧