您的位置:首页 > 其它

【算法】网上看到的5个问题

2015-05-15 17:29 218 查看
网上看到的5个问题,下面是解答,不知道有没有其他建议!

问题1

使用for循环、while循环和递归写出3个函数来计算给定数列的总和。

package com.luka;

public class Javat {

private static int[] arr_Ints = { 2, 1, 4, 3, 6, 5, 8, 7, 10, 9 };

public static void main(String[] args) {
System.out.println("The Count is " + getNumByFor() + " .");
System.out.println("The Count is " + getNumByWhile() + " .");
System.out.println("The Count is " + getNumByEcursion(0) + " .");
}

/**
* for 循环
*/
private static int getNumByFor() {
int count = 0;
for (int i = 0; i < arr_Ints.length; i++) {
count += arr_Ints[i];
}
return count;
}

/**
* while 循环
*/
private static int getNumByWhile() {
int count = 0;
int i = 0;
while (i < arr_Ints.length) {
count += arr_Ints[i];
i++;
}
return count;
}

/**
* 递归
*/
private static int getNumByEcursion(int i) {
if (arr_Ints.length == 0)
return 0;
else if (i < arr_Ints.length - 1)
return arr_Ints[i] + getNumByEcursion(i + 1);
else
return arr_Ints[i];
}
}


问题2

编写一个交错合并列表元素的函数。例如:给定的两个列表为[a,B,C]和[1,2,3],函数返回[a,1,B,2,C,3]。

package com.luka;

public class Javat {

private static String[] arr1 = { "a", "B", "c", "D", "e" };
private static String[] arr2 = { "1", "2", "3" };

public static void main(String[] args) {
String[] arr = getNum(arr1, arr2);
for (int i = 0; i < arr.length; i++)
System.out.println("The Num is " + arr[i] + " .");
}

private static String[] getNum(String[] arr12, String[] arr22) {
String[] arr = new String[arr1.length + arr2.length];
int i, j;
for (i = 0, j = 0; i < arr1.length; i++) {
j = 2 * i;
if (j > 2 * arr2.length)
j = arr2.length + i;
arr[j] = arr1[i];
}
for (i = 0, j = 0; i < arr2.length; i++) {
j = 2 * i + 1;
if (j > 2 * arr1.length)
j = arr1.length + i;
arr[j] = arr2[i];
}
return arr;
}

}


问题3

编写一个计算前100位斐波那契数的函数。根据定义,斐波那契序列的前两位数字是0和1,随后的每个数字是前两个数字的和。例如,前10位斐波那契数为:0,1,1,2,3,5,8,13,21,34。

package com.luka;

public class Javat {
public static void main(String[] args) {
try {
System.out.println("The Nums is " + getCount(100) + " .");
} catch (Exception e) {
}
}

// 获取值
private static int getNum(int num) {
int count = 0;
if (num <= 1)
count = 0;
else if (num == 2)
count = 1;
else
count = getNum(num - 1) + getNum(num - 2);
return count;
}

// 获取和
private static String getCount(int num) {
String strNums = "";
for (int i = 0; i <= num; i++) {
strNums += getNum(i) + ",";
}
strNums = strNums.substring(0, strNums.length()-1);
return strNums;
}

}


问题4

编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021。

package com.luka;

import java.util.Arrays;
import java.util.Comparator;

public class Javat {

private static Integer[] VALUES = { 50, 2, 100, 99, 5, 7, 51,50 ,11};

public static void main(String[] args) {
Arrays.sort(VALUES, new Comparator<Object>() {
@Override
public int compare(Object lhs, Object rhs) {
String v1 = lhs.toString();
String v2 = rhs.toString();

return (v1 + v2).compareTo(v2 + v1) * -1;
}
});

String result = "";
for (Integer integer : VALUES) {
result += integer.toString();
}

System.out.println(result);
}

}


问题5

编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。

package com.luka;

import java.util.ArrayList;

public class Javat {
private static int TARGET_SUM = 100;
private static int[] VALUES = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

private static ArrayList add(int digit, String sign, ArrayList branches) {
for (int i = 0; i < branches.size(); i++) {
branches.set(i, digit + sign + branches.get(i));
}

return branches;
}

private static ArrayList f(int sum, int number, int index) {
int digit = Math.abs(number % 10);
if (index >= VALUES.length) {
if (sum == number) {
ArrayList result = new ArrayList();
result.add(Integer.toString(digit));
return result;
} else {
return new ArrayList();
}
}

ArrayList branch1 = f(sum - number, VALUES[index], index + 1);
ArrayList branch2 = f(sum - number, -VALUES[index], index + 1);

int concatenatedNumber = number >= 0 ? 10 * number + VALUES[index] : 10
* number - VALUES[index];
ArrayList branch3 = f(sum, concatenatedNumber, index + 1);

ArrayList results = new ArrayList();

results.addAll(add(digit, "+", branch1));
results.addAll(add(digit, "-", branch2));
results.addAll(add(digit, "", branch3));

return results;
}

public static void main(String[] args) {
ArrayList list = f(TARGET_SUM, VALUES[0], 1);
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
}

}


输出结果:

1+2+3-4+5+6+78+9
1+2+34-5+67-8+9
1+23-4+5+6+78-9
1+23-4+56+7+8+9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
123+4-5+67-89
123+45-67+8-9
123-4-5-6-7+8-9
123-45-67+89


来源:http://developer.51cto.com/art/201505/476097.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐