逆序数字
题目描述
给出一个不多于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;}

浙公网安备 33010602011771号