Code Hunt 题解 00-04 (Java)

来源:互联网 发布:笔记本无线找不到网络 编辑:程序博客网 时间:2024/06/09 22:41

最近做了做微软的编程小游戏 Code Hunt,这是一个给你输入输出,让你反推程序代码,同时会根据你的代码精炼程度打分的小游戏。
强迫症同学们请小心哦~
链接:https://www.codehunt.com
作为一个java新手,最近刷这个刷了半天~ 给大家贴个题解(网上的怎么都是C#版本的)
(嗯,00-08中的好多都来自于网络。。。后面的就是自己做的了)

这里写图片描述
Sector 00 Training

00.02public class Program {    public static int Puzzle(int x) {        return x+1;    }}
00.03public class Program {    public static int Puzzle(int x) {        return x*2;    }}
00.04public class Program {    public static int Puzzle(int x, int y) {        return x+y;    }}

Sector 01 Arithmetic

01.01public class Program {    public static int Puzzle(int x) {        return -x;    }}
01.02public class Program {    public static int Puzzle(int x) {        return x-2;    }}
01.03public class Program {    public static int Puzzle(int x) {        return x*x;    }}
01.04public class Program {    public static int Puzzle(int x) {        return x*3;    }}
01.05public class Program {    public static int Puzzle(int x) {        return x/3;    }}
01.06public class Program {    public static int Puzzle(int x) {        return 4/x;    }}
01.07public class Program {    public static int Puzzle(int x, int y) {        return x-y;    }}
01.08public class Program {    public static int Puzzle(int x, int y) {        return x+2*y;    }}
01.09public class Program {    public static int Puzzle(int x, int y) {        return x*y;    }}
01.10public class Program {    public static int Puzzle(int x, int y) {        return x+y/3;    }}
01.11public class Program {    public static int Puzzle(int x, int y) {         return x/y;    }}
01.12public class Program {    public static int Puzzle(int x) {        return x%3;    }}
01.13public class Program {    public static int Puzzle(int x) {        return x%3+1;    }}
01.14public class Program {    public static int Puzzle(int x) {        return 10%x;    }}
01.15public class Program {    public static int Puzzle(int x, int y, int z) {        return (x+y+z)/3;    }}

Sector 02 Loops

02.01public class Program {    public static int[] Puzzle(int n) {        int[]puzzle=new int[n];        for (int i=0;i<n;i++) puzzle[i]=i;        return puzzle;     }}
02.02public class Program {    public static int[] Puzzle(int n) {        int[]puzzle=new int[n];        for (int i=0;i<n;i++) puzzle[i]=i*n;        return puzzle;    }}
02.03public class Program {    public static int[] Puzzle(int n) {        int[] puzzle=new int[n];        for (int i=0;i<n;i++) puzzle[i]=i*i;        return puzzle;    }}
02.04public class Program {    public static int Puzzle(int[] v) {         int sum=0;         for (int i=0;i<v.length;i++) sum+=v[i];         return sum;     }}
02.05 <必须直接写公式才有三星>public class Program {    public static int Puzzle(int n) {         return (n*(n-1)*(2*n-1))/6;    }}
02.06 <这题非常迷。。。replace()方法以后会常用>public class Program {    public static int Puzzle(String s) {        String s2=s.replace("a","");        return s.length()-s2.length();    }}
02.07public class Program {    public static int Puzzle(String s, char x) {        String s1=s.replace(x+"","");        return s.length()-s1.length();    }}

Sector 03 Loops 2

03.01 <写成递归才有三星>public class Program {    public static int Puzzle(int number, int power) {                 return power<=0?   1:Puzzle(number,power-1)*number;    }}
03.02public class Program {    public static int Puzzle(int i) {        return i<=0? 1:i*Puzzle(i-1);    }}
03.03public class Program {    public static int Puzzle(int lowerBound, int upperBound) {        return lowerBound>upperBound? 1: upperBound*Puzzle(lowerBound,upperBound-1);    }}
03.04 <神奇的整除>public class Program {    public static int Puzzle(int n) {        if (n<=2) return 0;        return ((n-1)/2) *((n-1)/2+1);    }}
03.05public class Program {    public static int Puzzle(int upperBound) {        return (upperBound)*(upperBound+1)*(upperBound+2)/6;    }}
03.06 <replaceAll()方法用到正则表达式!这题是照抄网上大神们的。。。。还有trim()方法会常用>public class Program {    public static String Puzzle(String word) {        return word.replaceAll("[a-z]","_ ").trim();            }}
03.07public class Program {    public static String Puzzle(String s) {        int m = 5;        char[] c = new char[s.length()];        for (int i=0;i<s.length();i++)         {            c[i]=(char)((s.charAt(i)-'a'+m)%26+'a');        }            return new String(c);    }}
03.08public class Program {    public static int Puzzle(int x) {        return (int)(Math.log10(x))+1;    }}

Sector 04 Conditionals

04.01public class Program {    public static Boolean Puzzle(Boolean x, Boolean y) {        return x || y;    }}
04.02public class Program {    public static Boolean Puzzle(Boolean x, Boolean y) {        return x && y;    }}
04.03public class Program {    public static Boolean Puzzle(int x) {         return (x<50);    }}
04.04public class Program {    public static Boolean Puzzle(int x, int y) {        return x<y;    }}
04.05public class Program {    public static int Puzzle(int i) {        return (int)(Math.signum(i));    }}
04.06public class Program {    public static Boolean Puzzle(int i, int j) {        return i>j;    }}
04.07public class Program {    public static int Puzzle(int i) {        return i<100? 2:3;    }}
04.08public class Program {    public static String Puzzle(int i) {        return i%2==0? "even": "odd";    }}
04.09public class Program {    public static String Puzzle(int i) {        return i%5==0?"multiple of 5":"not a multiple of 5";    }}
04.10public class Program {    public static String Puzzle(int i, int x) {        return i%x==0? "multiple of " + x : "not a multiple of " + x;    }}
04.11public class Program {    public static String Puzzle(int i, int j, int k) {        if (j==2*k && i==2*j) return("yes!");        return ("no");    }}
04.12 <多试几次就出来了>public class Program {    public static int Puzzle(int i) {        return i>14? 21: i>7? 7:0;    }}
0 0
原创粉丝点击