本题要求编写程序,对输入的一个整数,从高位开始逐位分割并输出它的各位数字。
输入格式:
输入在一行中给出一个长整型范围内的非负整数。
输出格式:
从高位开始逐位输出该整数的各位数字,每个数字后面有一个空格。
输入样例:
123456
输出样例:
1 2 3 4 5 6
# include <stdio.h>
# include <stdlib.h>
int main()
{
//法y1
//int n, t; //n>=0
//scanf_s("%d", &n);
//t = n;
//int cnt=1;
//while (t >= 10)
//{
// t /= 10;
// cnt++;
//}
//int* a = (int*)malloc(cnt * sizeof(int));
//for (int i = 0; i < cnt; i++)
//{
// a[i] = n % 10;
// n /= 10;
//}
//for (int i = cnt - 1; i >= 0; i--)
// printf("%d ", a[i]);
//free(a);
//法2
int n, t; //n>=0
scanf_s("%d", &n);
t = n;
int mask=1;
while (t >= 10)
{
mask *= 10;
t /= 10;
}
while (mask > 0)
{
printf("%d ", n / mask);
n %= mask;
mask /= 10;
}
return 0;
}