Cod sursa(job #1693606)

Utilizator sdwolfSDWOLF sdwolf Data 23 aprilie 2016 15:48:47
Problema Combinari Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.07 kb
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main() {
  FILE *input, *output;
  int limit, size, index, position, current;
  int *stack;

  input  = fopen("combinari.in", "r");
  fscanf(input, "%d %d\n", &limit, &size);
  fclose(input);

  stack = (int*) malloc(sizeof(int) * (size));

  for(index = 0; index < size; index += 1) {
    stack[index] = 0;
  }

  output = fopen("combinari.out", "w");

  position = 0;
  while (position > -1) {
    if (position < size) {
      current = stack[position];

      if (current <= limit - (limit - position) + 1) {
        if (position != 0 && stack[position] <= stack[position - 1]) {
          stack[position] += stack[position - 1] + 1;
        } else {
          stack[position] += 1;
        }
        position += 1;
      } else {
        stack[position] = 0;
        position -= 1;
      }
    } else {
      stack[position] = 0;
      position -= 1;

      for(index = 0; index < size; index += 1) {
        fprintf(output, "%d ", stack[index]);
      }
      fprintf(output, "\n");
    }
  }

  fclose(output);

  return 0;
}