fork download
  1. /*
  2.  * File: main.c
  3.  * Author: xxxx
  4.  *
  5.  * Created on 2013/02/12, 12:09
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #define MAXDECIMAL (24*1000000)
  10. #define MAXBINARY (60000000)
  11. #define TABLEWIDTH (1000000)
  12.  
  13. void makemultable();
  14. int* makelongdecimal();
  15. void calc(int);
  16. void decans(void);
  17. void clearmemory(void);
  18. void dispans(void);
  19. void dispans2(int);
  20.  
  21. char *b;
  22. int *ans;
  23. int blength = 0, anslength = 1;
  24. int multable[TABLEWIDTH][2];// 2倍しかしない
  25. int main(int argc, char** argv) {
  26. printf("start makemultable\n");
  27. makemultable();
  28. printf("start makelongdecimal(ans)\n");
  29. ans = makelongdecimal();
  30. printf("start calc(578851)\n");
  31. calc(578851);
  32. printf("start decans\n");
  33. decans();
  34. printf("start dispans\n");
  35. dispans();
  36. printf("start clearmemory\n");
  37. clearmemory();
  38. return (EXIT_SUCCESS);
  39. }
  40. void makemultable(void){
  41. int j, val;
  42. for(j = 0; j < TABLEWIDTH; j++){
  43. val = 2 * j;
  44. if(val > TABLEWIDTH){
  45. val -= TABLEWIDTH;
  46. multable[j][1] = 1;
  47. } else
  48. multable[j][1] = 0;
  49. multable[j][0] = val;
  50. }
  51. }
  52. int* makelongdecimal(){
  53. int *ptr;
  54. ptr = (int*)calloc(MAXDECIMAL / 6, sizeof(int));
  55. if(ptr == NULL){
  56. fprintf(stderr, "Can't allocate memory!\n");
  57. exit(-1);
  58. }
  59. return ptr;
  60. }
  61. void calc(int val){
  62. int i, j, flg = 0, *base, tmp;
  63. *ans = 1;
  64. for(j = 0; j < val; j++){
  65. //printf("Loop %d: ", j); dispans();
  66. // ansを2倍する
  67. base = ans;
  68. for(i = 0; i < anslength; i++) {
  69. tmp = multable[*base][0]; // *base = ans * 2; (6桁のみ)
  70. if(flg == 1)
  71. ++tmp;
  72. flg = multable[*base][1];
  73. if(flg == 1 && i == anslength - 1) //最上位桁があふれしたらansを一桁増やす
  74. ++anslength;
  75. *base = tmp;
  76. ++base;
  77. }
  78. }
  79. }
  80. void decans(void){
  81. int i, *base = ans;
  82. for(i = 0; i < anslength; i++){
  83. if(*base == 0)
  84. *base = 999999;
  85. else{
  86. *base -= 1;
  87. break;
  88. }
  89. ++base;
  90. }
  91. }
  92. void dispans(){
  93. int i, k;
  94. for(i = anslength - 1; i >= 0; --i){
  95. k = *(ans + i);
  96. if(k > 9999){
  97. dispans2(k / 10000);
  98. k %= 10000;
  99. }else
  100. dispans2(0);
  101. if(k > 99){
  102. dispans2(k / 100);
  103. k %= 100;
  104. }else
  105. dispans2(0);
  106. dispans2(k);
  107. }
  108. printf(" length:%d \n", anslength);
  109. }
  110. void dispans2(int val){
  111. if(val > 9){
  112. printf("%d", val / 10);
  113. val %= 10;
  114. }else
  115. printf("0");
  116. printf("%d", val);
  117. }
  118. void clearmemory(void){
  119. free(b);
  120. free(ans);
  121. }
Runtime error #stdin #stdout 0.02s 25272KB
stdin
Standard input is empty
stdout
Standard output is empty