#include <stdio.h> int main() { int zheng,i; int m=0; int yu[10]; int count=0; scanf("%d",&m); zheng=m; for(i=0;i<10;i++) if (zheng!=0) { yu[i]=zheng%10; zheng=zheng/10; count++; } for(i=0;i<count;i++) { printf("%d ",yu[i]); } return 0; }
第二種:
#include <stdio.h> int main() { int num = 0; int i = 0; int arr[10]; int left = 0; int right = 0; int j = 0; scanf("%d", &num); while (num) { arr[i] = num % 10; num = num / 10; i++; } //將arr[4]裡面的元素逆序 right = i - 1; while (left < right) { int tmp = arr[left]; arr[left] = arr[right]; arr[right] = tmp; left++; right--; } //輸出 for (j = 0; j < i; j++) { printf("%d ", arr[j]); } printf("\n"); return 0; }
第三種:采用遞歸
#include <stdio.h> void print(int num)//遞歸 { if (num >= 10) print(num / 10); printf("%d ", num % 10); } int main() { int num = 10; scanf("%d", &num); print(num); return 0; }