scytale的编码器/解码器(古斯巴达密码)【难度:3级】:

答案1:

using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Scytale
{public class Scytale   {public static string Decode(string m, int c)     {m = m.Length%c == 0 ? m : m +  new string(' ', c - m.Length%c);   return new Regex(@"\s+$").Replace(string.Concat(m.Select((e,i)=> m[(int)(i/Math.Round((double)m.Length/c))+ i%((int)Math.Round((double)m.Length/c))*c] )),""); }public static string Encode(string m, int c)     {string n = m.Length%c == 0 ? m : m +  new string(' ', c - m.Length%c); return string.Concat(n.Select((e,i)=> n[(int)(i/c) + (int)(i*(Math.Round((double)n.Length/c)))%n.Length] ));}}
}​

答案2:

using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Scytale
{public class Scytale{public static string Decode(string m, int n){var e=Regex.Matches(m,".{1,"+n+"}").OfType<Match>().Select(x=>x.Value).ToArray();return string.Join("",Enumerable.Range(0,n).Select((x,i)=>e.Aggregate("",(a,b)=>i<b.Length ? a+b[i] : a))).Trim();}public static string Encode(string m, int n){if (m=="") return "";if(m.Length%n!=0) m+=new string(' ',n-m.Length%n-1);var len=(m.Length+1)/n;var e=Regex.Matches(m,".{1,"+len+"}").OfType<Match>().Select(x=>x.Value).ToArray();return string.Join("",Enumerable.Range(0,len).Select((x,i)=>e.Aggregate("",(a,b)=>i<b.Length ? a+b[i] : a)));}}
}​

答案3:

using System;namespace Scytale
{public class Scytale{public static string Decode(string message, int cylinderSides){string res = "";for (int i = 0; i < cylinderSides; i++)for (int j = i; j < message.Length; j += cylinderSides)res = res + message[j];while (res.Length - 1 > 0 &amp;&amp; res[res.Length - 1] == ' ')res = res.Remove(res.Length - 1, 1);return res;}public static string Encode(string message, int cylinderSides){string res = "";while (message.Length % cylinderSides != 0)message = message + " ";for (int i = 0; i <  message.Length / cylinderSides; i++)for (int j = i; j < message.Length; j+= message.Length / cylinderSides)res = res + message[j];return res;}}
}​

答案4:

using System;
using System.Text;namespace Scytale
{public class Scytale{        public static string Decode(string message, int cylinderSides){StringBuilder sb = new StringBuilder();for (int i = 0; i < cylinderSides; i++){for (int j = 0; j < message.Length; j += cylinderSides){if ( sb.Length < message.Length){if (i + j < message.Length){sb.Append(message[i + j]);}else{sb.Append(" ");}}}}return sb.ToString().TrimEnd();}public static string Encode(string message, int cylinderSides){//Num Cols = message length / cylinder sides rounded updecimal cl =(decimal) message.Length /(decimal) cylinderSides;int cols = (int) Math.Ceiling(cl);int rows = cylinderSides;char[,] _scytale = new char[cols, rows];// wrap messagefor (int r = 0; r < rows; r++){for (int c = 0; c < cols; c++){if ((cols * r) + c < message.Length){_scytale[c, r] = message[(cols * r) + c];}else{_scytale[c, r] = ' ';}}}StringBuilder sb = new StringBuilder();for (int c = 0; c < cols; c++){for (int r = 0; r < rows; r++){sb.Append(_scytale[c, r]);}}return sb.ToString().TrimEnd();}}
}​

答案5:

namespace Scytale {public class Scytale {public static string Decode(string message, int cylinderSides) {return EncodeOrDecode(message, cylinderSides, false);}public static string Encode(string message, int cylinderSides) {return EncodeOrDecode(message, cylinderSides, true);}private static string EncodeOrDecode(string message, int cylinderSides, bool encode) {var length = ((message.Length - 1) / cylinderSides) + 1;message = message.PadRight(length * cylinderSides);var shift = encode ? length : cylinderSides;var output = "";for (var i = 0; i < shift; i++) {for (var j = i; j < message.Length; j += shift) {output += message[j];}}return output.Trim();}}
}​

答案6:

using System;
using System.Collections.Generic;
using System.Linq;namespace Scytale
{public static class Scytale{public static string Decode(string message, int cylinderSides){//! Example: Message has 21 letters for a 6 cylinder scytale.//! Since 21 % 6 = 1 != 0, the message will need ceil(21 / 6) = 4 strips.//! Only 21 % 6 = 1 of those strips will be filled with 6 letters, the rest will have 5.// Calculate the number of strips.// If remainderStrips == 0 all strips are full (i.e. the message divided fully into the cylinder).// Otherwise, we need to add 1 since integer division does floor, and we need ceil.var noOfStrips = Math.DivRem(message.Length, cylinderSides, out var remainderStrips);if (remainderStrips > 0) noOfStrips++;// Create jagged arrays for the rows.var rows = Enumerable.Repeat(0, cylinderSides).Select(_ => new List<char>()).ToList();// Iterate throught the message and assign each letter to the appropriate row.for (int i = 0; i < message.Length; i++)rows[i % cylinderSides].Add(message[i]);// Join rows to create the message. Trim the ends.return string.Concat(rows.SelectMany(r => r)).TrimEnd();}public static string Encode(string message, int cylinderSides){// See Decode for explanation of counting strips.var noOfStrips = Math.DivRem(message.Length, cylinderSides, out var remainderStrips);if (remainderStrips > 0){noOfStrips++;// We also want to pad with spaces here so that it wraps correctly at the bottom row.message = message.PadRight(noOfStrips * cylinderSides);}// Create fixed arrays for the strips.var strips = Enumerable.Repeat(0, noOfStrips).Select(_ => new char[cylinderSides]).ToList();// Iterate throught the message and assign each letter to the appropriate location of each strip.for (int i = 0; i < message.Length; i++){var rowInd = Math.DivRem(i, noOfStrips, out var stripInd);strips[stripInd][rowInd] = message[i];}// Join rows to create the message.  DON'T Trim the ends since spaces are important in the final strip.return string.Concat(strips.Select(r => new string(r)));}}
}​

答案7:

using System;
using System.Collections.Generic;
using System.Linq;namespace Scytale
{public class Scytale{private static void PrintRows(List<List<char>> rows){if (rows.Count == 0 || rows[0].Count == 0) return;var first = true;var noOfStrips = rows[0].Count;var bar = string.Concat(Enumerable.Repeat('-', 2 * noOfStrips - 1));foreach (var row in rows){if (first)first = false;elseConsole.WriteLine(bar);var rowToPrint = row.ToList();while (rowToPrint.Count < noOfStrips)rowToPrint.Add(' ');Console.WriteLine(string.Join("|", rowToPrint));}}public static string Decode(string message, int cylinderSides){//! Example: Message has 21 letters for a 6 cylinder scytale.//! Since 21 % 6 = 1 != 0, the message will need ceil(21 / 6) = 4 strips.//! Only 21 % 6 = 1 of those strips will be filled with 6 letters, the rest will have 5.// Calculate the number of strips.// If remainderStrips == 0 all strips are full (i.e. the message divided fully into the cylinder).// Otherwise, we need to add 1 since integer division does floor, and we need ceil.var noOfStrips = Math.DivRem(message.Length, cylinderSides, out var remainderStrips);if (remainderStrips > 0) noOfStrips++;// Create jagged arrays for the rows.var rows = Enumerable.Repeat(0, cylinderSides).Select(_ => new List<char>()).ToList();// Iterate throught the message and assign each letter to the appropriate row.for (int i = 0; i < message.Length; i++)rows[i % cylinderSides].Add(message[i]);PrintRows(rows);Console.WriteLine($"D (before) [{message.Length}]: \"{message}\"");// Join rows to create the message. Trim the ends.message = string.Concat(rows.SelectMany(r => r)).TrimEnd();Console.WriteLine($"D (after)  [{message.Length}]: \"{message}\"");return message;}private static void PrintStrips(List<char[]> strips){if (strips.Count == 0 || strips[0].Length == 0) return;var noOfRows = strips[0].Length;var bar = string.Concat(Enumerable.Repeat('-', 2 * strips.Count - 1));for (int i = 0; i < noOfRows; i++){if (i > 0) Console.WriteLine(bar);var rowToPrint = strips.Select(s => i < s.Length ? s[i] : ' ');Console.WriteLine(string.Join("|", rowToPrint));}}public static string Encode(string message, int cylinderSides){// See Decode for explanation of counting strips.var noOfStrips = Math.DivRem(message.Length, cylinderSides, out var remainderStrips);if (remainderStrips > 0){noOfStrips++;// We also want to pad with spaces here so that it wraps correctly at the bottom row.message = message.PadRight(noOfStrips * cylinderSides);}// Create fixed arrays for the strips.var strips = Enumerable.Repeat(0, noOfStrips).Select(_ => new char[cylinderSides]).ToList();// Iterate throught the message and assign each letter to the appropriate location of each strip.for (int i = 0; i < message.Length; i++){var rowInd = Math.DivRem(i, noOfStrips, out var stripInd);strips[stripInd][rowInd] = message[i];}PrintStrips(strips);Console.WriteLine($"E (before) [{message.Length}]: \"{message}\"");// Join rows to create the message. DON'T Trim the ends since spaces are important in the final strip.message = string.Concat(strips.Select(r => new string(r)));Console.WriteLine($"E (after)  [{message.Length}]: \"{message}\"");return message;}}
}​

答案8:

using System;
using System.Linq;namespace Scytale
{public class Scytale{public static string Decode(string message, int cylinderSides){string output = "";int i = 0;while (i < cylinderSides){output += new String(message.Where((s, index) => index % cylinderSides == i).ToArray());i++;}return output.TrimEnd(' ');}public static string Encode(string message, int cylinderSides){string output = "";int newLen=message.Length;while (newLen % cylinderSides != 0)newLen++;string tmpStr = message.PadRight(newLen, ' ');int lineLength = tmpStr.Length / cylinderSides;int i = 0;while (i < lineLength){output += new String(tmpStr.Where((s, index) => index % lineLength == i).ToArray());i++;}return output;}}
}​

答案9:

using System;
using System.Linq;
using System.Collections.Generic;namespace Scytale
{public class Scytale{public static string Decode(string message, int cylinderSides){Console.WriteLine($"Decode message: {message} cylinderSides: {cylinderSides}");var list = new List<char>();var blocks = message.Length % cylinderSides == 0 ? message.Length / cylinderSides : message.Length / cylinderSides + 1;for (var offset = 0; offset < cylinderSides; offset++) {for (var index = 0; index < blocks; index++) {var realIndex = offset + index * cylinderSides;if (realIndex < message.Length) {list.Add(message[realIndex]);}}}return new string(list.ToArray()).TrimEnd();}public static string Encode(string message, int cylinderSides){Console.WriteLine($"Encode message: {message} cylinderSides: {cylinderSides}");var blocks = message.Length % cylinderSides == 0 ? message.Length / cylinderSides : message.Length / cylinderSides + 1;var list = new List<char>();for (var offset = 0; offset < blocks ; offset++) {for (var index = 0; index < cylinderSides; index++) {var realIndex = offset + index * blocks;list.Add(realIndex < message.Length ? message[realIndex] : ' ');}}return new string(list.ToArray());}}
}​

答案10:

namespace Scytale {using System;using System.Collections.Generic;using System.Text;public class Scytale {public static string Decode( string message, int cylinderSides ) {return EncodeDecode( message, cylinderSides, true );}public static string Encode( string message, int cylinderSides ) {return EncodeDecode( message, cylinderSides );}private static string EncodeDecode( string message, int cylinderSides, bool decode = false ) {var wordLength = (int) Math.Ceiling( message.Length/(double) cylinderSides );message += new string( ' ', wordLength*cylinderSides - message.Length );var words = new List<string>( );var j = 0;var n = cylinderSides;var m = wordLength;if ( decode ) {n = wordLength;m = cylinderSides;}for ( int i = 0; i < n; i++ ) {var s = message.Substring( j, m );j += m;words.Add( s );}var result = new StringBuilder( );for ( int i = 0; i < m; i++ ) {foreach ( var w in words ) {result.Append( w[ i ] );}}return result.ToString( ).Trim( );}}
}​

C#练习题答案: scytale的编码器/解码器(古斯巴达密码)【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战相关推荐

  1. C#练习题答案: 字母战争 - 核打击【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    字母战争 - 核打击[难度:3级]: 答案1: using System; using System.Text.RegularExpressions; using System.Linq; publi ...

  2. C#练习题答案: 寻找恩人【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    寻找恩人[难度:1级]: 答案1: using System; using System.Linq;public class NewAverage {public static long NewAvg ...

  3. C#练习题答案: 反恐精英系列【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    反恐精英系列[难度:1级]: 答案1: namespace CS {using System;using System.Collections.Generic;public class Kata{pr ...

  4. C#练习题答案: 图片#1 - 重建巴别塔【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    图片#1 - 重建巴别塔[难度:1级]: 答案1: using System.Linq;public static class Kata {public static string Babel(int ...

  5. C#练习题答案: TO DE-RY-PO-陆琪暗号【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    TO DE-RY-PO-陆琪暗号[难度:1级]: 答案1: using System.Linq;public class Kata{public static string Encode(string ...

  6. C#练习题答案: 英雄的根【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    英雄的根[难度:1级]: 答案1: using System;public class IntSqRoot {const int error = 1;public static long IntRac ...

  7. C#练习题答案: 产品和LCMS之间的差异总和【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    产品和LCMS之间的差异总和[难度:1级]: 答案1: using System.Linq;public class Kata {static int gcd(int a, int b) {if(a ...

  8. C#练习题答案: 巴路士惠勒改造【难度:4级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    巴路士惠勒改造[难度:4级]: 答案1: using System; using System.Collections.Generic; using System.Linq;public class ...

  9. C#练习题答案: 折叠用自己的方式去月球【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    折叠用自己的方式去月球[难度:1级]: 答案1: using System; using System.Collections.Generic;public class Kata {public st ...

最新文章

  1. Koa2和Redux中间件源码研究
  2. FFT ---- 2021牛客多校第一场 H Hash Function
  3. “还完花呗,再也不用吃土!”是真的吗?(附代码)
  4. seL4 microkernel学习资料
  5. 启明云端分享|ESP32/ESP8266 烧录器 USB-TTL转接板开发工具ESP-T01的使用教程,视频可参考B站
  6. php任意文件删除漏洞,phpshe后台任意文件删除漏洞及getshell | CN-SEC 中文网
  7. 气温常年在25度的地方_最低调的海滨城市,物价便宜,常年25度,沙滩细白,不输三亚!...
  8. maven项目的目录结构
  9. 响应式网站设计_通过这个免费的四小时课程,掌握响应式网站设计
  10. Day11-递归性能测试
  11. delphi 调用Msftedit.dll,重写Richedit,支持RTF画表格
  12. Java-20180419
  13. ssm整合之配置applicationContext-service.xml
  14. HCIE Security AC访客管理和终端安全 备考笔记(幕布)
  15. 成员变量和局部变量详解
  16. 打开word2016文档时提示用文本恢复转换器打开文件
  17. MySQL面试问题包含答案仅参考
  18. cadence 通孔焊盘_Cadence学习3(通孔类焊盘的建立)(转)
  19. WechatPay-API-v3接口规则
  20. Vue promise的用法

热门文章

  1. Angular随记:Angular CLI安装及使用
  2. IBM超级计算机揭秘最古老英语单词
  3. u盘里的文件不见了怎么恢复正常?数据还有救吗?
  4. 静态综合实验(企业内网访问外网工程)
  5. U盘中的文件夹全变成应用程序格式如何解决?
  6. UVaOJ 11205 - The broken pedometer
  7. 易语言制作计算软件简单步骤_视频解说不想自己录,用什么简单好用的配音软件制作?...
  8. 【求助】救救“这个可怜的孩子”
  9. 法拉第PK特斯拉,美产与国产谁能取胜?
  10. 高等教育计算机等级,[高等教育]全国高校计算机等级考试5.doc