应该使用哪种数据类型在数组中存储大小为10 ^ 9的整数?

时间:2017-08-23 08:49:38

标签: c

我必须编码一个问题,其中我必须在扫描的所有整数中找到最小整数的频率。我已经声明数组是size_t类型,在我的机器上有8个字节的大小,我也试过数据类型unsigned long,但我在代码的hackerearth上最终提交时仍然遇到运行时错误。

我已经检查并验证了代码没有问题,因为在给定输入的情况下已经传递了3个测试用例,但是对于剩余的测试用例却没有问题,因此用于存储数字的数据类型出现问题。

请引导我使用我应该使用的正确数据类型。问题在下面的链接中提到:

https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/monk-and-lucky-minimum-3/

代码:

#include <stdio.h>
#include<stdlib.h> 
size_t a[100001]={0};
int main()
{
    int test_no,i;

    scanf(" %d",&test_no);
    for(i=0;i<test_no;i++)
    {
        int j,n ;
        size_t x,min;
        scanf(" %d",&n);
        for(j=0;j<n;j++)
        {
            scanf(" %zu",&x);
            a[x]++;
            if(j==0)
            {
                min=x;
            }
            if(x<=min)
            {
                min=x;
            }
        }
        if(a[min]%2==0 && a[min]!=0) //In case if every integer occurs only once so all entries of count will be 0 , so he'll be unlucky 
        {
            printf("Unlucky \n");
        }
        else
        {
            printf("Lucky \n") ;
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:3)

long long至少是一个64位有符号的类型,并且能够存储多个那么大的数量。

现代C编译器(C99起)需要支持它。

(顺便说一句,你不需要保留数组中的所有数字来计算最小值或最大值 - 而是可以存储当前达到的极值。)