我在 c 中编写了一个程序,它应该从 1 到 100000 接受输入,但是当我运行程序时,我只接受高达 12773 的输入,但是我使用 long long unsigned int 作为数组保存输入。
如果我输入的输入少于 12774,则程序会显示正确的行为,但不会超过该输入。我制作的程序是http://ideone.com/iXM1M0 我不明白c的这种奇怪行为。
#include<stdio.h>
#include<stdlib.h>
unsigned long long int inversion_count(unsigned long long int *arr, unsigned long long int start, unsigned long long int end)
{
unsigned long long int left_count, right_count, split_count = 0, i, left_arrend, j, no_ele;
no_ele = end - start;
unsigned long long int temp_arr[no_ele], k=0;
if(no_ele == 1) // this means that only one element is passed
return 0;
else
{
left_arrend = start + (no_ele/2);
left_count = inversion_count(arr, start, left_arrend);
right_count = inversion_count(arr, left_arrend, end);
i = start;
j = left_arrend;
while((i< left_arrend) && (j < end))
{
if(arr[i] <= arr[j])
{
temp_arr[k] = arr[i];
i++;
k++;
}
else
{
split_count += left_arrend - i;
temp_arr[k] = arr[j];
j++;
k++;
}
}
while(i < left_arrend)
{
temp_arr[k] = arr[i];
k++;
i++;
}
while(j < end)
{
temp_arr[k] = arr[j];
j++;
k++;
}
for(i=start; i<end; i++)
arr[i] = temp_arr[i-start];
return left_count + right_count + split_count;
}
}
int main()
{
unsigned long long int num, n = 100000, i, result;
unsigned long long int sum = 0;
// scanf("%lld", &n);
unsigned long long int arr[n];
for(i=0; i<n; i++)
{
scanf("%llu", &arr[i]);
sum += arr[i];
}
result = inversion_count(arr, 0, n);
// for(i=0; i<n; i++)
// printf("%lld\n", arr[i]);
printf("%llu\n", result);
printf("sum = %llu\n", sum);
printf("n = %llu\n", n);
return 0;
}