| |

VerySource

 Forgot password?
 Register
Search
View: 1028|Reply: 7

Exhaustive algorithm

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-19 19:00:01
| Show all posts |Read mode
Want an instance
For example enter 123
To get all the permutations
123,132,213,231,312,321, a total of 3! = 6 types
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-28 18:18:01
| Show all posts
Separate the numbers into an array, and then traverse the combination
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-28 23:54:01
| Show all posts
The rotation method produces a full permutation.
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-30 17:27:01
| Show all posts
Please post the code, thank you!
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-1 19:00:02
| Show all posts
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program // Calculate P (M, N) M = 3; N = 3
    {
        static int Count = 0; // stores the number of permutations and combinations.
        static void Main (string [] args)
        {
            int M = 3;
            int N = 3;
            int [] a = new int [M];

            for (int i = 1; i <= M; i ++) // The array is assigned the initial value "1,2,3"
            {
                a [i-1] = i;
            }
            if (M <N)
            {
            }
            else
            {
                getValue (a, 0, N);
                Console.WriteLine (Count); // Number of output combinations
                Console.Read ();
            }

        }

        static void getValue (int [] a, int k, int n)
        {
            int temp;
            if (k> = n) // output:
            {
                string s = "";
                for (int i = 0; i <n; i ++)
                {
                    s = s + a [i] .ToString () + "";
                }
                Count ++;
                Console.WriteLine (s);
            }
            else
            {
                for (int i = k; i <a.Length; i ++)
                {
                    temp = a [i];
                    a [i] = a [k];
                    a [k] = temp;
                    getValue (a, k + 1, n); // recursive call
                    temp = a [k];
                    a [k] = a [i];
                    a [i] = temp;
                }
            }
        }
    }
}
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-4 15:45:01
| Show all posts
If I want to add a full permutation with 1,2 bits, such as 1,2,3,12,13,21,31,23,32, etc., how should I write it?
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-20 19:30:02
| Show all posts
For example, 4 numbers 1, 2, 3, 4, the number of results I want to get is A41+A42+A43+A44=64 types
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Singapore

Post time: 2020-7-25 08:30:01
| Show all posts
using System;
using System.Collections;

namespace MyNameSpace
{
    class GenerateWords
    {
        public static void Main()
        {
            string str = "1234";
            ArrayList result = new ArrayList();
            ArrayList kinds = Generate_Permutations1(str);
            for (int i = 0; i <kinds.Count; i++)
            {
                ArrayList res = Generate_Permutations(kinds[i].ToString());
                result.AddRange(res);
            }
            
            Console.WriteLine("Count = "+ result.Count);
            for (int i = 0; i <result.Count; i++)
            {
                Console.WriteLine(result[i]);
            }
        }
        public static ArrayList Generate_Permutations(string word)
        {
            ArrayList result = new ArrayList();
            if (word.Length == 1)
            {
                result.Add(word);
                return result;
            }
            for (int i = 0; i <word.Length; i++)
            {
                string shorter_word = word.Substring(0, i) + word.Substring(i + 1, word.Length-i-1);
                ArrayList shorter_Permutations = Generate_Permutations(shorter_word);
                for (int j = 0; j <shorter_Permutations.Count; j++)
                {
                    string longer_word = word[i].ToString() + shorter_Permutations[j].ToString();
                    result.Add(longer_word);
                }
            }
            return result;
        }

        public static ArrayList Generate_Permutations1(string word)
        {
            ArrayList result = new ArrayList();
            result.Add(word[word.Length-1]);
            if (word.Length <= 1)
                return result;
            for (int i = word.Length-2; i >= 0; i--)
            {
                int count = result.Count;
                for (int j = 0; j <count; j++)
                {
                    result.Add(word[i].ToString() + result[j].ToString());
                }
                result.Add(word[i].ToString());
            }
            return result;
        }

    }
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list