JonsenElizee

Software Developing Blog

"An idea is fragile . It can be killed by a scornful smile or a yawn .It can be mound down by irony and scared to death by a cold look."
"Most cultures throughout human history have not liked creative individuals .They ignore them or kill them.It is a very efficient way of stopping creativity."

------Advertising boss Charles Browe and Howard Gardner ,professor at Harvard

   :: 首页 :: 新随笔 ::  ::  :: 管理 ::
This is a funny programm and I don't why it happens in this way.
No matter what data input that is composed with different numbers, like 123456,
124578, 235689, 789456, 147896, 321478 and so on. through this program, it will
get a same data ever got.



  1 // funny program by JonsenElizee.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <math.h>
  6 #include <assert.h>
  7 /*
  8  * set value in specified position.
  9  * void* ptr pointer of the start point of bit.
 10  * int pos position of the bit to be set and pos starts from 0.
 11  * int val value to set. val is 0 or 1.
 12  */
 13 void setbit(void* ptr, int pos, int val)
 14 {
 15     /* cast to unsigned char* type pointer */
 16     unsigned char* uch = (unsigned char*)ptr;
 17 
 18     /* move pointer */
 19     uch += pos/8;
 20     
 21     /* set the bit with right value, 0 or 1. */
 22     unsigned char tmp = 1 << (pos%8);
 23     if ( val && (unsigned char)(*uch & tmp) == 0*uch += tmp;
 24     else *uch -= tmp;
 25 }
 26 unsigned char getbit(void* ptr, int pos) 
 27 {
 28     /* cast to unsigned char* type pointer */
 29     unsigned char* uch = (unsigned char*)ptr;
 30 
 31     /* move pointer */
 32     uch += pos/8;
 33 
 34     /* set the bit with right value, 0 or 1. */
 35     unsigned char tmp = 1 << (pos%8);
 36     return (unsigned char)(*uch & tmp);
 37 }
 38 
 39 unsigned revert_unsigned_int(unsigned ipt)
 40 {
 41     unsigned opt = 0;
 42     unsigned short i = 0;
 43     for(opt = ipt; opt != 0; opt /= 10, i++); 
 44     for(opt = 0; ipt != 0; ipt /= 10)
 45         opt += (ipt - ipt / 10 * 10* ((unsigned)pow(10--i));
 46     return opt;
 47 }
 48 
 49 unsigned sort_unsigned_int(unsigned ipt, int ascend)
 50 {
 51     unsigned opt = 0;
 52     unsigned i = 0;
 53     
 54     /* get length of input data */
 55     for(opt = ipt; opt != 0; opt /= 10, i++); 
 56 
 57     unsigned char* num = (unsigned char *)malloc(i);
 58     memset(num, 0, i);
 59     unsigned char* ptr = num;
 60     unsigned char* mov = ptr + 1;
 61     unsigned n = i;
 62 
 63     /* get each number char of input data */
 64     for(; ipt != 0; ipt /= 10)
 65         num[--i] = (unsigned char)(ipt - ipt / 10 * 10);
 66 
 67     /* sort the number */
 68     for(i = 0; i < n-1; i ++) {
 69         if(ascend) {
 70             while(mov > num && mov[0< mov[-1]) {
 71                 mov[0^= mov[-1];
 72                 mov[-1^= mov[0];
 73                 mov[0^= mov[-1];
 74                 mov--;
 75             }
 76         } 
 77         else {
 78             while(mov > num && mov[0> mov[-1]) {
 79                 mov[0^= mov[-1];
 80                 mov[-1^= mov[0];
 81                 mov[0^= mov[-1];
 82                 mov--;
 83             }
 84         }
 85         ptr++;
 86         mov = ptr+1;
 87     }
 88     
 89     /* convert to a unsigned int */
 90     for (opt = i = 0; i < n; i++)
 91     {
 92         opt += num[i] * ((unsigned)pow(10, n-i-1));
 93     }
 94     
 95     free(num);
 96     return opt;
 97 }
 98 
 99 void loop_to_original_data(unsigned ipt)
100 {
101     assert(ipt < 1000000);
102     char his[1000000= {0};
103     while(1) {
104         printf("%d ", ipt);
105         unsigned a = sort_unsigned_int(ipt, 1);
106         unsigned b = sort_unsigned_int(ipt, 0);
107         if(a == b) {
108             printf("%d\n", ipt);
109             return;
110         }
111         unsigned rst =  b - a;
112         if (getbit(his, rst)) {
113             printf("%d\n", rst);        
114             break;
115         }
116         else 
117             setbit(his, rst, 1);
118         ipt = rst;
119     }
120 }
121 
122 int main()
123 {
124     loop_to_original_data(123456);
125     getchar();
126     return 0;
127 }
128 
129 



posted on 2010-10-22 08:55 JonsenElizee 阅读(1387) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


By JonsenElizee