华为上机题:任意输入一个数是1到9做运算的结果,求有多少种可能性

对1到9做运算可能是+,-,或者没有符号直接构造成新数,想123,456,2345等,程序已经经上机验证过

#include<stdio.h>

int sumf(int a1, int a2,int o);
void main(){
int a[9]={1,2,3,4,5,6,7,8,9};
int j1,j2,j3,j4,j5,j6,j7,j8,s2,s3,s4,s5,s6,s7,s8,s9,t;
int i=0;
char fh[3]={'+','-','_'};
printf("please input number");
scanf("%d",&t);
for(j1=0;j1<3;j1++)

 s2=sumf(a[0],a[1],j1);
        for(j2=0;j2<3;j2++)
  {
    s3=sumf(s2,a[2],j2);
       for(j3=0;j3<3;j3++)
    {

            s4=sumf(s3,a[3],j3);
                 for(j4=0;j4<3;j4++)
     {
       s5=sumf(s4,a[4],j4);
                        for(j5=0;j5<3;j5++)
      {
      s6=sumf(s5,a[5],j5);
                            for(j6=0;j6<3;j6++)
       {
        s7=sumf(s6,a[6],j6);
           for(j7=0;j7<3;j7++)
        {
         s8=sumf(s7,a[7],j7);
            for(j8=0;j8<3;j8++)
         {
          s9=sumf(s8,a[8],j8);
          if(s9==t)
          {
           ++i;
          printf("%d",i);
          }
         }
        }
       }
      }
        }
          }
      }
  
        }
    }

 

 int sumf(int a1,int a2,int o)
  {
  int sum ;
  if(o==0)
   sum=a1+a2;
  else if(o==1)
   sum=a1-a2;
     else
       sum=a1*10+a2;
  return sum;
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值