spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Every possible combination algorithm


For a given n there are always 2^n ways, as for each position we can choose 2 differents symbols. For a general number of symbols, the usual approach would be backtracking, but since you only have two symbols, an easier approach using bitmasks works.

Notice that the numbers between 0 and 2^n - 1 written in binary contain all possible bitmasks of lenght n, so you can just "print the numbers in binary, writing A or B depending if the bit is a 0 or a 1.

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < (1 << n); ++i) {
    for (int j = 0; j < n; ++j) {
      cout << (((i >> j) & 1) ?
"B" : "A");
    }
    cout << endl;
  }
}

Categories : Algorithm

Related to : Every possible combination algorithm
Algorithm: Cheapest product package combination
This is an NP-Complete problem. For example, you can encode subset-sum problems into it by including entries like N photos for N dollars for various N and then asking if you have to pay more than necessary. Because it's NP-Complete, there are no known solutions that scale well. But if your problem is small you can just brute force the solution. Basically, you want something like: bestCombinatio

Categories : PHP
Awk with combination of cut and if
You could do it with: awk '$3 ~ /:0$/{print $4-1;next}{print $4}' test.txt -0.238 0.754 0.848 0 0.851 If the 3rd column ends with :0 ($3 ~ /:0$/) then print the 4th column minus 1; else print the 4th column.

Categories : Unix
PHP insert combination to mysql
You can create array to avoid duplicate data $dupList=array(); //declare this array before loop //Hold $lista[$i] and $lista[$j] jointly in a variable $newVal=$lista[$i].$lista[$j]; if (!in_array($newVal, $dupList)) { $sql="INSERT INTO table10(id) VALUES ($newVal)"; mysql_query( $sql, $con ); array_push($dupList,$newVal); }

Categories : PHP
Angular UI state in combination with service
You could use a separate array for your attribute, like this: angular.controller('AddUsersCtrl', function ($scope, UsersService) { $scope.users = []; $scope.toggledUsers = {}; function toggleUser (user) { $scope.toggledUsers[user] = !$scope.toggledUsers[user]; } function addAll () var users = $scope.users; UsersService .addMany(users)

Categories : Angularjs
TypeConverter on Id Column in combination with jpql
You need to use Barcode objects in your application. Your JPA implementation (e.g. Hibernate) will use the converter for every database access to do the conversion. You can find an example in my github repo: https://github.com/thjanssen/ColorConverter/blob/master/Converter/src/test/java/blog/thoughts/on/java/jpa21/TestColorConverter.java#L52

Categories : Java
Recently Add
Proving optimality for a new algorithm that finds minimum spanning tree
why this assembly piece of code do jmp forever
Find out if segment is fully inside of polygon
Algorithm for coloring a hexagon tile map with minimum distance (3) for reoccurring colors
Sort pairs to be more consecutive
To find three unique numbers whose number of occurrence is even
Dealing with duplication between unit and integration tests
reflection and symmetry in back tracking queens
Big O analysis for method with multiple parameters
Divide Huge Array of Numbers in Buckets
Algorithm to find adjacent cells in a matrix
Why this code gives WA for Petersen Graph(codechef)?
Complexity of this prime number search algorithm
How to detect if a file has changed?
Given string x,y and z. Determine if z is a shuffle
Basic decryption for simple encryption algorithm
An efficient way to assign user_ids to huge dataset under certain conditions
What's a more efficient implementation of this puzzle?
Generating prime numbers in poly-time
What if I do not use G transpose in calculating Strongly Connected Components?
Dividing an array into optimum no of equal sum sublists
Counting derangements
How to iterate through all cases when partitioning objects
Algorithm: How to find closest element, having coordinates and dimension
Developing player rankings with ELO
How to transform two set of discrete points ( vectors ) to help plotting them on a common scale
Heap Sort Space Complexity
complex root finding algorithm
Every possible combination algorithm
RSA Cryptosystem - Retrieve m
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.