Exemplo n.º 1
0
bool symmetry_group_walker::nextsub (int g) {
        bool of = next_perm (perms[g]);
        if (of && g > 0)
                of = nextsub (g - 1);

        return of;
}
Exemplo n.º 2
0
 vector<vector<int>> permuteUnique(vector<int>&num) {
   vector<vector<int>> res;
   sort(num.begin(), num.end());
   do {
     res.push_back(num);
   } while(next_perm(num.begin(), num.end()));
   return res;        
 }
int main(void) {
	int p[] = {0,1,2,3};
	const int size = 4;

	do {
		print_array(p,size);
	} while (next_perm(p,size)); 

	return 0;
}
Exemplo n.º 4
0
int main(){
	int ring[10];
	std::string max = "";
	for(int i = 0; i < 10; i++)
		ring[i] = i + 1;
	for(int i = 1; i < 3628800; i++){ // 10! = 3628800
		if(is_valid(ring)){
			std::string s = make_string(ring);
			max = std::max(max, s);
		}
		next_perm(ring);
	}
	std::cout << max << std::endl;
	return 0;
}
Exemplo n.º 5
0
static int max_varp(double *rr,int m)
        {
        int i,j;
        char text[LLENGTH];
        char text2[LLENGTH];
    //  double a;

        for (i=0; i<m; ++i) perm1[i]=i;

        l=0L;
        tr_max=-1.0; tr_min=1e10;
        while (1)
            {
            ++l;
            comp_varp(rr,m);
            sprintf(sbuf,"\n %d %g %g",l,tr,tr_max); sur_print(sbuf);
            fprintf(f,"%g\n",tr);

            if (tr>tr_max)
                {
                tr_max=tr;
                for (i=0; i<m; ++i) perm_max[i]=perm1[i];
                }
            if (tr>=0.0 && tr<tr_min)
                {
                tr_min=tr;
                }
            if (sur_kbhit()) { i=sur_getch(); if (i=='.') break; }

            i=next_perm(m,perm1,perm2);

            if (i<0) break;
            }

        for (i=0; i<m; ++i) perm1[i]=perm_max[i];
        comp_varp(rr,m);
        for (i=0; i<m; ++i) for (j=0; j<m; ++j)
            rr11[i+m*j]=rr[perm1[i]+m*perm1[j]];
        save_varp(rr11,m,text);
        output_open(eout);
        fnconv(tr,accuracy+2,text);
        fnconv(tr_min,accuracy+2,text2);
        sprintf(sbuf,"Mvar[%s]=%s minM[%s]=%s dim=%d",
            word[1],sppois(text),word[1],sppois(text2),m);
        print_line(sbuf);
        print_line("MAT LOAD COVVAR.M,END+2 / Optimally permuted covariance matrix");
        return(1);
        }
Exemplo n.º 6
0
Arquivo: q5.c Projeto: k4rtik/CSU230
int main ()
{
	int i=0;

	printf("Enter n: ");
	scanf("%d", &n);

	if  (n <= 0) return 1;

	printf("Enter the string: ");
	while (i <= n) permutation[i++] = getchar();

	printf("\n");
	sort(); //Sorting required for implementing lexicographic ordering method.
	print_perm();
	
	while(next_perm());
	printf("\n");
	return 0;
}
int main() {
    /* Чиселки */
    int* nums;
    /* Операторы */
    char* operators;
    /*  Искомое число, колв-во цифр, итерационная переменная, есть ли ещё перестановка, результат выражения */
    int required_number, nums_count, i, perm_exists, res;

    scanf("%i", &nums_count);

    nums = (int*) malloc(sizeof(int) * nums_count);
    operators = (char*) malloc(sizeof(char) * nums_count - 1);

    for (i = 0; i < nums_count; i++) {
        scanf("%i", &nums[i]);
    }

    reset(operators, operators + nums_count - 1);

    scanf("%i", &required_number);

    /* Делаем, пока */
    do {
        res = calc(nums, operators, nums_count);
        print_expr(nums, operators, nums_count);
        printf("%i\n", res);
        /* Либо не сойдётся результат */
        if (res == required_number) break;
        perm_exists = next_perm(operators, nums_count - 1);
    } while (perm_exists); /* Либо не кончатся пеестановки */

    if (res == required_number) {
        print_expr(nums, operators, nums_count);
    } else {
        printf("IMPOSSIBLE\n");
    }

    free(operators);
    free(nums);
    return 0;
}
Exemplo n.º 8
0
int main(void){
  unsigned int digit = 10, limit = fact(10);
  unsigned int j,sum=0,hits=0;
   
  while(next_perm()){
    if(unusual()){
      results = realloc(results,(hits+1)*sizeof *results);
      results[hits] = malloc(len*sizeof *results[0]);
      memcpy(results[hits],perm,len*sizeof perm[0]);
      //printf("%d : ",j);
      printResult(results[hits]);
      hits++;
    }
    //next_perm();
  }
  calc_sum(results,hits);
  print_sum();
  //printf("sum is %d\n",sum);
  
  return 0;
}
int * implicit_enum(struct row_vertex * main_col, int lower_bound, int upper_bound, int * vertices, int vertex_num) {
  int i;
  int used_colors[vertex_num];
  int * color_count = (int *) malloc(sizeof(int));
  int color;
  int flag; //Para saber cuando se deja de ampliar la primera clique encontrada
  int out;
  int leftover = vertex_num - lower_bound; //Tamaño del resto a permutar
  //Arreglo con vértices que pertenecen a cota inferior de brelaz
  int * fixed = (int *) malloc(sizeof(int) * lower_bound);
  int fixed_s = lower_bound; //Tamaño de arreglo fijo
  //Arreglo con vértices que no están en la cota inferior de brelaz
  int * permuta = (int *) malloc(sizeof(int) * leftover);
  //Inicializar fixed
  for(i = 0; i < fixed_s; i++)
    fixed[i] = vertices[i];
  //Inicializar permuta
  for(i = 0; i < leftover; i++) 
    permuta[i] = vertices[fixed_s + i];

  int stop = 0;
  while(!stop) {

    //Inicialización de estructura de adyacencias y colores usados
    main_col_init(main_col, vertex_num); 
    initialize(used_colors, vertex_num);
    *color_count = 0;
    //Colorear vértices fijos
    color_fixed(fixed, main_col, fixed_s, used_colors, color_count); 
    flag = 1;
    out = 0;
    stop = check_perm(permuta, leftover);
    for(i = 0; i < leftover; i++) { 
      color = leastp_color(main_col, permuta[i], vertex_num);
      main_col[permuta[i]].color = color;
      if (used_colors[color] == 0) {
        (*color_count)++;
        used_colors[color] = 1;
        if (flag && *color_count > lower_bound) { //Actualizar cota inferior ?
          //Si cota inferior se hace igual a cota 
          //superior hay que retornar !
          lower_bound = *color_count; 
        }
      }
      else 
        flag = 0;    //No se sigue ampliando la clique encontrada
      if (lower_bound == upper_bound) {
        free(fixed);
        free(permuta);
        return color_count;
      }
      if (*color_count == upper_bound) {
        out = 1;
        break;
      }
      //Actualizamos vértices adyacentes con color usado
      update_color_around(main_col, permuta[i], color);
    }
    if (*color_count == lower_bound) {
      free(fixed);
      free(permuta);
      return color_count;  //Número cromático
    }
    if (*color_count < upper_bound)
      upper_bound = *color_count;
    if (out) { //Ocurrió que se alcanzó cota superior
      next_perm(permuta, leftover);      
      continue;
    }
    //Próxima permutación
    next_perm(permuta, leftover);
  } 

  free(fixed);
  int * solution = (int *) malloc(sizeof(int));
  *solution = upper_bound;
  return solution;
}