コード例 #1
0
 public RemainderState(int remainder, int distance, int digit, RemainderState previous)
 {
     this.Remainder = remainder;
     this.Distance  = distance;
     this.Digit     = digit;
     this.Previous  = previous;
 }
コード例 #2
0
        public static String Construct(int a)
        {
            Dictionary <int, RemainderState> states = new Dictionary <int, RemainderState>();
            SortedSet <int> queueRemainders         = new SortedSet <int>();
            Queue <int>     queue = new Queue <int>();

            RemainderState currentState = new RemainderState(RemainderState.START_REMAINDER, 0, 0, null), oldState;

            queue.Enqueue(currentState.Remainder);
            states[currentState.Remainder] = currentState;
            queueRemainders.Add(currentState.Remainder);

            while (queue.Count > 0)
            {
                currentState = states[queue.Dequeue()];
                queueRemainders.Remove(currentState.Remainder);

                foreach (RemainderState newState in currentState.Visit(a))
                {
                    if (states.TryGetValue(newState.Remainder, out oldState))
                    {
                        if (oldState.Distance > newState.Distance || oldState.Distance == newState.Distance && oldState.Digit > newState.Digit)
                        {
                            states[newState.Remainder] = newState;
                        }
                    }
                    else
                    {
                        states[newState.Remainder] = newState;
                        queue.Enqueue(newState.Remainder);
                    }
                }
            }

            if (states.TryGetValue(0, out currentState))
            {
                StringBuilder sb = new StringBuilder();
                while (currentState.Remainder != RemainderState.START_REMAINDER)
                {
                    sb.Append(currentState.Digit);
                    currentState = currentState.Previous;
                }

                return(sb.ToString());
            }
            return("Nope");
        }