第一个题 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; }
import java.util.Scanner;//有1分,2分,5分,10分四种硬币,每种硬币数量无限,给定n分钱,求有多少种组合可以组合成n分钱? public class Test { public static void main(String[] args) { int x1,x2,x5,x10;//分别定义1,2,5,10分钱的数量 int sum=0; Scanner input = new Scanner(System.in); int n=input.nextInt();//接收输入的n for(x10=0;x10<=n/10;x10++){ for(x5=0;x5<=(n-x10*10)/5;x5++){ for(x2=0;x2<=(n-x10*10-x5*5)/2;x2++){ for(x1=0;x1<=(n-x10*10-x5*5-x2*2);x1++){ if((x1+2*x2+5*x5+10*x10)==n){ sum++; } } } } } System.out.println("组合共有"+sum+"种"); } } 第一题把6楼的代码改进了一下,不知道还有没有更高效的,可以分享下。 第二题有空再写了,有点晚了,先睡了
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;
}
}随便找了几个进去测的 有不对之处 请指教。
身高 体重
A: 200 200
B: 100 150
C: 150 100
D: 110 50
E: 80 30
如果按照ACDE的顺序可以添加4层,如果按照ABE的顺序只能添加三层。
而你的遍历的顺序是先后,也就说第一个添加之后,第二个数据应该是先添加B,也就是错误的数据。
我觉得出题的人应该就是想考是不是考虑到这一层,第一层的出题点我还没想出来,我感觉应该不是简单的FOR循环那么简单,暂时猜测是不是考的是算法的运算时间。
public class Test {
public static void main(String[] args) {
int x1,x2,x5,x10;//分别定义1,2,5,10分钱的数量
int sum=0;
Scanner input = new Scanner(System.in);
int n=input.nextInt();//接收输入的n
for(x10=0;x10<=n/10;x10++){
for(x5=0;x5<=(n-x10*10)/5;x5++){
for(x2=0;x2<=(n-x10*10-x5*5)/2;x2++){
for(x1=0;x1<=(n-x10*10-x5*5-x2*2);x1++){
if((x1+2*x2+5*x5+10*x10)==n){
sum++;
}
}
}
}
}
System.out.println("组合共有"+sum+"种");
}
}
第一题把6楼的代码改进了一下,不知道还有没有更高效的,可以分享下。
第二题有空再写了,有点晚了,先睡了