- 论坛徽章:
- 3
|
本帖最后由 蔡万钊 于 2011-05-14 06:32 编辑
- /*
- ============================================================================
- Name : n21cal.c
- Author :
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- unsigned long h;
- unsigned long l;
- }bignumber;
- bignumber table[10]={
- {0,0},
- {0UL,1UL},
- {0UL,2097152UL},
- {0UL,10460353203UL},
- {0UL,4398046511104UL},
- {0UL,476837158203125UL},
- {0UL,21936950640377856UL},
- {0UL,558545864083284007UL},
- {0UL,9223372036854775808UL},
- {0x5UL,0xEE7E56E3721F2929UL},
- };
- bignumber del(bignumber a, bignumber b)
- {
- bignumber c= {0,0};
- c.l = a.l - b.l;
- if(a.l < b.l )
- {
- a.h --;
- // c.l++;
- }
- c.h = a.h - b.h;
- return c;
- }
- bignumber add(bignumber a, bignumber b)
- {
- bignumber c= {0,0};
- c.l = a.l + b.l;
- if( ( (-1UL) - a.l ) < b.l)
- c.h ++;
- c.h += a.h + b.h;
- return c;
- }
- //获得对应的数字
- bignumber get_table( int i)
- {
- return table[i];
- }
- bignumber get_n_i(int b , int i )
- {
- bignumber c = {0};
- for(; i >0 ; i--)
- c = add(c , get_table(b));
- return c;
- }
- int inline mylog2( unsigned long x)
- {
- int i;
- static unsigned long logtable[64] ={
- 0x8000000000000000,
- 0x4000000000000000,
- 0x2000000000000000,
- 0x1000000000000000,
- 0x800000000000000,
- 0x400000000000000,
- 0x200000000000000,
- 0x100000000000000,
- 0x80000000000000,
- 0x40000000000000,
- 0x20000000000000,
- 0x10000000000000,
- 0x8000000000000,
- 0x4000000000000,
- 0x2000000000000,
- 0x1000000000000,
- 0x800000000000,
- 0x400000000000,
- 0x200000000000,
- 0x100000000000,
- 0x80000000000,
- 0x40000000000,
- 0x20000000000,
- 0x10000000000,
- 0x8000000000,
- 0x4000000000,
- 0x2000000000,
- 0x1000000000,
- 0x800000000,
- 0x400000000,
- 0x200000000,
- 0x100000000,
- 0x80000000,
- 0x40000000,
- 0x20000000,
- 0x10000000,
- 0x8000000,
- 0x4000000,
- 0x2000000,
- 0x1000000,
- 0x800000,
- 0x400000,
- 0x200000,
- 0x100000,
- 0x80000,
- 0x40000,
- 0x20000,
- 0x10000,
- 0x8000,
- 0x4000,
- 0x2000,
- 0x1000,
- 0x800,
- 0x400,
- 0x200,
- 0x100,
- 0x80,
- 0x40,
- 0x20,
- 0x10,
- 0x8,
- 0x4,
- 0x2,
- 0x1,
- };
- for( i = 0 ; i < 64 ; i++)
- {
- if (x & logtable[i]) return i;
- }
- return -1;
- }
- bignumber dvi_10(bignumber a)
- {
- bignumber t;
- int tmp = mylog2(a.h);
- t.h = ((a.h << tmp) | a.l >> ( 64 - tmp)) / 10 ;
- unsigned long yu = ((a.h << tmp) | a.l >> ( 64 - tmp)) % 10;
- t.l = (((a.l << tmp )>> tmp) | (yu << (64 - tmp))) / 10;
- tmp = mylog2(t.l);
- a.h = t.h >> tmp;
- a.l = t.l | ( t.h << ( 64 - tmp )) ;
- return a;
- }
- void inline revstr(char *str)
- { //省去一变量, 时间换空间法
- char *head = str, *tail = str + strlen(str) -1;
- for(; head < tail; *head=*head ^ *tail, *tail=*head ^ *tail, *head=*head++ ^ *tail--);
- }
- void inline to_str(bignumber n, char N[22])
- {
- //基本原理是,
- // 寻找个位,然后 除以 10
- // 继续
- int cur=0;
- do{
- bignumber n3=n,n2 = dvi_10(n);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- n3 = del(n3,n2);
- N[cur++] = '0' + n3.l;
- n = n2;
- }while( n.h || n.l );
- revstr(N);
- }
- int inline charcnt(const char *ptr, unsigned char ch)
- {
- int count = 0;
- while(*ptr)
- {
- if(ch == *ptr)
- {
- count++;
- }
- ptr++;
- }
- return count;
- }
- int main(void)
- {
- char N[22]="";
- int n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;
- for(n9 = 1 ; n9 <= 21 ; n9++)
- for(n8 = 0 ; n8 <= 21 - n9 ; n8++)
- for(n7 = 0 ; n7 <= 21 -n9 - n8 ; n7++)
- for(n6 = 0 ; n6 <= 21 -n9 - n8 -n7 ; n6++)
- for(n5 = 0 ; n5 <= 21 -n9 - n8 -n7 -n6; n5++)
- for(n4 = 0 ; n4 <= 21 -n9 - n8 -n7 -n6 -n5 ; n4++)
- for(n3 = 0 ; n3 <= 21 -n9 - n8 -n7 -n6 -n5 -n4 ; n3++)
- for(n2 = 0 ; n2 <= 21 -n9 - n8 -n7 -n6 -n5 -n4 -n3; n2++)
- for(n1 = 0 ; n1 <= 21 -n9 - n8 -n7 -n6 -n5 -n4 -n3 -n2; n1++)
- {
- n0 = 21 - n9 - n8 - n7 - n6 - n5 - n4
- - n3 - n2 - n1;
- bignumber sum = { 0 };
- sum.l = n1;
- sum = add(sum, get_n_i(2, n2));
- sum = add(sum, get_n_i(3, n3));
- sum = add(sum, get_n_i(4, n4));
- sum = add(sum, get_n_i(5, n5));
- sum = add(sum, get_n_i(6, n6));
- sum = add(sum, get_n_i(7, n7));
- sum = add(sum, get_n_i(8, n8));
- sum = add(sum, get_n_i(9, n9));
- static long loop;
- ++loop;
- if(sum.h <= 4)
- continue;
- //开始校验
- // 转化为字符串
- to_str(sum, N );
- // 统计 0 1 2 3 4 5 6 7 8 9 的个数
- if(charcnt(N,'9') == n9)
- if(charcnt(N,'8') == n8)
- if(charcnt(N,'7') == n7)
- if(charcnt(N,'6') == n6)
- if(charcnt(N,'5') == n5)
- if(charcnt(N,'4') == n4)
- if(charcnt(N,'3') == n3)
- if(charcnt(N,'2') == n2)
- if(charcnt(N,'1') == n1)
-
- {
- printf("%ld 种组合\n", loop);
- printf("the number is %s\n", N);
- }
- memset(N,0,22);
- }
- return EXIT_SUCCESS;
- }
复制代码 |
|