1.关于本博客的说明
Code Hunt 是我从CSDN上的一篇文章中无意间看到的:微软研究院正式发布编程学习游戏Code Hunt,游戏地址从这里进入。
本篇博客是游戏的 JAGGED ARRAYS 和 Arrays 2 部分的C#部分解题代码
2.SECTOR10:JAGGED ARRAYS
1)SECTOR10-01
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[][] list, int i, int j) { return list[i][j]; } }
2)SECTOR10-02
SKILL RATING:1
建立一个大小为5*5的矩阵,每个元素赋初值为0
using System; public class Program { public static int[][] Puzzle() { return new int[][] { new int[5], new int[5], new int[5], new int[5], new int[5] }; } }
3)SECTOR10-03
建立一个大小为length*length的矩阵,每个元素赋初值为x
SKILL RATING:1
using System; public class Program { public static int[][] Puzzle(int length, int x) { int[] array = new int[length]; for (int i = 0; i < array.Length; i++) { array[i] = x; } int[][] result = new int[length][]; for (int i = 0; i < result.Length; i++) { result[i] = array; } return result; } }
4)SECTOR10-04
SKILL RATING:3
using System; public class Program { public static int[][] Puzzle(int[][] input) { int[][] result = new int[input.Length][]; for (int i = 0; i < result.Length; i++) { result[i] = new int[input[i].Length]; for (int j = 0; j < result[i].Length; j++) { result[i][j] = input[i][j] * 2; } } return result; } }
SKILL RATING:?
using System; public class Program { public static int[][] Puzzle(int[][] input) { for (int i = 0; i < input.Length; i++) { for (int j = 0; j < input[i].Length; j++) { input[i][j] *= 2; } } return input; } }
5)SECTOR10-05
找出数组input中等于i的元素个数
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[][] input, int i) { int counter = 0; for (int x = 0; x < input.Length; x++) { for (int y = 0; y < input[x].Length; y++) { if (input[x][y] == i) { counter++; } } } return counter; } }
6)SECTOR10-06
SKILL RATING:1
using System; public class Program { public static int Puzzle(int[][] input) { int max = int.MinValue; for (int x = 0; x < input.Length; x++) { for (int y = 0; y < input[x].Length; y++) { if (input[x][y] > max) { max = input[x][y]; } } } return max; } }
7)SECTOR10-07
找到锯齿型数组中和最大的一个元素数组
SKILL RATING:2
using System; public class Program { public static int[] Puzzle(int[][] input) { int maxsum = int.MinValue; int arrsum = 0; int isign = 0; for (int i = 0; i < input.Length; i++) { arrsum = 0; for (int j = 0; j < input[i].Length; j++) { arrsum += input[i][j]; } if (arrsum > maxsum) { maxsum = arrsum; isign = i; } } return input[isign]; } }
8)SECTOR10-08
找出二维数组各列中元素总和最大的列,并输出该列各元素和
SKILL RATING:2
using System; public class Program { public static int Puzzle(int[][] input) { int[] arrsum = new int[input[0].Length]; int sum = 0; for (int i = 0; i < input[0].Length; i++) { sum = 0; for (int j = 0; j < input.Length; j++) { sum += input[j][i]; } arrsum[i] = sum; } int max = int.MinValue; int sign = 0; for (int x = 0; x < arrsum.Length; x++) { if (arrsum[x] > max) { max = arrsum[x]; sign = x; } } return arrsum[sign]; } }
SKILL RATING:3
using System; public class Program { public static int Puzzle(int[][] input) { int max = int.MinValue; int sum = 0; for (int i = 0; i < input[0].Length; i++) { sum = 0; for (int j = 0; j < input.Length; j++) { sum += input[j][i]; } if (max < sum) { max = sum; } } return max; } }
2.SECTOR11:ARRAYS 2
1)SECTOR11-01
两个数组 a,b 生成一个新的数组 c
c[i]=a[i]-b[i] (i<a、b的长度,且a[i]>=b[i])
c[i]=b[i]-a[i] (i<a、b的长度,且a[i]<b[i])
c[i]=0-b[i] (i>=a的长度且i<b的长度)
c[i]=a[i]-0 (i>=b的长度且i<a的长度)
SKILL RATING:1
using System; public class Program { public static int[] Puzzle(int[] a, int[] b) { int[] c = new int[a.Length >= b.Length ? a.Length : b.Length]; for (int i = 0; i < c.Length; i++) { if (i < a.Length && i < b.Length) { c[i] = a[i] >= b[i] ? a[i] - b[i] : b[i] - a[i]; } else if (i >= a.Length && i < b.Length) { c[i] = 0 - b[i]; } else { c[i] = a[i] - 0; } } return c; } }
2)SECTOR11-02
将一个数组中每个元素复制一个放在自己后面,生成一个新数组
{"", "a"} → {"", "", "a", "a"}
SKILL RATING:2
using System; public class Program { public static string[] Puzzle(string[] a, int amount) { string[] s = new string[a.Length * amount]; for (int i = 0; i < a.Length; i++) { for (int j = 0; j < amount; j++) { s[amount * i + j] = a[i]; } } return s; } }
3)SECTOR11-03
一个二维方阵,判断两个对角线元素和是否相等
SKILL RATING:3
using System; public class Program { public static bool Puzzle(int[][] input) { int sum1 = 0, sum2 = 0; for (int i = 0; i < input.Length; i++) { sum1 += input[i][i]; sum2 += input[i][input.Length - 1 - i]; } return sum1 == sum2 ? true : false; } }
4)SECTOR11-04
SKILL RATING:3
using System; public class Program { public static int[] Puzzle(int[] list, int modBy) { for (int i = 0; i < list.Length; i++) { list[i] %= modBy; } return list; } }
5)SECTOR11-05
SKILL RATING:2
using System; public class Program { public static string[] Puzzle(string[] list) { string[] result = new string[(list.Length+1)/2]; for(int i=0;i<result.Length-1;i++) { result[i]=list[i*2]+list[i*2+1]; } result[result.Length-1] = (list.Length%2==0)? (list[list.Length-2]+list[list.Length-1]):(list[list.Length-1]); return result; } }
6)SECTOR11-06
SKILL RATING:3
using System; public class Program { public static string[] Puzzle(int[] grades, int A, int B, int C, int D) { string[] sgrades = new string[grades.Length]; for (int i = 0; i < grades.Length; i++) { sgrades[i] = ( grades[i] >= A ? "A" : ( grades[i] >= B ? "B" : ( grades[i] >= C ? "C" : ( grades[i] >= D ? "D" : "E")))); } return sgrades; } }
7)SECTOR11-07
SKILL RATING:3
using System; public class Program { public static string[][] Puzzle(string[][] input) { string temp; for (int i = 0; i < input.Length; i++) { for (int j = i; j < input[i].Length; j++) { temp = input[i][j]; input[i][j] = input[j][i]; input[j][i] = temp; } } return input; } }