Prompt the user for amount of possible numbers, the user types the numbers and then the program needs to display all the possibilities of those numbers.
For example:
Program prompts the user for amount of numbers,
lets say user types 3
Then he user has to type 3 different numbers
Lets say user types 1,2,3
Now the program needs to show all possible numbers out of those 3 numbers
so it would be 1,2,3, 12,13,21,31,23,32, etc you get the general idea.
Loading
Hemant SrivastavaPosted Jan 10, 2013, 12:25 PM
here is complete solution (Sample code is also attached..)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class PermuteNPowerSet
{
public List
{
//
var result = new List
>();(); listofNums = new List(); item in result) { 1, 2, 3 }; powerSetNumbers = oPermuteNPowerSet.GeneratePowerSet(list);
for (int i = 0; i < (1 << listOfNumbers.Count); i++)
{
// 1 << n == the n-th power of 2
var sublist = new List
for (int j = 0; j < listOfNumbers.Count; j++)
{
// analyze each bit in "i"
if ((i & (1 << j)) != 0)
{
// if the j-th bit of i is set...
sublist.Add(listOfNumbers[j]); // add the item to the current sublist
}
}
result.Add(sublist); // add the current sublist to the final result
}
List
foreach (List
{
int multiplier = 1;
int number = 0;
foreach (int digit in item)
{
int i = digit * multiplier;
number = number + i;
multiplier = multiplier * 10;
}
listofNums.Add(number);
}
return listofNums;
}
public void GeneratePermutations(string item)
{
char[] list = item.ToCharArray();
int x = list.Length - 1;
go(list, 0, x);
}
private void go(char[] list, int k, int m)
{
int i;
if (k == m)
{
Console.Write(list);
Console.WriteLine(" ");
}
else
{
for (i = k; i <= m; i++)
{
swap(ref list[k], ref list[i]);
go(list, k + 1, m);
swap(ref list[k], ref list[i]);
}
}
}
private void swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
}
class Program
{
static void Main()
{
PermuteNPowerSet oPermuteNPowerSet = new PermuteNPowerSet();
var list = new List
List
/*calling to generate permutations*/
foreach (int i in powerSetNumbers)
{
oPermuteNPowerSet.GeneratePermutations(i.ToString());
}
Console.Read();
}
}
}
Prime bPosted Jan 10, 2013, 6:53 PM
Also where did you get your degree?
And how much were your starting salary?
What languages do you know?
Hemant SrivastavaPosted Jan 10, 2013, 5:10 PM
Please mark it accepted so that others could refer to this.
Prime bPosted Jan 10, 2013, 4:59 PM
Where are you a software developer?
What do you develop?
D LPosted Jan 10, 2013, 11:42 AM
User types in 3 which becomes a new array length for an array of integers.
Then do a for loop:
for (int j = 0; j < arrayofNums.Length; j ++)
{
arrayofNums[j] = (input a number);
}
Once you have the arrayofNums filled with your numbers then you do permutations on it.
http://www.codeproject.com/Articles/37215/Permutations-in-C-Using-Recursion