#include <iostream>
using namespace std;
int length(int n){
int times = 0;
do {
n /= 10;
++times;
}while(n);
return times;
}
int main(){
//get the lastdigits
int number = 231213;
int count = 3; //extract the last count digits
int base = 1, result = 0;
for (int i = 0; i < count; ++i){
cout << result << " " << number << " " << base << endl;
result += base * (number % 10); //原result + 新来的数位 * 10,以保证顺序
number /= 10;
base *= 10;
}
cout << result << endl;
//get the first digit
number = 987654321;
count = 5; //extract the first count digits
int k = length(number) - count;
for (int i = 0; i < k; ++i){
number /= 10;
}
cout << number << endl;
}
-------------分割线---------------------
Summary:对于整数的操作,其实就是 *10 和 /10的合理运用。
-------------分割线---------------------
//display the reverse of integers
int reverse(int n){
int tmp = 1; //区别
int sum = 0;
for (;n!=0;){
tmp = n % 10;
n = n /10;
sum = sum*10 + tmp; //原sum * 10 + 新来的数位,则逆序
}
return sum;
}