题目
-某人有5张3分和4张5分的邮票,请编写一个程序,计算由这些邮票中的1张或若干张可以得到多少种不同的邮资,并按照邮资从小到大顺序显示。
代码
public class Test02 {
public static void main(String[] args) {
Set set = new HashSet();//定义集合存储符合的数据,使用set是因为set不可重复
int total;//邮资
for (int i = 0;i<=5;i++){
for(int j = 0;j<=4;j++){
total = 3*i+5*j;
if (total==0){
continue;//排除0
}else {
set.add(total);
}
}
}
System.out.println("-----------------------");
Iterator iterable = set.iterator();
while (iterable.hasNext()){
System.out.println(iterable.next());
}
System.out.println("个数为:"+set.size());
}
}