zoukankan      html  css  js  c++  java
  • (CareerCup)Find next higher number with same digits

    题目地址:http://www.careercup.com/question?id=4869907900530688

    Find next higher number with same digits. 
    Example 1 : if num = 25468, o/p = 25486 
    Example 2 : if num = 21765, o/p = 25167 
    Example 3 : If num = 54321, o/p = 54321 (cause it's not possible to gen a higher num than tiz with given digits ).

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace FindNextHigherNumberWithSameDigits
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Do();
    14         }
    15 
    16         static void Do()
    17         {
    18             Console.WriteLine("plz input number: ");
    19             int input = Convert.ToInt32(Console.ReadLine());
    20             var arrayInput = input.ToString().ToCharArray();
    21             Find(ref arrayInput);
    22             Console.WriteLine("result : " + string.Join("", arrayInput));
    23             Do();
    24         }
    25 
    26         static void Find(ref char[] array)
    27         {
    28             int indexFirst = 0;
    29             int indexLast = array.Length - 1;
    30             bool hasChanged = false;
    31             for (int i = array.Length - 1; i > 0; i--)
    32             {
    33                 //find the number larger than before one
    34                 if (array[i] > array[i - 1])
    35                 {
    36                     hasChanged = true;
    37                     indexLast = i;
    38                     indexFirst = i - 1;
    39                     //find the number smaller than the number we found 
    40                     //and larger than the previous number of the number we found
    41                     for (int a = array.Length - 1; a >= i; a--)
    42                     {
    43 
    44                         if (array[a] > array[i - 1] && array[a] < array[i] && array[a] <= array[indexLast])
    45                         {
    46                             indexLast = a;
    47                             indexFirst = i - 1;
    48                         }
    49                     }
    50                     break;
    51                 }
    52             }
    53             //if the number is descending, return .
    54             if (!hasChanged) return;
    55 
    56             var temp = array[indexFirst];
    57             array[indexFirst] = array[indexLast];
    58             array[indexLast] = temp;
    59             //sort the number after previous position
    60             Array.Sort(array, indexFirst + 1, array.Length - indexFirst - 1);
    61         }
    62     }
    63 }
  • 相关阅读:
    dedecms为导航栏目添加英文标题
    网页设计中一些小功能
    less使用总结
    canvas图形库
    前端面试总结三
    前端面试总结二
    DOM节点中获取文本易混淆的属性
    前端面试总结
    git 学习使用总结三(远程仓库操作)
    git 学习使用总结二(远程仓库操作)
  • 原文地址:https://www.cnblogs.com/naonaoye/p/find-next-higher-number-with-same-digits.html
Copyright © 2011-2022 走看看