hihocoder微软在线测试帮助
2T 3T的题干
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; ///在线测试 ///A+B problem namespace Ex01 { class Program { static void Main(string[] args) { string line; while ((line = Console.ReadLine()) != null) { string[] take = line.Split(' '); Console.WriteLine(int.Parse(take[0]) + int.Parse(take[1])); } } } }
///微软2014实习生在线测试题目
///http://blog.csdn.net/huolei_blog/article/details/23554891
///T1:字符串排序
///http://blog.csdn.net/huolei_blog/article/details/23555167
添加using System.Collections;
static void Main1()//Main方法没参数也行。。。 { string line; while ((line = Console.ReadLine()) != null) { bool isLegal = true; char[] input = line.ToArray(); ArrayList AL_input = new ArrayList(); for (int i = 0;isLegal && i < input.Length; i++) { if(input[i]>'z' || input[i]<'0' || (input[i]>'9' && input[i]<'a')) { isLegal = false; Console.Write("<invalid input string>"); break; } AL_input.Add(input[i]); } //默认按照ascii码排序,符合题意。 AL_input.Sort(); //此处用AL_input是否为null判断会导致死循环 while (isLegal && AL_input.Count != 0)//遇到不合法的输入不必做处理。 { ArrayList subSet = new ArrayList(); foreach (object i in AL_input) { if (!subSet.Contains(i)) { subSet.Add(i); //Console.WriteLine("subSet add {0}.", i); } } foreach (object i in subSet) { Console.Write(i); AL_input.Remove(i); } } Console.WriteLine(); } }
输出结果
/*输出结果 aabbccdd abcdabcd 007799aabbccddeeff113355zz 013579abcdefz013579abcdefz 1234.89898 .123489898 abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa */ /*定义bool isLegal = true;之后的输出结果 aabbccdd abcdabcd 007799aabbccddeeff113355zz 013579abcdefz013579abcdefz 1234.89898 <invalid input string> abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa */
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; ///T2:K-th string namespace Ex01 { /// <summary> /// 使用强大的ArrayList和StringBuilder /// </summary> class T2_K_string { static void Main(string[] args) { int testCase = int.Parse(Console.ReadLine()); while (testCase-- > 0) { int n, m, k; string line = Console.ReadLine(); string[] take = line.Split(' '); n = int.Parse(take[0]); m = int.Parse(take[1]); k = int.Parse(take[2]); //Console.WriteLine("{0} {1} {2}. ",n,m,k); ArrayList al = new ArrayList(); int number = GetCombination(n + m, n); //Console.WriteLine("number等于C (n+m) n"); while (al.Count < number) { string s = Creator(n, m); if (!al.Contains(s)) al.Add(s); } al.Sort(); //Console.WriteLine("排序之后的顺序"); //foreach (string s in al) //{ // Console.WriteLine(s); //} //Console.WriteLine("k={0} count={1}. 第k位的元素{2}", k, al.Count,al[k]); if (k > al.Count) Console.WriteLine("Impossible"); else Console.WriteLine(al[k-1]); } } /// <summary> /// 计算组合数 /// </summary> /// <param name="top">C的下标</param> /// <param name="bottom">C的上标</param> /// <returns>组合数的值</returns> private static int GetCombination(int top,int bottom) { int[] _top = new int[top]; int[] _bottom = new int[bottom]; for (int i = 0; i < top; i++) _top[i] = i+1; for (int i = 0; i < bottom; i++) _bottom[i] = i+1; //given the anwer = x/y int x, y; x=y=1; for (int i = 0; i < _bottom.Length; i++) { y *= _bottom[i]; x *= _top[_top.Length -1 - i]; } return x / y; } /// <summary> /// 获取一个0和1组成的随机字符串 /// 计算机计算速度快,用随机字符串取完所有组合。 /// </summary> /// <param name="n">字符串中0的个数</param> /// <param name="m">字符串中1的个数</param> /// <returns>随机字符串</returns> public static string Creator(int n, int m) { /**Each test case is 3 integers separated by blank space: * N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). * N stands for the number of ‘0’s, * M stands for the number of ‘1’s, * and K stands for the K-th of string in the set that needs to be printed as output. */ StringBuilder sb = new StringBuilder(); Random rand = new Random(); while (n > 0 && m > 0) { //以1/2的概率添加每一位 if (rand.Next(2) == 0) { sb.Append(0); n--; } else { sb.Append(1); m--; } } if (n > 0) { while (n-- >0) sb.Append(0); } else if (m > 0) { while (m-- > 0) sb.Append(1); } //we got the anwer return sb.ToString(); } } } /*输出结果{!!本算法导致数据大的时候算不出来} 3 2 2 2 0101 2 2 7 Impossible 4 7 47 */
/// 递归方法求字符串元素的全排列
http://www.dotblogs.com.tw/messboy000/archive/2014/02/15/144004.aspx
static void Main0(string[] args) { permute("", "123"); Console.ReadKey(); } static void permute(string result, string now) { if (now == "") { Console.WriteLine(result); } else { for (int i = 0; i < now.Length; i++) { //Console.WriteLine("i={0}, now.Substring(i + 1)={1}.", i, now.Substring(i + 1)); permute(result + now[i], now.Substring(0, i) + now.Substring(i + 1)); } } } /*递归过程主要使用这两个函数: * Substring(int startIndex):子字符串从指定的字符位置startIndex到字符串结束。 * Substring(int startIndex, int length):从startIndex开始长为length的子字符串。 * 如此就可以不断遍历,想象一棵递归树,那么其最左枝便是: * (null,123) * (1,23) * (12,3) * (123,null)这时输出。 */
输出结果
/*加上注释之后的输出结果。 i=0, now.Substring(i + 1)=23 i=0, now.Substring(i + 1)=3. i=0, now.Substring(i + 1)=. 123 i=1, now.Substring(i + 1)=. i=0, now.Substring(i + 1)=. 132 i=1, now.Substring(i + 1)=3. i=0, now.Substring(i + 1)=3. i=0, now.Substring(i + 1)=. 213 i=1, now.Substring(i + 1)=. i=0, now.Substring(i + 1)=. 231 i=2, now.Substring(i + 1)=. i=0, now.Substring(i + 1)=2. i=0, now.Substring(i + 1)=. 312 i=1, now.Substring(i + 1)=. i=0, now.Substring(i + 1)=. 321 */ /*将123替换为abcd的输出结果。 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba */
占位