解决方案 »

  1.   

    Java import java.io.*;
    import java.util.*;
    import java.lang.*; 
    import java.util.Enumeration; public class LingNum
    {
        static int iData = 0;
        static int iGetLen = 0;
        static StringBuilder sb = new StringBuilder();
        static Map<Object, IList<String>> dicNums = new HashMap<Object, IList<String>>();
        static int SetLen = 0;
        static Combinations combinations ;
        static IList<String> lstNum ;
        static void Main(String[] args)
            {
                int n;
                Console console = System.console();
                String line = console.readLine("入力 %d: ");
                while ( Integer.parseInt(line)<0)
                {
                    line = console.readLine("入力 %d: ");
                }
                n = Integer.parseInt(line);
                SetLen = n;
                long startDt = System.currentTimeMillis();
                PrintData(SetLen);
                long endDt = System.currentTimeMillis();
                            
                System.out.println(endDt- startDt);
                //Console.Read();
                //if (iData == iGetLen)
                //{
                    for (Map.Entry<Object, IList<String>> kvp : dicNums.entrySet())
                    {
                        for (Object strdata : kvp.getValue())
                        {
                            System.out.println(strdata.toString());
                        }
                    }
                    System.out.println(String.format("N(%d) = %d * (%d -1) +1)", n,n,n));
                //}
                //else
                //{ System.out.println("解答不可"); }
                endDt = System.currentTimeMillis();
                System.out.println(endDt- startDt);
                console.readLine();
                
            }
            private static void PrintData(int iLen)
            {
                iData = iLen * (iLen - 1) + 1;
                IList<int> b = new IList<int>();
                //var C = new List<int>();
                for(int i = 1;i<= iLen ;i++)
                {
                    b.add(i-1, i);
                    //C.Add(i);
                }
                for (int j = 1; j <= iLen; j++)
                {
                    WriteCombinations(b, j);
                }
            }        private static <T> void WriteCombinations(IList<T> array, int length)
            {
                combinations = new Combinations(array, length);
                lstNum = new IList<String>();
                for (Object oneCom : combinations._combinations)
                {
                    //if (Utility.CheckData<T>(oneCom) == false) continue;
                    String strData = Utility.<IList<T>>GetString(oneCom);
                    //System.out.println(strData);
                    lstNum.add(lstNum.size,strData);
                    iGetLen += 1;
                }
                dicNums.put(length, lstNum);
                
                //System.out.println(combinations.ToString());
                //System.out.println();
            }                public static class Utility
            {
                 public static <T> String GetString(IList<T> list)
                {
                    StringBuilder sb = new StringBuilder();
                    int isum = 0;
                    for (T item : list)
                    {
                        sb.append(item.toString() + "+");
                        isum += Integer.parseInt(item.toString());
                    }                if (sb.length() > 0) sb.deleteCharAt(sb.length()-1) ;
                    sb.append("=" + isum);
                    return sb.toString();
                }
            }        public class Combinations<T>
            {
                public IList<T> _items;
                private int _length;
                public IList<IList<T>> _combinations;
                private int[] _finalIndices;            public <T>Combinations(IList<T> items) 
                {
                    this(items, items.size);
                }
                public <T> Combinations(IList<T> items, int select)
                {
                    if (select < 0)
                        throw new IllegalArgumentException("選択要素数は、0以上でなくてはなりません。");
                    if (items.size < select)
                        throw new IllegalArgumentException("選択要素数は、対象要素数以下でなくてはなりません。");                _items = items;
                    _length = select;
                    _combinations = new IList<IList<T>>();                if (select == 0) return;
                    _finalIndices = GetFinalIndices(select);
                    ComputeCombination();
                }            private int[] GetFinalIndices(int select)
                {
                    int[] finalindices = new int[select];                int j = select - 1;
                    for (int i = _items.size - 1; i > _items.size - 1 - select; i--)
                    {
                        finalindices[j] = i;
                        j--;
                    }
                    return finalindices;
                }            public int Count()
                {
                     return (_combinations.size != 0 ? _combinations.size : 1); 
                }
               private void ComputeCombination()
                {
                    if (_finalIndices.length == 0) return;                int[] indices;
                    for(int i= 1; i<= _length; i++)
                    {
                        indices[i-1] = i-1;
                    }
                    _combinations.add(0,GetCurrentCombination(indices));               while (indices[0] != _finalIndices [0])
                    {
                        if (NextCombination(indices))
                            _combinations.add(_combinations.size,GetCurrentCombination(indices));
                    }
                }          private IList<T> GetCurrentCombination(int[] indices)
                {
                    IList<T> oneCom = new IList<T>();
                    for (int k = 0; k < _length; k++)
                    {
                        oneCom.set(k, _items.get(indices[k]));
                    }
                    return oneCom;
                }           private boolean NextCombination(int[] indices)
                {
                    for (int j = _finalIndices.length - 1; j > -1; j--)
                    {
                        if (indices[j] < _finalIndices[j])
                        {
                            indices[j]++;
                            for (int k = 1; j + k < _finalIndices.length; k++)
                            {
                                indices[j + k] = indices[j] + k;
                            }
                            //if (indices[j] == _finalIndices[j])
                            break;
                        }
                        if (j == 0) return false;
                    }
                    IList<IList<int>> isUnable = indices.Aggregate(
                       new IList<IList<int>>(),    // TAccumulate(集計結果)の初期化
                       (accumulate, value) ->
                       {
                           if (accumulate.Count == 0)
                           {
                               accumulate.add(new IList<int>((Arrays.asList(value))));
                           }
                           else
                           {
                               var currentList = accumulate.get(accumulate.size()-1);
                               var preValue = currentList.get(currentList.size()-1);
                               if (value - preValue == 1)
                               {
                                   currentList.add(value);
                               }
                               else
                               {
                                   accumulate.add(new ArrayList<int>((Arrays.asList(value))));
                               }
                           }                       return accumulate;
                       });
                        //.Select(l => String.Join(", ", l))
                        //.ToList().ForEach(x => System.out.println(x));
                    if (isUnable.size > 2)
                    {
                        return false; 
                    }
                    else if (isUnable.size == 2)
                    {
                        if (Integer.parseInt(isUnable.get(0).get(0).toString()) == 0 && Integer.parseInt(isUnable.get(1).get(isUnable.get(1).size-1).toString()) == (_items.size-1))
                            return true;
                        else
                            return false;
                    }
                    else { return true; }
                }         public String ToString()
                {
                    return String.format("{0}C{1} Of [{2}] - {3}通り"
                            , _items.size
                                , _length
                                , Utility.GetString(_items)
                                , this.Count());
                }        }
    public class IList<E> implements Iterable {
        Node head,tail;
        public int size;
        public IList () {
    head = new Node();
    tail = new Node();
    head.next = tail;
    tail.prev = head;
    head.prev = tail.next = null;
    size = 0;
        }
        public E get(int i) {
    Node tmp = head.next;
    for (int j=0; j<i;j++) {
        tmp = tmp.next;
    }
    return tmp.e;
        }
        public E set(int i, E e){
    Node tmp = head.next;
    for (int j=0; j<i;j++) {
        tmp = tmp.next;
    }
    E d = tmp.e;
    tmp.e = e;
    return d;
        }
        public void add(int i, E e){
    Node tmp = head.next;
    for (int j=0; j<i;j++) {
        tmp = tmp.next;
    }
    Node n = new Node();
    n.e = e;
    n.prev = tmp.prev;
    n.next = tmp;
    tmp.prev.next = n;
    tmp.prev = n;
        }
        public void remove(int i){
    Node tmp = head.next;
    for (int j=0; j<i;j++) {
        tmp = tmp.next;
    }
    tmp.prev.next = tmp.next;
    tmp.next.prev = tmp.prev;
        }
        class Node {
    Node prev, next;
    E e;
        }
        public void print() {
    Node tmp = head.next;
    while(tmp != tail) {
        System.out.println(tmp.e);
        tmp = tmp.next;
    }
        }
        protected Node getNode() {
    return new Node();
        }
        public ListIterator<E> iterator () {
    ListIterator<E> i = new ListIterator<E>(this);
    return i;
        }
    }}