Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Combinatorics.Collections;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Combinatorics
- {
- class Program
- {
- static void swap(ref int a, ref int b)
- {
- int temp = a;
- a = b;
- b = temp;
- }
- public static void Permutate(int[] arr, int n)
- {
- if (n == arr.Length - 1)
- {
- Console.WriteLine(string.Join("", arr));
- }
- else
- {
- for (int i = n; i < arr.Length; i++)
- {
- swap(ref arr[n], ref arr[i]);
- Permutate(arr, n + 1);
- swap(ref arr[n], ref arr[i]);
- }
- }
- }
- static int Binom(int n, int k)
- {
- if (k == n || k == 0)
- {
- return 1;
- }
- return Binom(n - 1, k - 1) + Binom(n - 1, k);
- }
- static void PascalTriangle(int m)
- {
- for (int n = 0; n < m; n++)
- {
- for (int k = 0; k <= n; k++)
- {
- Console.Write(Binom(n, k) + " ");
- }
- Console.WriteLine();
- }
- }
- static void Main(string[] args)
- {
- int[] a = { 1, 2, 3 };
- Permutate(a, 0);
- char[] inputSet = { 'A', 'B', 'C' };
- Permutations<char> perm = new Permutations<char>(inputSet, GenerateOption.WithRepetition);
- foreach (IList<char> l in perm)
- {
- Console.WriteLine(string.Join("", l));
- }
- Variations<char> vari = new Variations<char>(inputSet, 2, GenerateOption.WithRepetition);
- foreach (IList<char> l in vari)
- {
- Console.WriteLine(string.Join("", l));
- }
- Combinations<char> comb = new Combinations<char>(inputSet, 2, GenerateOption.WithRepetition);
- foreach (IList<char> l in comb)
- {
- Console.WriteLine(string.Join("", l));
- }
- Console.Write("Enter numbers: ");
- int[] nums = Console.ReadLine().Split(' ').Select(n => int.Parse(n)).ToArray();
- int[] signs = { 1, -1 };
- bool flag = false;
- Variations<int> s = new Variations<int>(signs, nums.Length, GenerateOption.WithRepetition);
- foreach (IList<int> l in s)
- {
- int sum = 0;
- for (int i = 0; i < nums.Length; i++)
- {
- sum += l[i] * nums[i];
- }
- if (sum == 0)
- {
- flag = true;
- for (int i = 0; i < nums.Length; i++)
- {
- Console.Write("{0}{1} ", l[i] == 1 ? '+' : '-', nums[i]);
- }
- Console.WriteLine(" = 0");
- }
- }
- if (!flag)
- {
- Console.WriteLine("There are no sum 0");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment