题目
题目来源
B - Problem Select
HOJ is an online judge system for students in HIT to practice. As the administrator, you would always setup contests by selecting problems from the problem set. Problem IDs are integers that can be phased by url. For example, the corresponding problem ID of url http://acm.hit.edu.cn/problemset/1001 is 1001.
Now the task is, given n urls, print the smallest k problem IDs in increasing order.
Input
The first line of the input contains one integer T (1 ≤ T ≤ 10), indicating the number of test cases.
For each test case, the first line contains two integers, n (1 ≤ n ≤ 1000) and k (1 ≤ k ≤ n).
For the next n lines,each line contains a string indicating the problem’s url. The problem ID is guaranteed to be in range [1,10000] and unique in each case.
Output
Output T lines.
For each test case, you should output k numbers in a line, and there shouldn’t be any spaces at the end of the line.
Sample Input
2
3 2
http://acm.hit.edu.cn/problemset/1003
http://acm.hit.edu.cn/problemset/1002
http://acm.hit.edu.cn/problemset/1001
4 1
http://acm.hit.edu.cn/problemset/1001
http://acm.hit.edu.cn/problemset/2001
http://acm.hit.edu.cn/problemset/3001
http://acm.hit.edu.cn/problemset/501
Sample Output
1001 1002
501
题目大意
给定一个T为测试用例的数目,对于每个测试用例,输入一个n和k,n为此测试用例中字符串的个数,而每个字符串都是一个网址,我们要做的就是把网址最后的数字取出来,然后按增加的顺序输出最小的k个数字。
题目分析
这道题很简单,直接根据网址的特点取出最后的几位数字然后把他们保存起来,然后将最小的k个元素挑出来依次输出。
解题分析
这道题很简单,直接根据网址的特点取出最后的几位数字然后把他们保存起来,然后将最小的k个元素挑出来依次输出。
解题思路
将固定网址前字母位都舍弃,保存数字部分的字符,然后转换位数字,然后挑出最小的k个数字。
具体代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int a[10000];
int main()
{
int T;
cin >> T;
while (T--)
{
int a[1000],l=0;
int n, k;
char str[1000] = {};
cin >> n >> k;
for (int i = 0; i < n; i++)
{
scanf("%s", str,1000);
int x=0;
for (int i = strlen(str)-1; i >=33 ; i--)
{
//cout << x<<" "<<i << " " << str[i] << " " << strlen(str) << endl;
x += (str[i]-'0')*pow(10, strlen(str) - i - 1);
}
a[l] = x;
l ++ ;
}
sort(a,a+l);
for (int i = 0; i < k; i++)
{
cout << a[i];
if (i != k - 1)cout << " ";
}
cout << endl;
}
return 0;
}