CordWars之C#0005

题目:

The input is a string str of digits. Cut the string into chunks (a chunk here is a substring of the initial string) of size sz (ignore the last chunk if its size is less than sz).

If a chunk represents an integer such as the sum of the cubes of its digits is divisible by 2, reverse that chunk; otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.

If

  • sz is <= 0 or if str is empty return ""
  • sz is greater (>) than the length of str it is impossible to take a chunk of size sz hence return "".
  • 百度翻译

  输入是一串str数字。将字符串剪切为大小的块(这里的块是初始字符串的子字符串)
    sz(如果其大小小于,则忽略最后一个块sz)。

如果一个块表示一个整数,例如其数字的多维数据集的总和可以被2整除,则反转该块;
否则将其向左旋转一个位置。将这些修改后的块放在一起并将结果作为字符串返回。

 Examples:
    revrot("123456987654", 6) --> "234561876549"
    revrot("123456987653", 6) --> "234561356789"
    revrot("66443875", 4) --> "44668753"
    revrot("66443875", 8) --> "64438756"
    revrot("664438769", 8) --> "67834466"
    revrot("123456779", 8) --> "23456771"
    revrot("", 8) --> ""
    revrot("123456779", 0) --> "" 
    revrot("563000655734469485", 4) --> "0365065073456944"

我的答案

 public static string RevRot(string strng, int sz)
    {
        // your code
        if (sz<=0||sz>strng.Length||String.IsNullOrEmpty(strng))
        {
            return "";
        }
        else
        {
            int count = strng.Length / sz;
            StringBuilder stringBuilder = new StringBuilder(count * sz);
            if (sz==1)
            {
                return strng;
            }
            else if(sz==2)
            {
                for (int i = 0; i < sz*count; i=i+2)
                {
                    stringBuilder.Append(strng[i + 1]).Append(strng[i]);
                }
                return stringBuilder.ToString();
            }
            else
            {
                for (int i = 0; i < sz * count; i = i + sz)
                {
                    char[] tempsz=  strng.Substring(i, sz).ToCharArray();
                    int number = 0;
                    for (int k = 0; k < tempsz.Length; k++)
                    {
                        number +=int.Parse(tempsz[k].ToString());
                    }
                    if (number%2==0)
                    {
                        //反转
                        Array.Reverse(tempsz);                        
                    }
                    else
                    {
                        char tempChar = tempsz[0];
                        /*
                         0   1   2
                         1   2   3
                         2   3   1
                         */
                        //左移一位
                        for (int f = 0; f < tempsz.Length-1; f++)
                        {                           
                            tempsz[f] = tempsz[f + 1];
                        }
                        tempsz[tempsz.Length - 1] = tempChar;                     
                    }
                    stringBuilder.Append(tempsz);
                }
                return stringBuilder.ToString();
            }
        }

比较牛的答案一“:

using System;
using System.Linq;

public class Revrot 
{
    public static string RevRot(string strng, int sz)
    {
        if (String.IsNullOrEmpty(strng) || sz <= 0 || sz > strng.Length)
            return String.Empty;

        return
            new String(
                Enumerable.Range(0, strng.Length/sz)
                    .Select(i => strng.Substring(i*sz, sz))
                    .Select(
                        chunk =>
                            chunk.Sum(digit => (int) Math.Pow(int.Parse(digit.ToString()), 3))%2 == 0
                                ? chunk.Reverse()
                                : chunk.Skip(1).Concat(chunk.Take(1)))
                    .SelectMany(x => x)
                    .ToArray());
    }
}

比较牛答案二:

using System;
using System.Linq;

public class Revrot 
{
    public static string RevRot(string s, int sz)
    {
        if (sz==0||s=="") return "";
        var rs=Enumerable.Range(0,s.Length/sz).Select((x,i)=>s.Skip(i*sz).Take(sz).ToArray());
        var rs1=rs.Select(x=>x.Count(y=>(int)y%2==1)).ToArray();
        return string.Join("",rs.Select((x,i)=>string.Join("",rs1[i]%2==1 ? x.Skip(1).Concat(new char[]{x[0]}) : x.Reverse())));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41995872/article/details/83538542
今日推荐