给一个正整数,要求:
1.求出它是几位数;
2.分别输出每一位数;
3.按逆序输出各位数字,例如原数为123,应输出321.
#include<stdio.h>
#include<math.h>
int Count(int n)
{
int count=0;//确定位数
while(n)//真1 非零 零 假0
{
n/=10;//n=n/10//每次除10
count++;//每除一次+1,最终为总位数
}
return count;
}
int Nixu(int n)
{
int a;//求余数的个位
int c=Count(n)-1;
int b=1;//10的次方
int d=0;
while ©//凑10的次方
{
b*=10;
c–;
}
while(n)//求出个位余数乘10的次方
{
a=n%10;//各位余数
d=d+a*b;
b/=10;//1000的依次降幂
n/=10;//数的位数-1
}
return d;
}
void ZhengXu(int n)
{
int count = Count(n)-1;
int a=1;
int b;
while(count)//凑10的次方
{
a*=10;
count–;
}
while(n)
{
b=n/a;
n%=a;//分别求出各位余数
a/=10;
printf("%d ",b);
}
}
int main()
{
printf("%d\n",Count(1234));
printf("%d\n",Nixu(1234));
ZhengXu(1234);
return 0;
}
运行如下: