北大ACM试题1010-暴力搜索

来源:互联网 发布:广西银河商品交易软件 编辑:程序博客网 时间:2024/05/19 03:46

这道题初看不难,大致的题意就是有很多种邮票(题目说是不超过25种,实际据说有26。。),每种邮票的面额随机(可以有一样的面额,但是也算两种),现在给出一个顾客的需求,最多可以取4张邮票,然后求最优解(括号里面的数字表示邮票的种类,而不是可能解的个数!),什么是最优解,即尽量多的使用那个不同种类的邮票;若有多种方案满足,则选取张数最小的一种方案;若仍有多种方案满足,则选取“最大面额”最高的一种方案;若依然有多种方案满足,则输出 “tie”。

这道题很多人都说是水题,因为邮票最多就4张,用暴力搜索,26^4的开销并不大,这也是我们这种战斗力只有5的渣渣能想出来的了。。。然后就是下面的代码了,并不难。但是用DFS还有动态规划都可以解题,这就很有意思了。。。仔细研究一下~~

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class Main_1010_Force {


//获得邮票的种类
public static int getStampType(int[] data){
int type = data.length;
int temp = 0;
for(int i=1;i<data.length;i++){
if(data[i] == data[temp]){
type--;
continue;
}
temp = i;
}
return type;
}

//优先级的比较,0是种类,1是数量,2是最大面额
public static int compare(int[] a,int[] b){
if(a[0] > b[0]){
return 1;
}else if(a[0] == b[0]){
if(a[1] > b[1]){
return -1;
}else if(a[1] == b[1]){
if(a[2] > b[2]){
return 1;
}else if(a[2] == b[2]){
return 0;
}else{
return -1;
}
}else{
return 1;
}
}else{
return -1;
}
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
List<String> output = new ArrayList<String>();
while(cin.hasNext()){
String stampss = cin.nextLine();
String[] stamps = stampss.split(" ");
int[] stamp = new int[stamps.length];
for(int i=0;i<stamps.length;i++){
stamp[i] = Integer.parseInt(stamps[i]);
}
//对输入的邮票进行排序,小心第一个是0。。。
Arrays.sort(stamp);
String[] customers = cin.nextLine().split(" ");
for(int i=0;i<customers.length-1;i++){
int customer = Integer.parseInt(customers[i]);
int count = 0;//解的个数,最后仍然为0输出none
int types = 0;//最优解用到的邮票种类
int[] priority = new int[3];//优先级,邮票种类最多,总张数最少,最大面额最大
String solution = "";
for(int j=1;j<stamp.length;j++){
if(stamp[j] == customer){
count ++ ;
int[] tempP = {1,1,stamp[j]};
//每次循环比较优先级,比目前最优解的优先级高的就更新,相等暂时为tie
//后面的每次循环过程都跟这类似
if(compare(tempP,priority) == 0){
solution = "tie";
continue;
}
if(compare(tempP,priority) == 1){
types = 1;
priority = tempP;
solution = stamp[j]+"";
}
}
for(int k=j;k<stamps.length;k++){
if(stamp[j] + stamp[k] == customer){
count ++ ;
int[] tempP = {getStampType(new int[]{j,k}),2,stamp[k]};
if(compare(tempP,priority) == 0){
solution = "tie";
continue;
}
if(compare(tempP,priority) == 1){
types = tempP[0];
priority = tempP;
solution = stamp[j]+" "+stamp[k];
}
}
for(int m=k;m<stamps.length;m++){
if(stamp[j] + stamp[k] + stamp[m] == customer){
count ++ ;
int[] tempP = {getStampType(new int[]{j,k,m}),3,stamp[m]};
if(compare(tempP,priority) == 0){
solution = "tie";
continue;
}
if(compare(tempP,priority) == 1){
types = tempP[0];
priority = tempP;
solution = stamp[j]+" "+stamp[k]+" "+stamp[m];
}
}
for(int n=m;n<stamps.length;n++){
if(stamp[j] + stamp[k] + stamp[m] + stamp[n] == customer){
count ++ ;
int[] tempP = {getStampType(new int[]{j,k,m,n}),4,stamp[n]};
if(compare(tempP,priority) == 0){
solution = "tie";
continue;
}
if(compare(tempP,priority) == 1){
types = tempP[0];
priority = tempP;
solution = stamp[j]+" "+stamp[k]+" "+stamp[m]+" "+stamp[n];
}
}
}
}
}
}
String o = customer+" ";
if(count == 0){
o += "---- none";
}else{
o += "("+types+"): "+solution;
}
output.add(o);
}
}
for(int i=0;i<output.size();i++){
System.out.println(output.get(i));
}
cin.close();
}
}

下一篇研究一下DFS(深度优先搜索)来解题~~~

0 0
原创粉丝点击