1.关于本博客的说明
Code Hunt 是我从CSDN上的一篇文章中无意间看到的:微软研究院正式发布编程学习游戏Code Hunt,游戏地址从这里进入。
本篇博客是游戏的 NESTED LOOPS 和 1D Arrays 部分的C#部分解题代码
2.SECTOR8:NESTED LOOPS
1)SECTOR8-01
求两个数字(含)间所有数字的阶乘和
SKILL RATING:3
using System; public class Program { public static int Puzzle(int i, int j) { int result=0; for(int x=i;x<=j;x++) {result+=fac(x);} return result; } public static int fac(int x) { if(x==0)return 1; if(x==1)return 1; if(x==2)return 2; return fac(x-1)*x; } }
SKILL RATING:?
这两个方法没有通过CodeHunt的编译
using System; public class Program { public static int Puzzle(int i, int j) { double[] fac = new double[j + 1]; fac[0] = 1; for (int x = 1; x < j + 1; x++) { fac[x] = fac[x - 1] * x; } double result = 0; for (int y = i; y <= j; y++) { result += fac[y]; } return (int)result; } }
using System; public class Program { public static int Puzzle(int i, int j) { int fac = 1; int result = 0; for (int x = 1; x <= j; x++) { fac *= x; if (x >= i) result += fac; } return result; } }
2)SECTOR8-02
输出字符串:"# ## ### #### ..."
SKILL RATING:2
using System; public class Program { public static string Puzzle(int n) { string output = ""; for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { output += "#"; } output += " "; } return output; } }
SKILL RATING:2
using System; public class Program { public static string Puzzle(int n) { char[] ch = new char[n + n * (n + 1) / 2]; for (int i = 0; i < ch.Length; i++) { ch[i] = '#'; } int c1 = -1; int c2 = 2; for (int j = 0; j < n; j++) { c1 = c1 + c2; c2++; ch[c1] = ' '; } return new string(ch); } }
3)SECTOR8-03
输出符合规律的字符串,如n=5时为:
"0_____ 01____ 012___ 0123__ 01234_ 012345"
SKILL RATING:3
using System; public class Program { public static string Puzzle(int n) { string result = ""; for (int i = 0; i < n + 1; i++) { char[] ch = (new string('_', n + 1)).ToCharArray(); for (int j = 0; j <= i; j++) { ch[j] = (char)((int)'0' + j); } result += (new string(ch) + " "); } return result; } }
4)SECTOR8-04
输出字符串:共n个字符b[0]的字符串重复输出,每次输出时用字符'-'依次向后替换一个字
如n为3,b[0]为'&'时:"-&& &-& &&-"
SKILL RATING:3
using System; public class Program { public static string Puzzle(int n, string b) { string result = ""; for (int i = 0; i < n; i++) { char[] ch = (new string(b[0], n)).ToCharArray(); ch[i] = '-'; result += (new string(ch)); if (i != n - 1) { result += " "; } } return result; } }
5)SECTOR8-05
给定字符串如"aaaa"重复输出,每次输出时用字符'_'依次向后替换一个字
结果如:"_aaa a_aa aa_a aaa_"
SKILL RATING:3
using System; public class Program { public static string Puzzle(string a) { string result = ""; for (int i = 0; i < a.Length; i++) { char[] ch = a.ToCharArray(); ch[i] = '_'; result += (new string(ch)); if (i != a.Length - 1) { result += " "; } } return result; } }
6)SECTOR8-06
假设一个n*n的盒子,边用'$'表示,里面用'_'表示,如:
"$$$$$$" "$____$" "$____$" "$____$" "$____$" "$$$$$$"
将各行放到同一行,中间加上空格,输出如:
"$$$$$$ $____$ $____$ $____$ $____$ $$$$$$"
SKILL RATING:2
using System; public class Program { public static string Puzzle(int size) { string result = ""; for (int i = 0; i < size; i++) { char[] ch; if (i == 0 || i == size - 1) { ch = (new string('$', size)).ToCharArray(); } else { ch = (new string('_', size)).ToCharArray(); ch[0] = '$'; ch[size - 1] = '$'; } result += (new string(ch)); if (i != size - 1) { result += " "; } } return result; } }
SKILL RATING:2
using System; public class Program { public static string Puzzle(int size) { char[] ch = (new string(' ', size * size + size - 1)).ToCharArray(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i != 0 && i != size - 1 && j != 0 && j != size - 1) { ch[i * size + i + j] = '_'; } else { ch[i * size + i + j] = '$'; } } } return new string(ch); } }
7)SECTOR8-07
SKILL RATING:3
using System; public class Program { public static string Puzzle(int height, int width) { char[] ch = (new string(' ', height * width + height - 1)).ToCharArray(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if ((i + j) % 2 == 0) { ch[i * width + i + j] = 'x'; } else { ch[i * width + i + j] = 'o'; } } } return new string(ch); } }
8)SECTOR8-08
输出字符串形如:1个1 2个2 3个3 ... 6个6 ... 3个3 2个2 1个1
"1 22 333 4444 55555 666666 55555 4444 333 22 1"
SKILL RATING:3
using System; public class Program { public static string Puzzle(int number) { string result = new string((char)((int)'0' + number), number); while (--number > 0) { result = result + " " + new string((char)((int)'0' + number), number); result = new string((char)((int)'0' + number), number) + " " + result; } return result; } }
3.SECTOR9:1D ARRAYS
1)SECTOR9-01
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[] list, int i) { return list[i]; } }
2)SECTOR9-02
SKILL RATING:3
using System; public class Program { public static string Puzzle(string[] list) { return list[0] + list[list.Length - 1]; } }
3)SECTOR9-03
SKILL RATING:1
using System; public class Program { public static bool Puzzle(int[] list, int i) { for (int x = 0; x < list.Length; x++) { if (list[x] == i) return true; } return false; } }
SKILL RATING:?
下面这个方法在VS2012编译能通过,但在CodeHunt中不被承认
using System; public class Program { public static bool Puzzle(int[] list, int i) { return list.Contains(i); } }
4)SECTOR9-04
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int x) { int[] array = new int[10]; for (int i = 0; i < array.Length; i++) { array[i] = x; } return array; } }
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int x) { return new int[] { x, x, x, x, x, x, x, x, x, x }; } }
5)SECTOR9-05
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = i; } return array; } }
6)SECTOR9-06
SKILL RATING:1
using System; public class Program { public static bool[] Puzzle() { bool[] b = new bool[10]; for (int i = 0; i < 10; i++) { b[i] = true; } return b; } }
SKILL RATING:3
using System; public class Program { public static bool[] Puzzle() { return new bool[] { true, true, true, true, true, true, true, true, true, true }; } }
7)SECTOR9-07
SKILL RATING:3
using System; public class Program { public static string[] Puzzle(string s) { string[] sarr = new string[s.Length]; for (int i = 0; i < s.Length; i++) { sarr[i] = s[i].ToString(); } return sarr; } }
8)SECTOR9-08
SKILL RATING:2
using System; public class Program { public static int[] Puzzle(int i) { int[] array = new int[i + 1]; for (int x = 0; x <= i; x++) { array[x] = i - x; } return array; } }
9)SECTOR9-09
SKILL RATING:3
using System; public class Program { public static int[] Puzzle(int[] numbers) { for (int i = 0; i < numbers.Length; i++) { numbers[i] = -numbers[i]; } return numbers; } }
10)SECTOR9-10
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int[] numbers) { int temp; for (int i = 0; i < numbers.Length / 2; i++) { temp = numbers[i]; numbers[i] = numbers[numbers.Length - i - 1]; numbers[numbers.Length - i - 1] = temp; } return numbers; } }
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int[] numbers) { int[] array = new int[numbers.Length]; for (int i = 0; i < numbers.Length; i++) { array[i] = number[numbers.Length - i - 1]; } return array; } }
SKILL RATING:3
using System; public class Program { public static int[] Puzzle(int[] numbers) { Array.Reverse(numbers); return numbers; } }
11)SECTOR9-11
SKILL RATING:3
using System; public class Program { public static string Puzzle(string s) { char[] ch = s.ToCharArray(); Array.Reverse(ch); return new string(ch); } }
12)SECTOR9-12
数组向后循环移位,位数由amount指定
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int[] numbers, int amount) { int[] array = new int[numbers.Length]; for (int i = 0; i < numbers.Length; i++) { int temp = i + amount; if (temp > numbers.Length - 1) temp -= numbers.Length; array[temp] = numbers[i]; } return array; } }
13)SECTOR9-13
SKILL RATING:3
using System; public class Program { public static int[] Puzzle(int[] numbers, int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; return numbers; } }
14)SECTOR9-14
SKILL RATING:2
using System; public class Program { public static int[] Puzzle(int[] a, int[] b) { int[] array = new int[a.Length > b.Length ? a.Length : b.Length]; for (int i = 0; i < array.Length; i++) { if (i < a.Length && i < b.Length) { array[i] = a[i] + b[i]; } else if (i >= a.Length) { array[i] = b[i]; } else { array[i] = a[i]; } } return array; } }