腾讯2018暑期实习生招聘在线笔试之字符串操作
某互联网公司2017年暑期实习生招聘软件开发在线笔试时间是2017年4月3日19:00至21:00(北京时间),题目主要是30道不定向选择题(1小时),2道简答题,2道在线编程题(1小时),题目涉及范围很广,包括C++、网络、算法等基础知识。其中,在线编程题语言有C、C++、C#、Java、Python、PHP、JavaScript可以选择!
*******************************************************************
在线编程题:
编写代码,接收从屏幕输入的长度为16字节的整数倍的字符串,回车后,按示例格式排版输出。
屏幕输入(均为可见字符):
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl
屏幕输出(斜体部分是格式说明,不要在结果里输出)
![](https://images2015.cnblogs.com/blog/932612/201704/932612-20170408231044082-84410686.png)
*********************************************************************
Python实现代码(使用python 2.7.10解释运行):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #python代码示例处理正常,但是可能存在缺少特殊情况的考虑,欢迎在下面批评指出 inputstr = raw_input () lineCount = len (inputstr) / / 16 #输入行数 lineIndex = 0 # copyright @ Cnblogs DHUtoBUAA while lineIndex<lineCount: outputStr = "" linestr = hex (lineIndex * 16 + 16 ) outputStr + = linestr[ 2 :].zfill( 8 ) #zfill方法用来给字符串前面补0 outputStr + = " " #字符串截取:当使用以冒号分隔的字符串,python返回一个新的对象,结果包含了以这对偏移标识的连续的内容 #左边的开始是包含了下边界,而取到的最大范围不包括上边界 for x in inputstr[lineIndex * 16 :lineIndex * 16 + 8 ]: outputStr + = hex ( ord (x))[ 2 :] + " " outputStr + = " " for x in inputstr[lineIndex * 16 + 8 :lineIndex * 16 + 16 ]: outputStr + = hex ( ord (x))[ 2 :] + " " outputStr + = " " outputStr + = inputstr[lineIndex * 16 :lineIndex * 16 + 16 ] print (outputStr) lineIndex + = 1 |
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
C#实现代码(使用.NET 4编译运行):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main( string [] args) { string InputStr = Console.ReadLine(); //屏幕输入 int lineCount = InputStr.Length / 16;<br> //copyright @ cnblogs DHUtoBUAA for ( int lineIndex=0;lineIndex<lineCount;lineIndex++) { string output= "" ; int line = (lineIndex + 1) * 16; output+=Convert.ToString(line, 16).PadLeft(8, '0' ); output += " " ; for ( int charIndex=0;charIndex<16;charIndex++) { char temp = InputStr[charIndex + 16 * lineIndex]; // Get the integral value of the character. int value = Convert.ToInt32(temp); // Convert the decimal value to a hexadecimal value in string form. string hexOutput = String.Format( "{0:x}" , value); output += hexOutput; output += " " ; if (charIndex==7) output += " " ; } //截取 Substring(Int32, Int32):子字符串从指定的字符位置开始且具有指定的长度, // 提取字符串中的第i个字符开始的长度为j的字符串 output += " " ; output += InputStr.Substring(lineIndex * 16,16); Console.WriteLine(output); } Console.ReadKey(); } } } |
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
代码实现截图:
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
//参考文章
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步