C语言正序分解非负整数并输出
2019-03-21 本文已影响0人
李药师_hablee
输入:
1234
输出
1 2 3 4
输入:
1000
输出
1 0 0 0
输入:
0
输出
0
代码
//c语言非负整数分解
//12345 / 10000 = 1;
//12345 % 10000 = 2345;
// 10000/10 = 1000; mask2
//每次做以上3件事,直到mask<10;
//2345 /1000 = 2;
//2345 % 1000 = 345;
//1000 / 10 =100; mask3
//345 /100 = 3;
//345 %100 = 45;
//100/10= 10; mask4
//45/10=4;
//45%10=5;
//10/10=1; mask5
//5/1=5;
//5%1=0;
//1/10=0 mask6
#include<stdio.h>
int main()
{
int x;
int t = 0;
int mask = 1;
int d;
printf("input x: ");
scanf("%d",&x);
t = x;
//先求mask是否大于1
printf("mask的变化:\n");
while(t>9)
{
mask = mask*10;
t = t/10;
printf("%d\n",mask);
}
printf("\n");
//按照之前的算法求每位数
do
{
d = x / mask ;
printf("%d",d);
if(mask>9)
{
printf("*");//最后一个空格不输出
}
x %= mask;
mask = mask/10;
}while(mask>0);
return 0;
}