1. 程式人生 > >逆序輸出數字

逆序輸出數字

題目:給一個不多於5位的正整數,要求:

1.求出它是幾位數;

2.分別輸出每一位數字;

3.按逆序輸出個位數字,例如原數為321,應輸出123.

#include<stdio.h>
//計算m的n次方
int sqrt(int m,int n){
int i = 1;
int a = m;
if(m == 0){
return 0;
}
if(n==0){
return 1;
}
while(i < n ){
m *= a;
i++;
}
return m;
}

//判斷整數n是幾位數
int test_01(int n){
int i = 0;
do{
i++;
n /= 10;
}while(n != 0);
         return i;
}


//逆序輸出n的每一位數字
void test_02(int n){

while(n != 0){
int a = n % 10;
        n /= 10;
printf("%d ",a);
}
printf("\n");
}


//n中有幾個 1 ?
void test_03(int n){
int i = 0;
while(n != 0){
int a = n%10;
if(a == 1){
i++;
}
n /= 10;
}
printf("這個數字有 %d 個 1\n",i);
}


//正序輸出n的每一位數字
void test_04(int n){
while( n != 0){
int temp = sqrt(10,test_01(n)-1);
int m = n/temp;
n %= temp;  
printf("%d ",m);
}
printf("\n");


}


int main(){
printf("%d\n",test_01(3521));
printf("%d\n",test_01(618951));
printf("%d\n",test_01(118151));
printf("%d\n",test_01(0));


printf("\n");
test_02(3521);
test_02(618951);
test_02(118151);


printf("\n");
test_03(3521);
test_03(618951);
test_03(118151);


printf("\n");
test_04(3521);
test_04(618951);
test_04(118151);


printf("\n");
printf("%d\n",sqrt(3,2));
printf("%d\n",sqrt(2,3));
printf("%d\n",sqrt(5,0));
printf("%d\n",sqrt(3,1));
printf("%d\n",sqrt(0,2));
printf("%d\n",sqrt(2,0));
printf("%d\n",sqrt(0,0));
}