Guest User

Untitled

a guest
Nov 12th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. [1,1]
  2. [0,1]
  3. [1,0]
  4. [1,1]
  5.  
  6. [1,1,1]
  7. [0,1,1]
  8. [1,0,1]
  9. [0,0,1]
  10. [1,1,0]
  11. [0,1,0]
  12. [1,0,0]
  13. [0,0,0]
  14.  
  15. 0 0 1
  16. 1 0 0
  17. 0 0 32563
  18. 0 32765 0
  19. 32765 0 0
  20. 1664188935 3 -1
  21.  
  22. #include<stdio.h>
  23.  
  24. int pow_2(int digit); /*2の引数乗を戻す*/
  25. void make_binary(int len1,int len2, int n, int (*a)[n]);
  26. /*len1(=len2)×nの二次元配列に0または1を代入し,n桁?の全ての2進数を作成*/
  27. int main(void){
  28. int i,j;
  29. int digit,len;
  30. digit=3;
  31. len=pow_2(digit);
  32. int a[len][digit]; /*ここでは,3桁の二進数(先頭が0でも良い)が8通り存在するので8×3の配列を用意*/
  33. make_binary(len, len, digit-1,a);
  34.  
  35. for(i=0; i<len; i++)『
  36. for(j=0; j<digit; j++){
  37. printf("%d ",a[i][j]);
  38. }
  39. printf("n");
  40. }
  41.  
  42. return 0;
  43. }
  44. int pow_2(int digit){
  45. int i;
  46. int a=1;
  47. for(i=0; i<digit; i++){
  48. a=a*2;
  49. }
  50. return a;
  51. }
  52. void make_binary(int len1,int len2, int n, int (*a)[n]){
  53. int i,j;
  54. i=0;
  55. if (n==-1){
  56. return ;
  57. }
  58. else{
  59. while(i<len1){
  60. for(j=0; j<len2/2; j++){
  61. printf("%d %d ",i,j);
  62. printf("n");
  63. a[i++][n]=1;
  64. }
  65. for(j=len2/2; j<len2; j++){
  66. printf("%d %d ",i,j);
  67. printf("n");
  68. a[i++][n]=0;
  69. }
  70. }
  71. make_binary(len1, len2/2, n-1, a);
  72. }
  73. }
  74.  
  75. void make_binary(int len1,int len2, int n, int (*a)[n])
  76.  
  77. void make_binary(int len1,int len2, int x, int y, int a[x][y])
  78.  
  79. make_binary(len1, len2, (max - 1), (digit -1), a);
  80.  
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <math.h>
  84.  
  85. void makeArray(int digit);
  86.  
  87. int main(int argc, const char * argv[]) {
  88. // insert code here...
  89. int digit = atoi(argv[1]);
  90. makeArray(digit);
  91.  
  92. return 0;
  93. }// end int main
  94.  
  95. void makeArray(int digit)
  96. {
  97. int max = pow(2, digit);
  98. // allocate allay
  99. unsigned short array[max][digit];
  100. // create data
  101. int currentBit = 0;
  102. int value = 0;
  103. for (value = max - 1; value >= 0; value--) { // 最大値 -1 から0まで逆順に
  104. for (currentBit = digit - 1; currentBit >= 0; currentBit--) { // 大きい方のビットから小さい方のビットへ
  105. // ビット演算の値が0なら、0, そうでないならそのビットは1
  106. array[value][currentBit] = (value & (1 << currentBit)) == 0 ? 0 : 1;
  107. }// end foreach bit
  108. }// end foreach value
  109.  
  110. // print value
  111. for (value = max - 1; value >= 0; value--) {
  112. printf("%d : [", value);
  113. for (currentBit = digit - 1; currentBit >= 0; currentBit--) {
  114. if (currentBit != 0) {
  115. printf("%d,", array[value][currentBit]);
  116. } else {
  117. printf("%d", array[value][currentBit]);
  118. }
  119. }// end foreach bit
  120. printf("]n");
  121. }// end foreach value
  122. }// end int **makeArray
  123.  
  124. void make_binary(int len1,int len2, int n, int (*a)[n]);
  125.  
  126. make_binary(len, len, digit-1,a);
  127.  
  128. make_binary(len1, len2/2, n-1, a);
  129.  
  130. (オフセットは`int`単位)
  131. +-----------+-----------+-----------+
  132. a -> |[0][0]-> +0|[0][1]-> +1|[0][2]-> +2|
  133. +-----------+-----------+-----------+
  134. |[1][0]-> +3|[1][1]-> +4|[1][2]-> +5|
  135. +-----------+-----------+-----------+
  136. |... |
  137.  
  138. +-----------+-----------+
  139. a -> |[0][0]-> +0|[0][1]-> +1|
  140. +-----------+-----------+
  141. |[1][0]-> +2|[1][1]-> +3|
  142. +-----------+-----------+
  143. |[2][0]-> +4|[2][1]-> +5|
  144. +-----------+-----------+
  145. |... |
  146.  
  147. void make_binary(int len1, int len2, int n, int size, int (*a)[size]);
  148.  
  149. make_binary(len, len, digit-1, digit, a);
  150.  
  151. make_binary(len1, len2/2, n-1, size, a);
Add Comment
Please, Sign In to add comment