Code Hunt Sector 02 Loops - Java

原创
2016/06/04 13:10
阅读数 122

微软编码游戏 https://www.codehunt.com/

Code Hunt 02.01

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

 

Code Hunt 02.02

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

 

Code Hunt 02.03

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

 

Code Hunt 02.04

public class Program {
    public static int Puzzle(int[] v) {
		int result = 0;
		for (int i = 0; i < v.length; i++) {
			result = result + v[i];
		}
        return result;
    }
}

 

Code Hunt 02.05

public class Program {
    public static int Puzzle(int n) {
        return (n - 1) * n * (2 * n - 1) / 6;
    }
}

 

Code Hunt 02.06

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

 

Code Hunt 02.07

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

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部