全部问题 > 当前问题

求详解

#include  <stdio.h>

unsigned long fun(unsigned long  n)

{  unsigned long  x=0, s, i;   int  t;

   s=n;

/**********found**********/

   i=1;

/**********found**********/

   while(s>0)

   {  t=s%10;

      if(t%2==0){

/**********found**********/

         x=x+t*i;  i=i*10;

      }

       s=s/10;

   }

   return  x;

}

main()

{  unsigned long  n=-1;

   while(n>99999999||n<0)

  { printf("Please input(0<n<100000000): ");  scanf("%ld",&n);  }

  printf("\nThe result is: %ld\n",fun(n));

}


上善若水 2015-8-23 15:44:41

共 2 个回答

我是学霸 2015-8-23 15:52:27

首先s%10是取输入数字的末尾,然后用if语句判断这个末尾数字是否为偶数,如果是偶数则成为要输出数字的其中一位,当第一次循环时,x=0,i=1,t应为输出数字的个位,当第二次循环时,i=10,此时输出的t与i=10相乘,成为十位,以此类推,第三次输出的t值为百位……

嘿嘿大人 2015-8-23 17:33:48


unsigned long fun(unsigned long  n)


{  unsigned long  x=0, s, i;   int  t;//定义一个无符号长整型x=0,s,i;一个整型 t;


   s=n;                              //将形参n赋值给自己定义的一个长整型s


/**********found**********/


   i=1;                              //为增位变量赋初值1;


/**********found**********/


   while(s>0)                        //while语句,逻辑判断条件为s大于零


   {  t=s%10;                        //取s的个位数赋值给t


      if(t%2==0)                    //判断s的个位是不是一个偶数

{           


/**********found**********/


         x=x+t*i;  i=i*10;         //如果是的话,就执行x=x+t*i,也就是将i位赋值给x的i位;i=i*10,增位变量乘以10为下一位做准备


      }


       s=s/10;                     //将s除以10就是将s的个位去除,十位百位等各降一位,变成个位十位等;


   }


   return  x;                     //返回新构成的整数x


}


main()


{  unsigned long  n=-1;


   while(n>99999999||n<0)


  { printf("Please input(0<n<100000000): ");  scanf("%ld",&n);  }


  printf("\nThe result is: %ld\n",fun(n));


}


问题来自: 填空题真题