1.关于本博客的说明
Code Hunt 是我从CSDN上的一篇文章中无意间看到的:微软研究院正式发布编程学习游戏Code Hunt,游戏地址从这里进入。
本篇博客是游戏的LOOPS部分的C#部分解题代码
2.SECTOR2:LOOPS
1)SECTOR2-01
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; } }
2)SECTOR2-02
SKILL RATING:3
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 * n; } return array; } }
3)SECTOR2-03
SKILL RATING:2
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 * i; } return array; } }
4)SECTOR2-04
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[] v) { int sum = 0; foreach (int i in v) { sum += i; } return sum; } }
5)SECTOR2-05
SKILL RATING:2
计算递增整数的平方和
using System; public class Program { public static int Puzzle(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i * i; } return sum; } }
SKILL RATING:3
利用平方和公式
using System; public class Program { public static int Puzzle(int n) { return n*(n-1)*(n*2-1)/6; } }
6)SECTOR2-06
SKILL RATING:1
public class Program { public static int Puzzle(string s) { int count = 0; foreach (char ch in s) { if (ch == 'a') { count++; } } return count; } }
7)SECTOR2-07
SKILL RATING:1
public class Program { public static int Puzzle(string s, char x) { int count = 0; foreach (char c in s) { if (c == x) count++; } return count; } }
3.SECTOR3:LOOPS2
1)SECTOR3-01
SKILL RATING:2
计算整数的整数次幂
public class Program { public static int Puzzle(int number, int power) { int result = 1; while (power != 0) { while (power % 2 == 0) { power /= 2; number *= number; } while (power % 3 == 0) { power /= 3; number *= (number * number); } result *= number; power--; } return result; } }
2)SECTOR3-02
SKILL RATING:3
计算阶乘
public class Program { public static int Puzzle(int i) { int result = 1; while (i > 1) { result *= i--; } return result; } }
3)SECTOR3-03
SKILL RATING:3
计算两个整数(含)之间所有整数的积
public class Program { public static int Puzzle(int lowerBound, int upperBound) { int result = upperBound; while (upperBound > lowerBound) { result *= --upperBound; } return result; } }
4)SECTOR3-04
SKILL RATING:2
public class Program { public static int Puzzle(int n) { return n <= 2 ? 0 : (n % 2 == 0 ? (n / 2) * (n / 2 - 1) : (n / 2) * (n / 2 + 1)); } }
5)SECTOR3-05
SKILL RATING:3
计算x[n]:规律为 x[1]=1;x[n]=n(n+1)/2.0+x[n-1]
public class Program { public static int Puzzle(int upperBound) { if (upperBound == 1) return 1; return upperBound * (upperBound + 1) / 2 + Puzzle(upperBound - 1); } }
6)SECTOR3-06
SKILL RATING:2
将一个字符串按字数输出为串"_ _ ... _ _",其中字符'_'的数量等同于原字符串的长度
using System; public class Program { public static string Puzzle(string word) { string s = "_"; for (int i = 1; i < word.Length; i++) { s += " _"; } return s; } }
SKILL RATING:2
using System; public class Program { public static string Puzzle(string word) { char[] ch = new char[word.Length * 2 - 1]; for (int i = 0; i < ch.Length; i++) { if (i % 2 == 0) ch[i] = '_'; else ch[i] = ' '; } return new string(ch); } }
7)SECTOR3-07
SKILL RATING:3
字符串中所有字母后移5位,超过'z'则从'a'重新计算
public class Program { public static string Puzzle(string s) { char[] c = new char[s.Length]; for (int i = 0; i < s.Length; i++) { c[i] = (char)(s[i] + 5 > 122 ? s[i] - 21 : s[i] + 5); } return new string(c); } }
8)SECTOR3-08
SKILL RATING:3
输出给定的整数的位数
using System; public class Program { public static int Puzzle(int x) { return x.ToString().Length; } }