输入一个不多于5位的正整数,判断它是几位数,并逆序输出各位数字。
if只用选择结构来说的话,那可以每个情况一点一点来分析
#include<stdio.h>
main()
{int a,b,c,d,e,f;
printf("输入一个不多于5位的正整数:");
scanf("%d",&a);
if(a/10>0&&a/100==0)
{printf("这是一个两位数\n");
b=a%10;
c=a/10;
printf("逆序输出为%d,%d",b,c);}
else if(a/100>0&&a/1000==0)
{printf("这是一个三位数\n");
b=a%10;
c=a/10%10;
d=a/100;
printf("逆序输出为%d,%d,%d",b,c,d);}
else if(a/1000>0&&a/10000==0)
{printf("这是一个四位数\n");
b=a%10;
c=a/10%10;
d=a/100%10;
e=a/1000;
printf("逆序输出为%d,%d,%d,%d",b,c,d,e);}
else if(a/10000>0&&a/100000==0)
{printf("这是一个五位数\n");
b=a%10;
c=a/10%10;
d=a/100%10;
e=a/1000%10;
f=a/10000;
printf("逆序输出为%d,%d,%d,%d,%d",b,c,d,e,f);}
else printf("输入就错误,你还想继续算?洗洗睡吧!");
}
如果是五位数以下的话在整除10000时会等于零
#include<stdio.h>
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;
b=x%10000/1000;
c=x%1000/100;
d=x%100/10;
e=x%10;
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
else if (e!=0) printf(" there are 1,%ld\n",e);
}