我们给出了一组2n个整数,其中这个整数数组中的每一对分别代表恐龙的出生年份和死亡年份。我们要考虑的有效年份范围是[-100000到2005]。例如,如果输入为:
-80000 -79950 20 70 22 60 58 65 1950 2004
这意味着第一只恐龙的出生年份为-80000,死亡年份为-79950。同样,第二只恐龙的寿命为20至70岁,依此类推。
我们想知道有史以来最多的恐龙活着。在给定上述2n个整数数组的情况下,编写一个计算方法。
这是我到目前为止所尝试的内容:
#include<stdio.h>
#include<stdlib.h>
#include <stddef.h>
static void insertion_sort(int *a, const size_t n)
{
size_t i, j;
int value;
for (i = 1; i < n; i++) {
value = a[i];
for (j = i; j > 0 && value < a[j - 1]; j--) {
a[j] = a[j - 1];
}
a[j] = value;
}
}
int main(){
int arr[10]={-80000,-79950,20,70,22,60,58,65,1950,2004};
int strt[5],end[5];
int bal[5];
int i,j,k,l,m,length;
l=0;
m=0;
for (i=0; i<10; i++){
//printf("i = %2d arr[i] = %2d\n", i, arr[i]);
if(i%2==0) {
strt[l]=arr[i];
l++;
} else {
end[m]=arr[i];
m++;
}
}
insertion_sort(strt, sizeof strt / sizeof strt[0]);
insertion_sort(end, sizeof end / sizeof end[0]);
k=sizeof(end)/sizeof(int);
for(j=0;j<5;j++) {
bal[i]=strt[i]-end[k-1];
k--;
}
length=sizeof bal / sizeof bal[0];
insertion_sort(bal, sizeof bal / sizeof bal[0]);
printf("answer is = %2d",bal[length-1]);
}
答案 0 :(得分:0)
我会做类似的事情:
从理论上讲,你可以使用一个向量而不是一个地图,但它可能会填充得足够稀疏,以至于地图更有意义。