逆序数字

题目描述

给出一个不多于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 ",place);
   switch(place)
    {
      case 5:printf("%d %d %d %d %d ",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 ",thousand,hundred,ten,indiv);
             printf("%d%d%d%d",indiv,ten,hundred,thousand);
             break;
      case 3:printf("%d %d %d ",hundred,ten,indiv);
             printf("%d%d%d",indiv,ten,hundred);
             break;
      case 2:printf("%d %d ",ten,indiv);
             printf("%d%d",indiv,ten);
             break;
      case 1:printf("%d ",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 ",i);
    for(j=i-1;j>0;j--)
        printf("%d ",y[j]);
    printf("%d",y[0]);
    putchar(' ');
    for(j=0;j<i;j++)
        printf("%d",y[j]);
    putchar(' ');
    return 0;
}
原文地址:https://www.cnblogs.com/SSYYGAM/p/4211082.html