循环条件如何判定?

如何去正序分解一个整数并输出?

#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 选择循环结束条件 我要它干了什么再结束? 干完之后的标志条件是什么? 几个条件选一个? 特殊情况是否满足? 确定循环结束条件 循环提前结束| yes no

目前觉得逻辑+尝试是不三法宝.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值