如何去正序分解一个整数并输出?
#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;
}
总结循环判定条件的确定流程:
目前觉得逻辑+尝试是不三法宝.