給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
[cpp]
#include<iostream>
int digit(int,int&);
int order(int,int);
int main(int argc,int argv[]){
int number = 0;
int count = 0;
printf("請輸入一個不多於5位的數:");
scanf("%d",&number);
digit(number,count);
printf("位數=%d\n",count);
order(number,count);
return 0;
}
int digit(int x,int& y){
while(x>0){
x = x/10;
y++;
}
return 0;
}
int order(int x,int y){
int a = 0;
int c = 0;
int d = 0;
for(int i=1;i<=y;i++){
int b = 1;
for(int j=1;j<=i;j++){
b *= 10;
}
a=(x%b-c);
d = a/(b/10);
c=x%b;
printf("%d",d);
}
printf("\n");
return 0;
}
摘自 like7xiaoben