Greatest product of five consecutive digits in a 1000-digit number

后端 未结 9 1678
旧巷少年郎
旧巷少年郎 2020-12-24 11:15

I am working through the problems on project Euler and am not too certain if my understanding of the question is correct.

Problem 8 is as follows:

9条回答
  •  清歌不尽
    2020-12-24 11:21

    In C I copied it in a txt file and read from it, or you can just initialise string at the beginning.

    #include 
    #include 
    
    int main()
    {
        FILE *a;
        a=fopen("Long.txt","r");
        char s[1001];
        fscanf(a,"%s",s);
    char p[6];
    
    int i=0,x,prdmax=1,m,n;
    while(s[i]!='\0')
    {
        p[0]=s[i];
        p[1]=s[i+1];
        p[2]=s[i+2];
        p[3]=s[i+3];
        p[4]=s[i+4];
        p[5]='\0';
    
        x=atoi(p);
        n=x;
        int prd=1;
        while(x!=0)
        {
            int q=x%10;
            prd*=q;
            x/=10;
        }
    
        if(prd>prdmax)
        {
            prdmax=prd;
            m=n;
        }
        i++;
    }
    
    printf("Numbers are: %d\n Largest product is: %d",m,prdmax);
    
    fclose(a);
    }
    

提交回复
热议问题