Code Hunt SECTOR4 & SECTOR5 (Conditionals1-2)

原创
2014/05/20 00:07
阅读数 526

1.关于本博客的说明

Code Hunt 是我从CSDN上的一篇文章中无意间看到的:微软研究院正式发布编程学习游戏Code Hunt,游戏地址从这里进入。

本篇博客是游戏的CONDITIONALS部分的C#部分解题代码

2.SECTOR4:CONDITIONALS

1)SECTOR4-01

SKILL RATING:3

using System;
public class Program
{
    public static bool Puzzle(bool x, bool y)
    {
        return x || y;
    }
}

2)SECTOR4-02

SKILL RATING:3

using System;
public class Program 
{
    public static bool Puzzle(bool x, bool y) 
    {
        return x&&y;
    }
}

3)SECTOR4-03

SKILL RATING:3

public class Program
{
    public static bool Puzzle(int x)
    {
        return x < 50 ? true : false;
    }
}

4)SECTOR4-04

SKILL RATING:3

using System;
public class Program
{
    public static bool Puzzle(int x, int y)
    {
        return y > x ? true : false;
    }
}

5)SECTOR4-05

SKILL RATING:2

using System;
public class Program
{
    public static int Puzzle(int i)
    {
        return i > 0 ? 1 : (i < 0 ? -1 : 0);
    }
}

SKILL RATING:3

using System;
public class Program
{
    public static int Puzzle(int i)
    {
        return i == 0 ? i : Math.Sign(i);
    }
}

6)SECTOR4-06

SKILL RATING:3

using System;
public class Program
{
    public static bool Puzzle(int i, int j)
    {
        return j > i ? false : true;
    }
}

7)SECTOR4-07

SKILL RATING:3

using System;
public class Program
{
    public static int Puzzle(int i)
    {
        return i < 100 ? 2 : 3;
    }
}

8)SECTOR4-08

SKILL RATING:3

判断数字奇偶

using System;
public class Program
{
    public static string Puzzle(int i)
    {
        return i % 2 == 0 ? "even" : "odd";
    }
}

9)SECTOR4-09

SKILL RATING:3

判断数字能否被5整除

public class Program
{
    public static string Puzzle(int i)
    {
        return i % 5 == 0 ? "multiple of 5" : "not a multiple of 5";
    }
}

10)SECTOR4-10

SKILL RATING:3

判断i能否被x整除

using System;
public class Program
{
    public static string Puzzle(int i, int x)
    {
        return i % x == 0 ?
            "multiple of " + x.ToString() :
            "not a multiple of " + x.ToString();
    }
}

11)SECTOR4-11

SKILL RATING:3

using System;
public class Program
{
    public static string Puzzle(int i, int j, int k)
    {
        return i == 2 * j && j == 2 * k ? "yes!" : "no";
    }
}

12)SECTOR4-12

SKILL RATING:3

using System;
public class Program
{
    public static int Puzzle(int i)
    {
        if (i < 8) return 0;
        if (i < 15) return 7;
        return 21;
    }
}

2.SECTOR5:CONDITIONALS2

1)SECTOR5-01

SKILL RATING:2

public class Program
{
    public static string Puzzle(string s)
    {
        if (s.Length < 4) return "short";
        if (s.Length < 8) return "average";
        if (s.Length < 15) return "long";
        return "super long";
    }
}

SKILL RATING:3

using System;
public class Program {
    public static string Puzzle(string s) {
        return s.Length < 4 ? "short" : (
            s.Length < 8 ? "average" : (
            s.Length < 15 ? "long" : "super long"));
    }
}

2)SECTOR5-02

SKILL RATING:3

using System;
public class Program
{
    public static string Puzzle(int i)
    {
        return i % 1111 == 0 ? "fancy year" : "not a fancy year";
    }
}

3)SECTOR5-03

SKILL RATING:2

判断三个数字是否为勾股数

using System;
public class Program
{
    public static bool Puzzle(int a, int b, int c)
    {
        if (a * a + b * b == c * c) return true;
        if (a * a + c * c == b * b) return true;
        if (b * b + c * c == a * a) return true;
        return false;
    }
}

SKILL RATING:3

using System;
public class Program
{
    public static bool Puzzle(int a, int b, int c)
    {
        return ((a * a + b * b == c * c) ||
            (a * a + c * c == b * b) ||
            (b * b + c * c == a * a)) ? true : false;
    }
}

4)SECTOR5-04

SKILL RATING:3

using System;
public class Program
{
    public static int Puzzle(int x, int y)
    {
        return Math.Abs(x) + Math.Abs(y);
    }
}

5)SECTOR5-05

SKILL RATING:3

using System;
public class Program 
{
    public static bool Puzzle(int i, int j) 
    {
        return i*i==j?true:false;
    }
}

END


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部