Everyone said that there is a problem with the open array of the topic, so I will put the code of the topic into OJ and run it
PS: There is a pitfall in the output format of the questioner. According to the principle of allowing the questioner to understand and make minimal modifications, I have improved your code
#include <stdio.h>
#define N 500000
int main() {
int n, m;
int t[1000001];
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 0; i <= 1000000; i++) {
t[i] = 0;
}
while(n--) {
int x;
scanf("%d", &x);
t[x+N]=1;
}
int num=0;
for (int i = 1000000; i>=0; i--) {
if (num==m) {
printf("\n");
break;
}
else if(t[i]==1) {
printf("%d", i-N); // 这里没有空格了
num++;
if (num != m){
printf(" "); // 这里是个坑, 因为最后一个输出是没有空格的
}
}
}
}
return 0;
}
HDU
Nine Degrees
As mentioned in the title, the Hash algorithm is not a sorting problem.
This is a basic ACM question. Take the input number and add 500000 to avoid negative numbers as keys. Value is the number of occurrences. If the value is entered, it will be sorted directly.
Then when outputting, just output the keys whose value is not 0
Everyone said that there is a problem with the open array of the topic, so I will put the code of the topic into OJ and run it
PS: There is a pitfall in the output format of the questioner. According to the principle of allowing the questioner to understand and make minimal modifications, I have improved your code
HDU
Nine Degrees
As mentioned in the title, the Hash algorithm is not a sorting problem.
This is a basic ACM question. Take the input number and add 500000 to avoid negative numbers as keys. Value is the number of occurrences. If the value is entered, it will be sorted directly.
Then when outputting, just output the keys whose value is not 0
Many answers online
http://www.cnblogs.com/acmer-...
Open such a large array on the stack?
You don’t need to ask this kind of postgraduate entrance examination question.
1. Define t to the heap
2. Enter n and m and then judge the size relationship. Is it better?
If your array is too large, it is very likely that the stack will explode. You can try to use
new
to apply for an array.