Project Euler:problem 104

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Project Euler:problem 104相关的知识,希望对你有一定的参考价值。

题目链接:http://pe-cn.github.io/104/

# include <stdio.h>
# include <stdlib.h>

void sort(int x,int y,char *a)
{
    int xx=x,yy=y; 
    char k=a[x]; 
    if(x>=y)
        return ; 
    while(xx!=yy)
    {
         while(xx<yy&& a[yy]>=k)yy--;
         a[xx]=a[yy];
         while(xx<yy&& a[xx]<=k)xx++;
         a[yy]=a[xx];
    }
    a[xx]=k;
    sort(x,xx-1,a);
    sort(xx+1,y,a);
}

int main()
{
    char *a, *b, *c ,d[9];
    int i,j,k=0,n,N;
    while((a=(char *)malloc(sizeof(char)*100))==NULL);
    while((b=(char *)malloc(sizeof(char)*100))==NULL);
    while((c=(char *)malloc(sizeof(char)*100))==NULL);
    for(i=0;i<100;i++)
    {
        a[i]=0;
        b[i]=0;
        c[i]=0;
    }
    N=100;
    a[0]=1;
    b[0]=1;
    n=0;
    for(i=3;;i++)
    {
        for(j=0;j<=n;j++)
            c[j]=a[j]+b[j];
        for(j=0;j<=n;j++)
            if(c[j]>=10)
            {
                c[j+1]+=c[j]/10;
                c[j]%=10;
            }
        if(c[n+1]>0)
            n+=1;
        if(n>10)
        {
            for(j=0;j<9;j++)
                d[j]=c[j];
            sort(0, 8, d);
            for(j=0;j<9;j++)
                if(d[j]!=j+1)
                    break;
            if(j==9)
            {
                printf("last nine %d\n",i);
                k=1;
            }
            for(j=n;j>n-9;j--)
                d[n-j]=c[j];
            sort(0, 8, d);
            for(j=0;j<9;j++)
                if(d[j]!=j+1)
                    break;
            if(j==9)
            {
                printf("first nine %d\n",i);
                k+=1;
            }
            if(k==2)
            {
                printf("last and first nine %d\n",i);
                break;
            }
            else
                k=0;
        }
        if(n==N-1)
        {
            N+=100;
            free(a);
            a=NULL;
            while((a=(char *)malloc(sizeof(char)*N))==NULL);
            for(j=0;j<N-100;j++)
                a[j]=b[j];
            for(;j<N;j++)
                a[j]=0;
            free(b);
            b=NULL;
            while((b=(char *)malloc(sizeof(char)*N))==NULL);
            for(j=0;j<N-100;j++)
                b[j]=c[j];
            for(;j<N;j++)
                b[j]=0;
            free(c);
            c=NULL;
            while((c=(char *)malloc(sizeof(char)*N))==NULL);
            for(j=0;j<N;j++)
                c[j]=0;
        }
        else
        {
            for(j=0;j<=n;j++)
            {
                a[j]=b[j];
                b[j]=c[j];
                c[j]=0;
            }
        }
    }
    return 0;
}

部分结果:

first nine 308563
first nine 309329
last nine 309354
last nine 309998
first nine 310064
last nine 310229
first nine 311434
first nine 312461
last nine 312971
last nine 314328
first nine 315817
first nine 315965
first nine 317784
first nine 320465
last nine 320556
first nine 322658
first nine 325047
last nine 327712
last nine 328733
last nine 329468
first nine 329468
last and first nine 329468
Press any key to continue

以上是关于Project Euler:problem 104的主要内容,如果未能解决你的问题,请参考以下文章

Project Euler:Problem 89 Roman numerals

Problem 43 // Project Euler

Project Euler :Problem 54 Poker hands

Project Euler:Problem 77 Prime summations

Project Euler:Problem 76 Counting summations

Project Euler:Problem 34 Digit factorials