解题思路:
遍历所有可能的情况




注意事项:
最高为不能为0,无结果时返回-1




参考代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
    char a[11]= {"0123456789"};
    char b[6]= {"00000"},c[7]= {"000000"};
    int i1,i2,i3,i4,i5,i6,j,flag,s,num,sum,counts=0;
    scanf("%d",&num);
    for(i1=1; i1<10; i1++)
    {
        b[0]=a[i1];
        for(i2=0; i2<10; i2++)
        {
            b[1]=a[i2];
            for(i3=0; i3<10; i3++)
            {
                b[2]=a[i3];
                for(i4=0; i4<10; i4++)
                {
                    b[3]=a[i4];
                    for(i5=0; i5<10; i5++)
                    {
                        b[4]=a[i5];
                        sum=0;
                        for(s=0; s<5; s++)
                        {
                            sum=sum+(b[s]-'0');
                        }
                        if(sum==num)
                        {
                            flag=1;
                            for(j=0; j<2; j++)
                            {
                                if(b[j]==b[4-j])continue;
                                else flag=0;

                            }
                            if(flag)
                                {puts(b);counts++;}
                        }
                    }
                }
            }
        }
    }
    /*--------------------------------------------------*/
    for(i1=1; i1<10; i1++)
    {
        c[0]=a[i1];
        for(i2=0; i2<10; i2++)
        {
            c[1]=a[i2];
            for(i3=0; i3<10; i3++)
            {
                c[2]=a[i3];
                for(i4=0; i4<10; i4++)
                {
                    c[3]=a[i4];
                    for(i5=0; i5<10; i5++)
                    {
                        c[4]=a[i5];
                        for(i6=0; i6<10; i6++)
                        {
                            c[5]=a[i6];
                            sum=0;
                            for(s=0; s<6; s++)
                            {
                                sum=sum+(c[s]-'0');
                            }
                            if(sum==num)
                            {
                                flag=1;
                                for(j=0; j<3; j++)
                                {
                                    if(c[j]==c[5-j])continue;
                                    else flag=0;

                                }
                                if(flag)
                                    {puts(c);counts++;}
                            }
                        }
                    }
                }
            }
        }
    }
if(!counts)printf("%d",-1);
    return 0;
}


 

0.0分

0 人评分

  评论区

  • «
  • »