码迷,mamicode.com
首页 > 其他好文 > 详细

Descending Order

时间:2015-07-05 13:45:16      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

Descending Order

Description:

Your task is to make a function that can take any non-negative integer as a argument and return it with it‘s digits in descending order. Descending order means that you take the highest digit and place the next highest digit immediately after it.

Examples:

Input: 145263 Output: 654321

Input: 1254859723 Output: 9875543221

 

using System;
using System.Linq;

public static class Kata
{
  public static int DescendingOrder(int num)
  {
    // Bust a move right here
    return Convert.ToInt32(string.Join(string.Empty, num.ToString().OrderBy(c => c).Reverse()));
  }
}

 

其他人的解法:值得学习的是,str本身自带了降序排列的函数

关于string.Join以及string.Concat的区别http://www.cnblogs.com/chucklu/p/4621996.html

using System;
using System.Linq;

public static class Kata
{
  public static int DescendingOrder(int num)
  {
    return int.Parse(string.Concat(num.ToString().OrderByDescending(x => x)));
  }
}

 

Descending Order

标签:

原文地址:http://www.cnblogs.com/chucklu/p/4622011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!