循環條件如何判定?

如何去正序分解一個整數並輸出?

#include<stdio.h>
int main(){
    int x;
    printf("Please input a integer:");
    scanf("%d",&x);
    /*
    A correct and even graceful way to solve the problem tends to kill a host of relatively incorrect ways, 
    which needs us to try it again and again.
    We can show the essence or fundament that adapts to  the special condition because to 
    meet the special condition tends to need more complicated requirements that tends to 
    meet the common conditions: 
    1   60000/10000=6//Wanted, the head of the number
    2   60000%10=0//Take off the head and prepare fot the next step
    3   10000/10=1000//10000 needs to change with the number
    1   0/1000=0//Wanted,thanks to the above step
    2   0%10=0
    3   1000/10=100
        ...
    1   0/10=0
    2   0%10=0
    3   10/10=1
    1   0/1=0//the last head of the number that has been broken up
    2   0%1=0
    3   1/10=0
    */
    if(x<0){
        printf("- ");
        x=-x;
    }
    int mask=1,temp=x;
    while(temp>9){//In fact, when "temp>0" loop can run one more time, so we change the judgment criteria.
        temp/=10;
        mask*=10;  
        //printf("mask=%d\n",mask);  
    }
    /*
    Now this program wants a proper mask. Why do not I use "do while()"? Because I know the mask needs to syschro with 
    temp's change and even if I can't think of it, after that I may try another relatively incorret way I 
    can find that I should use "while()" in begin. Virtually, one may begin with walking in a incorrrect way, 
    but he can return back after the try with some time that is probably wasted or not. 
    */    
   int head;
   do{
       head=x/mask;//1
       printf("%d",head);
       x%=mask;    //2
       mask/=10;   //3
       if(mask>0)   printf(" ");
   }while(mask>0);
   /*
   Because head changes with x & mask we can choose x or mask as the leading actor or actress of the end 
   condition of loop, meanwhile because x has become 0 before the end of loop we choose mask finally. 
   */ 
    return 0;
}

總結循環判定條件的確定流程:

Created with Raphaël 2.2.0選擇循環結束條件我要它幹了什麼再結束?幹完之後的標誌條件是什麼?幾個條件選一個?特殊情況是否滿足?確定循環結束條件循環提前結束|yesno

目前覺得邏輯+嘗試是不三法寶.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章