Cod sursa(job #1728158)

Utilizator TiiberiuBujor Tiberiu-Cosmin Tiiberiu Data 12 iulie 2016 13:11:31
Problema Combinari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <stdio.h>
#include <stdlib.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 - (size - position)){
              if(position && stack[position] < stack[position-1])
                  stack[position] += stack[position-1] +1;
              else
                  stack[position]++;
           position++;   
          }
       else{
            stack[position] = 0;
            position--;
          }
      }             
      else{
          position--;
          for(index = 0;index < size;++index)
              if(index ==  size - 1)
                fprintf(output,"%d ",stack[index]);
              else
                  fprintf(output,"%d",stack[index]);
          
          fprintf(output,"\n");
      }
  }
 
  fclose(output);
 
  return 0;
}