确定位数时一般采用丢个位的方法n/=10 丢个位
123/10=12 n=12 count=1
12/10=1 n=1 count=2
1/10=0 n=0 count=3 返回到count值进行输出
int GetFigures(intn)
{
intcount = 0;
do
{
count++;
n /=10;//丢弃个位 123/10=12
}while(n!= 0);
returncount;
}
给定一个整数顺序输出它的每位数字
顺序输出十进制数字n,如123-》1 2 3
123/100=1 n=1 1%100=1
23/10=2 n=2 2%10=2
3/10=0 n=3 3%10=3
void PrintOrder1(intn)//1234
{//int GetFigures(int n)
intcount;
intpower = 1;
//pow(10,count-1);
for(inti=0;i<count-1;i++)//4->1000
{
power*= 10;//power = power*10;
}
do
{
printf("%d ",n/power);//得到最高位
n %=power;//丢弃最高位
power/= 10;
}while(n!=0);
printf("\n");
}
也可采用栈的方式来运算
void PrintOrder(intn)
{
stack<int>s;
do
{
s.push(n%10);//进栈///1234
n/=10;
}while(n!=0);
while(!s.empty())
{
printf("%d ",s.top());
s.pop();
}
printf("\n");
}
逆序输出十进制数字n,如123-》3 2 1
123%10=3 123/10=12
12%10=2 12/10=1
1%10=1 1/10=0
void PrintReverse(intn)
{
if(n< 0)//
{
printf("-");
n =-n;
}
do
{
printf("%d ",n%10);//得到个位数字
n /=10;//丢弃个位数字
}while(n!=0);
printf("\n");
}