1.关于本博客的说明
Code Hunt 是我从CSDN上的一篇文章中无意间看到的:微软研究院正式发布编程学习游戏Code Hunt,游戏地址从这里进入。
本篇博客是游戏的 SEARCH SORT、CYPHERS 和 PUZZLES 部分的C#部分解题代码
本片博客也是这个系列的最后一篇博客
2.SECTOR12:SEARCH SORT
1)SECTOR12-01
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[] numbers, int x) { int counter=0; for(int i=0;i<numbers.Length;i++) { if(numbers[i]==x) { counter++; } } return counter; } }
2)SECTOR12-02
SKILL RATING:3
using System; public class Program { public static int Puzzle(string[] words, string s) { int counter = 0; for (int i = 0; i < words.Length; i++) { if (words[i] == s) { counter++; } } return counter; } }
3)SECTOR12-03
SKILL RATING:1
using System; public class Program { public static int Puzzle(int[] numbers, int x) { for (int i = 0; i < numbers.Length; i++) { if (numbers[i] == x) { return i; } } return -1; } }
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[] numbers, int x) { return Array.IndexOf(numbers, x); } }
4)SECTOR12-04
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[] numbers, int x) { return Array.LastIndexOf(numbers, x); } }
5)SECTOR12-05
返回数组numbers内与x相等的元素的序号
SKILL RATING:2
using System; public class Program { public static int[] Puzzle(int[] numbers, int x) { int counter = 0; foreach (int num in numbers) { if (num == x) { counter++; } } int[] result = new int[counter]; int i, j = 0; for (i = 0; i < numbers.Length; i++) { if (numbers[i] == x) { result[j++] = i; } } return result; } }
6)SECTOR12-06
将数组内等于x的元素替换为y
SKILL RATING:3
using System; public class Program { public static int[] Puzzle(int[] numbers, int x, int y) { for (int i = 0; i < numbers.Length; i++) { if (numbers[i] == x) { numbers[i] = y; } } return numbers; } }
7)SECTOR12-07
从数组中找出所有的元素对,使其下标ij满足numbers[i]+numbers[j]的值与x相等
SKILL RATING:2
using System; public class Program { public static int[][] Puzzle(int[] numbers, int x) { int counter = 0; for (int i = 0; i < numbers.Length; i++) { for (int j = i + 1; j < numbers.Length; j++) { if (numbers[i] + numbers[j] == x) { counter++; } } } int[][] result = new int[counter][]; int k = 0; for (int i = 0; i < numbers.Length; i++) { for (int j = i + 1; j < numbers.Length; j++) { if (numbers[i] + numbers[j] == x) { result[k++] = new int[] { i, j }; } } } return result; } }
8)SECTOR12-08
(这题想了好半天才摸清楚规律)同时从左向右、从有向左统计,找到使左右元素和相等的点
SKILL RATING:2
using System; public class Program { public static int Puzzle(int[] numbers) { int sum_left = 0; int sum_right = 0; for (int i = 0; i <= numbers.Length - 2; i++) { sum_left = 0; for (int j = 0; j <= i; j++) { sum_left += numbers[j]; } sum_right = 0; for (int k = i + 1; k < numbers.Length; k++) { sum_right += numbers[k]; } if (sum_left == sum_right) { return i; } } int sum = 0; for (int i = 0; i < numbers.Length; i++) { sum += numbers[i]; } if (sum == 0) { return numbers.Length - 1; } return -1; } }
这个方法没有通过系统测试
using System; public class Program { public static int Puzzle(int[] numbers) { int signer1 = 0; int signer2 = numbers.Length - 1; int sum1 = 0; int sum2 = 0; while (signer1 <= signer2) { if (sum1 >= sum2) { sum2 += numbers[signer2--]; } else { sum1 += numbers[signer1++]; } } if (sum1 == sum2) { return signer1 - 1; } else { return -1; ; } } }
9)SECTOR12-09
判断一个数组是否升序排列
SKILL RATING:1
using System; public class Program { public static bool Puzzle(int[] numbers) { for (int i = 0; i < numbers.Length - 1; i++) { if (numbers[i] > numbers[i + 1]) return false; } return true; } }
10)SECTOR12-10
SKILL RATING:1
using System; public class Program { public static bool Puzzle(string[] words) { for (int i = 0; i < words.Length - 1; i++) { if (string.Compare(words[i], words[i + 1]) > 0) { return false; } } return true; } }
11)SECTOR12-11
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int[] a) { int temp; for (int i = 0; i < a.Length; i++) { for (int j = i; j < a.Length; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return a; } }
SKILL RATING:3
using System; public class Program { public static int[] Puzzle(int[] a) { Array.Sort(a); return a; } }
12)SECTOR12-12
SKILL RATING:3
using System; public class Program { public static string[] Puzzle(string[] a) { Array.Sort(a); return a; } }
3.SECTOR13:CYPHERS
1)SECTOR13-01
SKILL RATING:3
using System; public class Program { public static string Puzzle(string s) { char[] c = new char[s.Length]; for (int i = 0; i < c.Length; i++) { c[i] = (int)s[i] + 7 > (int)'z' ? (char)((int)s[i] - 19) : (char)((int)s[i] + 7); } return new string(c); } }
2)SECTOR13-02
SKILL RATING:2
using System; public class Program { public static string Puzzle(string s) { char[] c = s.ToCharArray(); for (int i = 0; i < c.Length; i++) { switch (c[i]) { case '|': c[i] = '0'; break; case '}': c[i] = '1'; break; case '~': c[i] = '2'; break; default: { c[i] = (char)((int)s[i] + 3); } break; } } return new string(c); } }
SKILL RATING:3
using System; public class Program { public static string Puzzle(string s) { char[] c = s.ToCharArray(); for (int i = 0; i < c.Length; i++) { int temp = (int)s[i] + 3; if (temp >= 127) temp -= 79; c[i] = (char)temp; } return new string(c); } }
3)SECTOR13-03
给出一个字符串生成一个新字符串
s_new[0]=s_old[0]+1
s_new[1]=s_old[1]+1+4
...
s_new[i]=s_old[i]+1+4*i
如果字符大小超过'z'则重新从'a'开始计
SKILL RATING:3
using System; public class Program { public static string Puzzle(string s) { char[] ch = s.ToCharArray(); for (int i = 0; i < s.Length; i++) { int temp = (int)ch[i] + i * 4 + 1; while (temp > 'z') { temp -= 26; } ch[i] = (char)temp; } return new string(ch); } }
4)SECTOR13-04
设有字符串s,i为其中各个字符的下标
i%4==0:该字符后移2位
i%4==1:该字符后移3位
i%4==2:该字符后移19位
i%4==3:该字符后移19位
字符后移超过'z'则从'a'重新计算
SKILL RATING:1
using System; public class Program { public static string Puzzle(string s) { char[] c = new char[s.Length]; for (int i = 0; i < c.Length; i++) { switch (i % 4) { case 0: { c[i] = (char)((int)s[i] + 2 > 'z' ? (int)s[i] - 24 : (int)s[i] + 2); } break; case 1: { c[i] = (char)((int)s[i] + 3 > 'z' ? (int)s[i] - 23 : (int)s[i] + 3); } break; case 2: { c[i] = (char)((int)s[i] + 19 > 'z' ? (int)s[i] - 7 : (int)s[i] + 19); } break; case 3: { c[i] = (char)((int)s[i] + 19 > 'z' ? (int)s[i] - 7 : (int)s[i] + 19); } break; } } return new string(c); } }
4.SECTOR14:PUZZLES
1)SECTOR12-01
(It's play time with Baby Gauss!)
给出x计算1+2+3+...+x的值
SKILL RATING:3
using System; public class Program { public static int Puzzle(int x) { return x * (x + 1) / 2; } }
2)SECTOR12-02
(It's magic (square)!)
幻方的各行数字和、各列数字和、对角线数字和相同
现给出幻方的阶x,求它的行数字和为多少
SKILL RATING:3
using System; public class Program { public static int Puzzle(int x) { return x * (x * x + 1) / 2; } }
3)SECTOR12-03
返回true值时应该满足的条件:1.f可以整除x;2.f为质数
SKILL RATING:2
using System; public class Program { public static bool Puzzle(int x, int f) { if (f == 1 || x % f != 0) return false; for (int i = 2; i <= Math.Sqrt(f); i++) { if (f % i == 0) return false; } return true; } }
4)SECTOR12-04
给一个字符串,求新字符串
新字符串每一位是老字符串每两位的字母均值
SKILL RATING:1
using System; public class Program { public static string Puzzle(string s) { char[] ch = new char[(s.Length + 1) / 2]; for (int i = 0; i < ch.Length - 1; i++) { ch[i] = (char)((((int)s[i * 2] - 'a') + ((int)s[i * 2 + 1] - 'a')) / 2 + 'a'); } if (s.Length % 2 == 0) { ch[ch.Length - 1] = (char)((((int)s[s.Length - 2] - 'a') + ((int)s[s.Length - 1] - 'a')) / 2 + 'a'); } else { ch[ch.Length - 1] = s[s.Length - 1]; } return new string(ch); } }
5)SECTOR12-05
找出数组中互异的元素数
SKILL RATING:1
using System; public class Program { public static int Puzzle(int[] list) { Array.Sort(list); int x = list[0]; int diffcount = 1; for (int i = 1; i < list.Length; i++) { if (x != list[i]) { x = list[i]; diffcount++; } } return diffcount; } }
6)SECTOR12-06
将数组中元素按先后顺序不重复存到数组中
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int[] list) { //找到list中有多少个不同的元素 int[] temp = new int[list.Length]; for (int i = 0; i < list.Length; i++) { temp[i] = list[i]; } Array.Sort(temp); int x = temp[0]; int diffcount = 1; for (int i = 1; i < temp.Length; i++) { if (x != temp[i]) { x = temp[i]; diffcount++; } } //将不同的元素按list中出现的先后顺序输出 int[] result = new int[diffcount]; x = list[0]; result[0] = list[0]; diffcount = 1; for (int i = 1; i < list.Length; i++) { bool bTrue = false; for (int j = 0; j < diffcount; j++) { if (result[j] == list[i]) { bTrue = true; break; } } if (!bTrue) { result[diffcount++] = list[i]; } } return result; } }
7)SECTOR12-07
判断数组是否左右对称
SKILL RATING:1
using System; public class Program { public static bool Puzzle(string s) { for (int i = 0; i < s.Length / 2; i++) { if (s[i] != s[s.Length - 1 - i]) { return false; } } return true; } }
8)SECTOR12-08
SKILL RATING:2
using System; public class Program { public static bool Puzzle(string a, string b) { char[] cha = a.ToCharArray(); char[] chb = b.ToCharArray(); Array.Sort(cha); Array.Sort(chb); return new string(cha) == new string(chb) ? true : false; } }
9)SECTOR12-09
求两个直线的交点
SKILL RATING:1
using System; public class Program { public static string Puzzle(int slope1, int yintercept1, int slope2, int yintercept2) { //slope:斜率 //intercept:截距 if (slope1 == slope2 && yintercept1 == yintercept2) { return "same line"; } if (slope1 == slope2) { return "parallel lines"; } double x = (yintercept2 - yintercept1) * 1.0 / (slope1 - slope2); double y = 1.0 * slope1 * x + yintercept1; return string.Format("({0}, {1})", x, y); } }
5.纪念
题目全做完了,虽然很多题目仍然有进步的空间,还是上传个图片纪念一下吧!