我想把 4567 分成 4, 5, 6, 7 , 分离的方法之一是:
int value =4567, rightDigit;
rightDigit =number%10;
number /=10;
然而结果是 7, 6, 5, 4, 我如何让它打印 4 5 6 7 呢?谢谢
最佳答案
您不需要显式存储数字。您可以使用 call stack存储它们并以正确的顺序打印它们。
void print_digits(int n) {
// In case n is negative, print the leading '-'
// and transform n to a non-negative number
if(n < 0) {
printf("-");
n = -n;
}
if(n/10) {
print_digits(n/10);
}
printf("%d", n%10);
}
关于c - 如何在不交换顺序的情况下将多位数字分成单独的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845127/