逆序数字

题目描述

给出一个不多于5位的整数,要求 1、求出它是几位数 2、分别输出每一位数字 3、按逆序输出各位数字,例如原数为321,应输出123

输入

一个不大于5位的数字

输出

三行 第一行 位数 第二行 用空格分开的每个数字,注意最后一个数字后没有空格 第三行 按逆序输出这个数

样例输入

12345

样例输出

5
1 2 3 4 5
54321

提示

哈姆雷特:数字还是字符?这是一个问题!

来源

题解:

#include<stdio.h>
#include<math.h>
int main()
{
     int num,indiv,ten,hundred,thousand,ten_thousand,place;
     scanf ( "%d" ,&num);
     ten_thousand=num/10000;
     thousand=( int )(num-ten_thousand*10000)/1000;
     hundred=( int )(num-ten_thousand*10000-thousand*1000)/100;
     ten=( int )(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
     indiv=( int )(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
     if (num>9999)
         place=5;
     else if (num>999)
         place=4;
     else if (num>99)
         place=3;
     else if (num>9)
         place=2;
     else
         place=1;
     printf ( "%d\n" ,place);
    switch (place)
     {
       case 5: printf ( "%d %d %d %d %d\n" ,ten_thousand,thousand,hundred,ten,indiv);
              printf ( "%d%d%d%d%d" ,indiv,ten,hundred,thousand,ten_thousand);
              break ;
       case 4: printf ( "%d %d %d %d\n" ,thousand,hundred,ten,indiv);
              printf ( "%d%d%d%d" ,indiv,ten,hundred,thousand);
              break ;
       case 3: printf ( "%d %d %d\n" ,hundred,ten,indiv);
              printf ( "%d%d%d" ,indiv,ten,hundred);
              break ;
       case 2: printf ( "%d %d\n" ,ten,indiv);
              printf ( "%d%d" ,indiv,ten);
              break ;
       case 1: printf ( "%d\n" ,indiv);
              printf ( "%d" ,indiv);
              break ;
     }
     return 0;
 
}
或:
#include<stdio.h>
 
int main() {
     int x,y[5],i,j;
     scanf ( "%d" ,&x);
     for (i=0;x!=0;i++) {
         y[i]=x%10;
         x=x/10;
     }
     printf ( "%d\n" ,i);
     for (j=i-1;j>0;j--)
         printf ( "%d " ,y[j]);
     printf ( "%d" ,y[0]);
     putchar ( '\n' );
     for (j=0;j<i;j++)
         printf ( "%d" ,y[j]);
     putchar ( '\n' );
     return 0;
}

转载于:https://www.cnblogs.com/SSYYGAM/p/4211082.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值