fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. //#define FIN "submultimi.in"
  4. //#define FOUT "submultimi.out"
  5.  
  6.  
  7. typedef struct node {
  8. int info;
  9. struct node *next;
  10. } Stack;
  11.  
  12. void push(Stack**head, int value){
  13. Stack*q=(Stack*)malloc(sizeof(Stack));
  14. q->info = value;
  15. q->next = *head;
  16. *head = q;
  17. }
  18.  
  19. int isEmpty(Stack*head) {
  20. if(head==NULL) return 1;
  21. return 0;
  22. }
  23.  
  24. void pop(Stack**head) {
  25. Stack*q;
  26. q = *head;
  27. if(isEmpty(*head)) return;
  28. (*head) = (*head)->next;
  29. free(q);
  30. }
  31.  
  32.  
  33. int top(Stack**head){
  34. if(!isEmpty(*head))
  35. return (*head)->info;
  36. return -1;
  37. }
  38.  
  39. void topPlusPlus(Stack**head) {
  40.  
  41. (*head)->info = (*head)->info + 1;
  42. }
  43.  
  44. void write(Stack*head) {
  45.  
  46. int arr[100], k = 0;
  47.  
  48. while(head){
  49. arr[k++] = head->info;
  50. head = head->next;
  51. }
  52. for(int i = k-1; i >= 0; i--) printf("%d ", arr[i]);
  53. }
  54.  
  55. int main(int argc, char const *argv[]) {
  56.  
  57. Stack*head=NULL;
  58. int n;
  59.  
  60. //freopen(FIN, "r",stdin);
  61. //freopen(FOUT,"w",stdout);
  62.  
  63. scanf("%d",&n);
  64. push(&head,1);
  65. while(!isEmpty(head)) {
  66. write(head);
  67. printf("\n");
  68. if(top(&head) < n) push(&head,top(&head)+1);
  69. else {
  70. pop(&head);
  71. if(!isEmpty(head)) topPlusPlus(&head);
  72. }
  73. }
  74. return 0;
  75. }
Success #stdin #stdout 0.01s 5380KB
stdin
4
stdout
1 
1 2 
1 2 3 
1 2 3 4 
1 2 4 
1 3 
1 3 4 
1 4 
2 
2 3 
2 3 4 
2 4 
3 
3 4 
4