腾讯移动端开发笔试编程题
第一题:和谐的数字 100%
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string s; s = Console.ReadLine(); int n = Convert.ToInt32(s); while (n-- > 0) { s = Console.ReadLine(); int i = Convert.ToInt32(s); Console.WriteLine(Judge(i) ? "Yes" : "No"); } Console.ReadKey(); } private static bool Judge(int i) { int n = i; int sum = 0; while(i != 0) { sum += i % 10; i /= 10; } return (n % sum == 0); } } }
第二题:巧克力球 100%
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string s; s = Console.ReadLine(); int n = Convert.ToInt32(s); //就是求连续0个数的乘积,首先要保证头尾都是1 //先去空格 s = Console.ReadLine(); s = s.Replace(" ", ""); //去掉头尾的0 s = s.Trim('0'); int sum = 1; //最多s.Length - 2个0 int max = s.Length - 2; for(int i = 1; i < max; i++) { Regex regex = new Regex("(?=[1][0]{" + i + "}[1])"); int t = regex.Matches(s).Count; sum *= (int)Math.Pow(i + 1, t); } Console.WriteLine(sum); Console.ReadKey(); } } }
第三题:能量矿石 20% 求大佬解答
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string s; s = Console.ReadLine(); string p; p = Console.ReadLine(); List<string> collection = ps(p); string pattern = "(?:(" + collection[0]; for(int i = 1; i < collection.Count; i++) { pattern += "|" + collection[i]; } pattern += ")+)"; //Console.WriteLine(pattern); Regex regex = new Regex(pattern); MatchCollection matchCollection = regex.Matches(s); int max = 0; foreach (Match match in matchCollection) { int len = match.ToString().Length; if (len > max) { max = len; } } Console.WriteLine(max * max); Console.ReadKey(); } //去除回文和重复 private static List<string> ps(string p) { List<string> all = new List<string>(); int len = p.Length; while (len > 0) { int movement = p.Length - len + 1; if (p.Substring(len - 1).Equals(p.Substring(0, movement))) all.Add(p + p.Substring(movement)); len--; } return all; } } }