1.求一个整数的第k位数字
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 //求x用10进制表示时的数位长度 6 int len(int x){ 7 if(x<10) 8 return 1; 9 return len(x/10)+1; 10 } 11 12 //取x的第k位数字 13 int f(int x,int k){ 14 if(len(x)-k==0) 15 return x%10; 16 return f(x/10,k); 17 } 18 19 int main() 20 { 21 int x=23574; 22 printf("%d ",f(x,3)); 23 return 0; 24 }
2.求一个数的个位,十位,百位情况
比如说一个三位数:
num[0] = a/100; //百
num[1] = a/10%10; //十
num[2] = a%10; //个