1.关于本博客的说明
Code Hunt 是我从优快云上的一篇文章中无意间看到的:微软研究院正式发布编程学习游戏Code Hunt,游戏地址从这里进入。
本篇博客是游戏的STRINGS部分的C#部分解题代码
2.SECTOR6:STRINGS
1)SECTOR6-01
SKILL RATING:3
(这道题点进去就可以过关了。。。)
using System;
public class Program
{
public static bool Puzzle(string s)
{
return false;
}
}
2)SECTOR6-02
SKILL RATING:3
字符串隔一个字母进行小写转大写
using System;
public class Program
{
public static string Puzzle(string s)
{
char[] ch = s.ToCharArray();
for (int i = 0; i < ch.Length; i+=2)
{
ch[i] = (char)((int)ch[i] + 'A' - 'a');
}
return new string(ch);
}
}
3)SECTOR6-03
SKILL RATING:3
每个单词的最后一个字母小写转大写
public class Program
{
public static string Puzzle(string s)
{
char[] ch = s.ToCharArray();
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] >= 'a' && ch[i] <= 'z')
{
if (i == ch.Length - 1 || ch[i + 1] == ' ')
{
ch[i] = (char)((int)ch[i] + 'A' - 'a');
}
}
}
return new string(ch);
}
}
4)SECTOR6-04
SKILL RATING:3
using System;
public class Program
{
public static char Puzzle(string s, int x)
{
return s[x];
}
}
5)SECTOR6-05
SKILL RATING:3
public class Program
{
public static string Puzzle(string one, string two)
{
return two + one;
}
}
6)SECTOR6-06
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string s)
{
return s.Substring(s.Length / 2);
}
}
7)SECTOR6-07
SKILL RATING:1
using System;
public class Program
{
public static string Puzzle(string s)
{
char[] ch = s.ToCharArray();
for (int i = 0; i < s.Length / 2; i++)
{
ch[i] = (char)(ch[i + (s.Length + 1) / 2] + 'A' - 'a');
}
return ch.Length % 2 == 0 ?
new string(ch, 1, ch.Length - 1) : new string(ch);
}
}
8)SECTOR6-08
SKILL RATING:3
using System;
public class Program
{
public static int Puzzle(string a, string b)
{
return a.Length > b.Length ? a.Length : b.Length;
}
}
9)SECTOR6-09
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string a, string b)
{
return a.Length > b.Length ? a : b;
}
}
10)SECTOR6-10
SKILL RATING:2
using System;
public class Program
{
public static string Puzzle(string a, string b)
{
return a.Length == b.Length ? a + b : (a.Length > b.Length ? a : b);
}
}
SKILL RATING:3
using System;
public class Program
{
public static int Puzzle(string s)
{
return s.Length / 3;
}
}
11)SECTOR6-11
SKILL RATING:1
这道题我没有找到规律,只有一个可以过关的解法
using System;
public class Program
{
public static string Puzzle(int i, int j, string s)
{
char[] ch = new char[s.Length * 2 - 2 - (i + j)];
for (int x = 0; x < ch.Length; x++)
{
ch[x] = s[i > j ? i : j];
}
if (i == 0 && j == 0 && s.Length > 2)
{
for (int x = 0; x < ch.Length; x++)
{
if (x % 2 == 0) ch[x] = s[0];
else ch[x] = s[1];
}
}
else if (i == 0 && j == 1)
{
ch[0] = s[0];
}
else if (i == 1 && j == 0)
{
ch[1] = s[0];
}
else if (i == 1 && j == 2)
{
ch[0] = s[1];
ch[2] = s[1];
}
else
{
for (int x = 0; x < ch.Length; x++)
{
if (x % 2 == 0) ch[x] = s[i];
else ch[x] = s[j];
}
}
return new string(ch);
}
}
12)SECTOR6-12
SKILL RATING:1
using System;
public class Program
{
public static string Puzzle(string s)
{
char[] ch = new char[s.Length * 2];
for (int x = 0; x < s.Length; x++)
{
ch[x] = s[x];
ch[s.Length * 2 - 1 - x] = s[x];
}
return new string(ch);
}
}
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string s)
{
char[] ch1 = s.ToCharArray();
char[] ch2 = s.ToCharArray();
Array.Reverse(ch2);
return new string(ch1) + new string(ch2);
}
}
2.SECTOR7:STRINGS2
1)SECTOR7-1
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string one, string two, string three)
{
return two + three + one + one + three + two;
}
}
2)SECTOR7-2
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string s)
{
return s.Substring(0, s.Length / 2);
}
}
3)SECTOR7-3
SKILL RATING:1
public class Program
{
public static string Puzzle(string a, string b, string c)
{
char[] ch = a.ToCharArray();
for (int i = 0; i < a.Length; i++)
{
ch[i] = ch[i] != b[0] ? ch[i] : c[0];
}
return new string(ch);
}
}
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string a, string b, string c)
{
return a.Replace(b[0], c[0]);
}
}
4)SECTOR7-4
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);
}
}
5)SECTOR7-5
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string a, string b)
{
char[] ch = new char[a.Length];
for (int i = 0; i < a.Length; i++)
{
if (i % 2 == 0) ch[i] = b[i];
else ch[i] = a[i];
}
return new string(ch);
}
}
6)SECTOR7-6
SKILL RATING:3
using System;
public class Program
{
public static string Puzzle(string s)
{
return s.Replace(" ", "");
}
}
7)SECTOR7-7
SKILL RATING:1
using System;
public class Program
{
public static string Puzzle(string s)
{
char[] ch = new char[s.Length];
int counter = 0;
foreach (char c in s)
{
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u')
{
ch[counter] = c;
counter++;
}
}
return (new string(ch)).Substring(0, counter);
}
}
SKILL RATING:2
using System;
public class Program
{
public static string Puzzle(string s)
{
return s.Replace("a", "").Replace("e", "").
Replace("i", "").Replace("o", "").Replace("u", "");
}
}
8)SECTOR7-8
SKILL RATING:2
using System;
public class Program
{
public static bool Puzzle(string input, string a, string b, string c)
{
if (input.IndexOf(a) == 0 &&
input.IndexOf(b) != -1 &&
input.LastIndexOf(c) == input.Length - c.Length)
{
return true;
}
else
return false;
}
}
SKILL RATING:3
using System;
public class Program
{
public static bool Puzzle(string input, string a, string b, string c)
{
return (input.IndexOf(a) == 0 && input.IndexOf(b) != -1 &&
input.LastIndexOf(c) == input.Length - c.Length) ? true : false;
}
}
9)SECTOR7-9
SKILL RATING:2
using System;
public class Program
{
public static string Puzzle(int i, string s)
{
string result = s;
while (--i > 0)
{
result += (" " + s);
}
return result;
}
}
10)SECTOR7-10
SKILL RATING:1
using System;
public class Program
{
public static string Puzzle(int t)
{
string s = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
switch (t)
{
case 1: return "a b c d e f g h i j k l m n o p q r s t u v w x y z";
case 2: return "a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x z";
case 3: return "a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w z";
case 4: return "a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v z";
case 5: return "a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u v w x y z" +
" a b c d e f g h i j k l m n o p q r s t u z";
default: return "";
}
}
}
SKILL RATING:?
下面这段代码没有通过,但我并不清楚具体原因,因为本段代码输出字符串和结果看上去是完全一样的。
using System;
public class Program
{
public static string Puzzle(int t)
{
string s = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
string result = s;
int counter = t;
while (--counter > 0)
{
result += (" " + s);
}
return result.Substring(0, t * 50) + " z";
}
}
END