(CareerCup)Find next higher number with same digits
2014-04-03 12:50
281 查看
题目地址: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 ).
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 ).
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FindNextHigherNumberWithSameDigits { class Program { static void Main(string[] args) { Do(); } static void Do() { Console.WriteLine("plz input number: "); int input = Convert.ToInt32(Console.ReadLine()); var arrayInput = input.ToString().ToCharArray(); Find(ref arrayInput); Console.WriteLine("result : " + string.Join("", arrayInput)); Do(); } static void Find(ref char[] array) { int indexFirst = 0; int indexLast = array.Length - 1; bool hasChanged = false; for (int i = array.Length - 1; i > 0; i--) { //find the number larger than before one if (array[i] > array[i - 1]) { hasChanged = true; indexLast = i; indexFirst = i - 1; //find the number smaller than the number we found //and larger than the previous number of the number we found for (int a = array.Length - 1; a >= i; a--) { if (array[a] > array[i - 1] && array[a] < array[i] && array[a] <= array[indexLast]) { indexLast = a; indexFirst = i - 1; } } break; } } //if the number is descending, return . if (!hasChanged) return; var temp = array[indexFirst]; array[indexFirst] = array[indexLast]; array[indexLast] = temp; //sort the number after previous position Array.Sort(array, indexFirst + 1, array.Length - indexFirst - 1); } } }
相关文章推荐
- [面试题] Find next higher number with same digits
- Find next greater number with same set of digits
- Given a number,find the next higher number using the same digits in the number.
- [leetcode]31. Next Permutation -Find next greater number with same set of digits
- Next Number with Same Set of Digits
- [LeetCode] Next Permutation - Next bigger number with the same set of digits
- Next higher number with same number of set bits
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- rsync - find - perl - Super fast delete a folder with large number of files
- [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
- [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- 素数遍历方法比较(Find the prime number, with performance monitoring function)
- [CareerCup][Google Interview] Find kth number in a BST
- [CareerCup][Google Interview] Find kth number in a BST
- Setup has detected at least another Rational product with a higher version number.
- Find n‘th number in a number system with only 3 and 4
- Find n‘th number in a number system with only 3 and 4
- Find the smallest number whose digits multiply to a given number n
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.