- 论坛徽章:
- 2
|
下面的程序是按照LZ的遍历算法编写的,但结果不正确.
前面的数只需0.几秒就出来了.但最后一个数老是出不来,另外中间少了几个数
- #include <stdio.h>
- unsigned f(unsigned n){
- static unsigned base[][2]={
- 1, 0, /* 0 */
- 10, 0*10 +1, /* 1 */
- 100, 1*10 +10, /* 20 */
- 1000, 20*10 +100, /* 300 */
- 10000, 300*10 +1000, /* 4000 */
- 100000, 4000*10 +10000, /* 50000 */
- 1000000, 50000*10 +100000, /* 600000 */
- 10000000, 600000*10 +1000000, /* 7000000 */
- 100000000, 7000000*10 +10000000, /* 80000000 */
- 1000000000, 80000000*10+100000000, /* 900000000 */
- };
- int i, j;
- unsigned cnt;
- cnt = 0;
- for(i = sizeof(base)/sizeof(base[0]) - 1; i >= 0; i--){
- for(j = 0; j < 10; j++){
- if(n < base[i][0]){
- if(j == 1){
- cnt += n + 1;
- }
- break;
- }
- cnt += base[i][1];
- if(j == 1){
- cnt += base[i][0];
- }
- n -= base[i][0];
- }
- }
- return cnt;
- }
- int k(unsigned n){
- static unsigned base[] = {
- 1,
- 10,
- 100,
- 1000,
- 10000,
- 100000,
- 1000000,
- 10000000,
- 100000000,
- 1000000000,
- };
- int i;
- for(i = 0; i < sizeof(base)/sizeof(base[0]); i++){
- if(n > base[i]){
- break;
- }
- }
- return i + 1;
- }
- unsigned max(unsigned a, unsigned b){
- return a > b ? a : b;
- }
- int main(void){
- static unsigned x[] = {
- 1,
- 199981,
- 199982,
- 199983,
- 199984,
- 199985,
- 199986,
- 199987,
- 199988,
- 199989,
- 199990,
- 200000,
- 200001,
- 1599981,
- 1599982,
- 1599983,
- 1599984,
- 1599985,
- 1599986,
- 1599987,
- 1599988,
- 1599989,
- 1599990,
- 2600000,
- 2600001,
- 13199998,
- 35000000,
- 35000001,
- 35199981,
- 35199982,
- 35199983,
- 35199984,
- 35199985,
- 35199986,
- 35199987,
- 35199988,
- 35199989,
- 35199990,
- 35200000,
- 35200001,
- 117463825,
- 500000000,
- 500000001,
- 500199981,
- 500199982,
- 500199983,
- 500199984,
- 500199985,
- 500199986,
- 500199987,
- 500199988,
- 500199989,
- 500199990,
- 500200000,
- 500200001,
- 501599981,
- 501599982,
- 501599983,
- 501599984,
- 501599985,
- 501599986,
- 501599987,
- 501599988,
- 501599989,
- 501599990,
- 502600000,
- 502600001,
- 513199998,
- 535000000,
- 535000001,
- 535199981,
- 535199982,
- 535199983,
- 535199984,
- 535199985,
- 535199986,
- 535199987,
- 535199988,
- 535199989,
- 535199990,
- 535200000,
- 535200001,
- 1111111110,
- };
- unsigned n;
- unsigned m;
- int i;
- for(i = 0; i < sizeof(x)/sizeof(x[0]); i++){
- m = f(x[i]);
- if(m != x[i]){
- printf("%u\n", x[i]);
- }
- }
- printf("my result\n");
- n = 1;
- while(n < ~0){
- m = f(n);
- if(m == n){
- printf("%u\n", m);
- n++;
- }else if(m > n){
- n = m;
- }else{
- m = n + (n - m);
- n = max(m / k(m), n + 1);
- }
- }
- return 0;
- }
复制代码 |
|