C语言:奇位取数

功能:从低位开始取出长整型变量s奇数位上的数,依次
构成一个新数放在t中。
例如:当s中的数为:7654321时,t中的数为:7531。

#include <stdio.h>
long fun (long s,long t)
{
    
    
  /**********Program**********/

  /**********  End  **********/
 return t;
}  
main()     
{
    
    
  long s, t=0,m;
  printf("\nPlease enter s:"); scanf("%ld", &s);     
  m=fun(s,t);
  printf("The result is: %ld\n", m);
}

1.一个没有才华的写法

 int i, a = 0, cnt = 0;
 while(s){
    
       
         a = s % 10;
         for(i = 0;i < cnt;i++)
           a *= 10;
           t += a;
           s /= 100;
           cnt++;
 }

2.尽显才华的写法

   long k =1;
   while(s)
   {
    
    
   	  t += s % 10 * k;
   	  k *= 10;
   	  s /= 100;
   }
   return t;

猜你喜欢

转载自blog.csdn.net/m0_51354361/article/details/111565538