整數的分解
一個整數是由1至多位數字組成的,如何分解出整數的各個位上的數字,然后加以計算
對一個整數做%10的操作,就得到它的個位數;
對一個整數做/10的操作,就去掉了它的個位數;
然后再對上結果做%10,就得到原來數的十位了;
一次類推。
例
int main()
{
int x;
scanf("%d", &x);
int digit;
int ret = 0;
while ( x > 0 ) {
digit = x%10;
ret = ret*10 + digit;
printf("x=%d, digit = %d, ret = %d\n", ret);
x /= 10;
}
return 0;
}
字符串又怎么做反轉呢?
note:
digit 從零到九的任意數字