fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tagStack{
  5. int esp;
  6. int ebp;
  7. int* ptr;
  8. int ecx;
  9. int* buf;
  10. } STACK,*PSTACK;
  11.  
  12. #define esp s->esp
  13. #define ebp s->ebp
  14. #define ptr s->ptr
  15. #define ecx s->ecx
  16. #define buf s->buf
  17. #define pop(x) x=ptr[--esp]
  18. #define push(x) ptr[esp++]=x
  19. #define wellbuf 10
  20. #define label0 0
  21. #define label1 1
  22. #define label2 2
  23. #define label3 3
  24.  
  25. PSTACK CreateStack(int n,int k){
  26. PSTACK s=(PSTACK) malloc(sizeof(STACK));
  27. ptr=(int*)malloc(wellbuf*k*sizeof(int));
  28. buf=(int*)malloc(k*sizeof(int));
  29. esp=ebp=ecx=0;
  30. push(n);
  31. push(k);
  32. push(label3);
  33. push(label0);
  34. return s;
  35. }
  36.  
  37. int* dupCombination(PSTACK s){
  38. int a;
  39. d_fin:
  40. while(1)
  41. switch(pop(a)){
  42. case label0:
  43. push(ebp);
  44. ebp=esp;
  45. esp+=1;
  46. if(ptr[ebp-3]==1){
  47. buf[ecx]=ptr[ebp-4];
  48. push(label1);
  49. return buf;
  50. case label1:
  51. esp=ebp;
  52. pop(ebp);
  53. goto d_fin;
  54. }
  55. for(ptr[ebp]=0;ptr[ebp]<=ptr[ebp-4];++ptr[ebp]){
  56. buf[ecx++]=ptr[ebp];
  57. push(ptr[ebp-4]-ptr[ebp]);
  58. push(ptr[ebp-3]-1);
  59. push(label2);
  60. push(label0);
  61. goto d_fin;
  62. case label2:
  63. --ecx;
  64. }
  65. esp=ebp;
  66. pop(ebp);
  67. goto d_fin;
  68. case label3:
  69. push(label3);
  70. return NULL;
  71. }
  72. }
  73.  
  74. int main(){
  75. PSTACK s=CreateStack(8,4);
  76. int i,*p;
  77. while(p=dupCombination(s)){
  78. for(i=0;i<4;++i)
  79. printf("%d ",p[i]);
  80. printf("\n");
  81. }
  82. return 0;
  83. }
Success #stdin #stdout 0s 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