Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using UnityEngine;
  4.  
  5. namespace Libraries
  6. {
  7.     public class Probleme
  8.     {
  9.        
  10.         public List<int> listInput = new List<int>();
  11.         public List<List<int>> listOutput = new List<List<int>>();
  12.  
  13.  
  14.         public void Init()
  15.         {
  16.             for (int i = 1; i <= 6; i++)
  17.             {
  18.                 listInput.Add(i);
  19.             }
  20.  
  21.             listOutput = calc(listInput);
  22.            
  23.             Debug.Log(listOutput.Count + " combinaisons possibles de " + ArrayToString(listInput));
  24.            
  25.             foreach (List<int> combi in listOutput)
  26.             {
  27.                 combi.Reverse();
  28.                 Debug.Log(ArrayToString(combi));
  29.             }
  30.         }
  31.  
  32.         public string ArrayToString(List<int> list)
  33.         {
  34.             StringBuilder sb = new StringBuilder();
  35.  
  36.             sb.Append("[");
  37.             for (int i = 0 ; i < list.Count ; i++)
  38.             {
  39.                 sb.Append((i > 0?",":"") + list[i]);
  40.             }
  41.             sb.Append("]");
  42.            
  43.             return sb.ToString();
  44.         }
  45.  
  46.         public List<List<int>> calc(List<int> listCurrentInput)
  47.         {
  48.             List<List<int>> result = new List<List<int>>();
  49.  
  50.             if (listCurrentInput.Count == 1)
  51.             {
  52.                 List<int> subCombi = new List<int>(listCurrentInput);
  53.                 result.Add(subCombi);
  54.             }
  55.             else
  56.             {
  57.                 for (int i = 0; i < listCurrentInput.Count; i++)
  58.                 {
  59.                     List<int> listNewInput = new List<int>(listCurrentInput);
  60.                     listNewInput.Remove(listCurrentInput[i]);
  61.                     List<List<int>> combi = calc(listNewInput);
  62.                     foreach (List<int> subCombi in combi)
  63.                     {
  64.                         subCombi.Add(listCurrentInput[i]);
  65.                         result.Add(subCombi);
  66.                     }
  67.                 }
  68.             }
  69.  
  70.             return result;
  71.         }
  72.        
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement