日期:2014-05-20  浏览次数:20753 次

创新工场笔试题 帮忙详解
1,有1分,2分,5分,10分四种硬币,每种硬币数量无限,给定n分钱,求有多少种组合可以组合成n分钱?

2,马戏团里有个叠罗汉的表演,为了便于美观,下面的人身高和体重都要大于上面的人。现在知道n个演员的身高和体重,请问最多能叠多少层?

------解决方案--------------------
第一个题 for()循环嵌套 
import java.util.Scanner;
public class Coin
{
public static void main(String [] args)
{
int sum=0;
System.out.println("Input a number is n:"); //输入一个n的值
Scanner input = new Scanner(System.in);
int n=input.nextInt();
for(int i=0;i<=n/10;i++) // 10 分 的个数
for(int j=0;j<=(n-10*i)/5;j++) //5分 的个数
for(int m=0;m<=(n-10*i-5*j)/2;m++) // 2 分的个数
for(int t=0;t<=n-10*i-5*j-2*m;t++) // 1分的个数
if(10*i+5*j+2*m+t==n) // 判断是不是 相等
sum++;
System.out.println(sum);
}
}
------解决方案--------------------
代码在这里 
package practice;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Test {
public static void main(String[] args) {
Set<Person> set = new TreeSet<Person>(new Mycomparator());

Person p1 = new Person(18.1,57.5);
Person p2 = new Person(17.2,56.4);
Person p3 = new Person(18.3,45.5);
Person p4 = new Person(16.5,80.5);
Person p5 = new Person(19.3,60.5);
Person p6 = new Person(19.2,56.0);
set.add(p1);
set.add(p2);
set.add(p3);
set.add(p4);
set.add(p5);
set.add(p6);
for(Iterator ite =set.iterator();ite.hasNext();){
Person p = (Person)ite.next();
System.out.println(p.getHeight());


}



}

 class Person 
 {
private double height;
private double weight;
public Person(double height,double weight){
this.height=height;
this.weight=weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
 // 自定义的比较器
 class Mycomparator implements Comparator{


public int compare(Object o1,Object o2) {
Person p1 =(Person)o1;
Person p2 =(Person)o2;
// 比较只能体重和身高都占优势的时候才能加进去
if(p1.getHeight()>p2.getHeight()&&p1.getWeight()>p2.getWeight()){
return 1;
}else if(p1.getHeight()<p2.getHeight()&&p1.getWeight()<p2.getWeight()){
return 1;
}
return 0;
}
 
 }随便找了几个进去测的 有不对之处 请指教。