code hunt题解(1)

code hunt 这个网站类似于learn codecademyd,都是通过闯关,给你一些碎片化的程序片段,然后你需要完成它需要你返回的结果。但是怎么说呢,code hunt的题目更加无厘头一点,因为他只会给你一些你需要返回的数据,而并没有明确说明如何得到。所以它更类似游戏(有人说这是微软的网游,其实真的很不错,安利一下)。当然这个网站也有C#的学习(貌似网上有大牛看书刷这个就进了编程之美C#复赛,哈哈哈想想那些学C#的还天天打游戏的人,多亏我来hit了)逃。恩,注册一个微软账号或者facebook就好了,不过我只写到了第二关。以下只是我个人的答案不过都拿到三星了,要是有人有更好的交流交流呗。。。

网址:https://www.codehunt.com

00.01 简单的熟悉该网站的操作。

00.02

public class Program {
    public static int Puzzle(int x) {
        return x+1;
    }
}

00.03

public class Program {
    public static int Puzzle(int x) {
        return x*2;
    }
}

00.04

public class Program {
    public static int Puzzle(int x, int y) {
        return x+y;
    }
}

01.01

public class Program {
    public static int Puzzle(int x) {
        return -x;
    }
}

01.02

public class Program {
    public static int Puzzle(int x) {
        return x-2;
    }
}

01.03

好吧,这道题33和1089的关系,真的一时没做出来,只好多尝试几次当题目给出更多数据终于知道了。

public class Program {
    public static int Puzzle(int x) {
        return x*x;
    }
}

01.04

public class Program {
    public static int Puzzle(int x) {
        return x*3;
    }
}

01.05

public class Program {
    public static int Puzzle(int x) {
        return x/3;
    }
}

01.06

public class Program {
    public static int Puzzle(int x) {
        return 4/x;
    }
}

01.07

public class Program {
    public static int Puzzle(int x, int y) {
        return x-y;
    }
}

01.08

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

01.09

public class Program {
    public static int Puzzle(int x, int y) {
        return x*y;
    }
}

01.10

唔,这道题同01.03都是小boss啊。还是我太渣了。。。

public class Program {
    public static int Puzzle(int x, int y) {
        return x+y/3;
    }
}

01.11

public class Program {
    public static int Puzzle(int x, int y) { 
        return x/y;
    }
}

01.12

public class Program {
    public static int Puzzle(int x) {
        return x%3;
    }
}

01.13

由上一题自然想出来。。。

public class Program {
    public static int Puzzle(int x) {
        return x%3+1;
    }
}

01.14

public class Program {
    public static int Puzzle(int x) {
        return 10%x;
    }
}

01.15

public class Program {
    public static int Puzzle(int x, int y, int z) {
        return (x+y+z)/3;
    }
}

02.01

这一关开始进入了Java中数组的学习。。。。不过我当时还没真正学习Java,其实是刚学完C的数组,所以只能百度谷歌了用法。Java中允许int[] a和int a []这两种定义方法,但是前者更好,因为它可以明确告知我们定义了一个数组。

public class Program {
    public static int[] Puzzle(int n) {
        int i;
        int [] score=new int[n];
        for(i=0;i<n;i++)
        {
            score[i]=i;
        }
        return score;
    }
}

02.02

public class Program {
    public static int[] Puzzle(int n) {
        int i;
        int [] score=new int[n];
        for(i=0;i<n;i++)
        {
            score[i]=i*n;
        }
        return score;
    }
}

02.03

public class Program {
    public static int[] Puzzle(int n) {
        int i;
        int [] a=new int[n];
        for(i=0;i<n;i++)
        {
            a[i]=i*i;
        }
        return a;   
    }
}

02.04

这一关有些Java的色彩了,v.length是Java中数组的属性:长度。

public class Program {
    public static int Puzzle(int[] v) {
            int sum=0,i;
            for(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

进入字符串。额Java中字符串有很多方法,比如求字符串s的长度的方法:s.length()。注意区别:s.length()和上面的.length不同,一个是方法一个是属性,至于二者的区别和含义以后再说吧。其实.length方法和strlen一样都是不计算NUL也就是’’的。

public class Program {
    public static int Puzzle(String s) {
          String s1 = s.replace("a","");
          
          return s.length()-s1.length();
    }
}

02.07

这一关我学会了如何把一个字符型变量转化为字符串的最简单方法,当然Java中也有专门的方法。””+x和x+””含义一样都是把x转化为字符串”x”。

public class Program {
    public static int Puzzle(String s, char x) {
        String s1=s.replace(""+x,"");
        return s.length()-s1.length();
    }
}

以前的就写到这里了,Java到现在也是刚入门,也在看《Java核心技术》和一些视频,但是还是没有完全弄懂OOP,所以要努力。而且看网上说.replace这个方法有正则表达式的内容,还需要在研究一下。。。

原文地址:https://www.cnblogs.com/kugwzk/p/5043241.html