悲剧的,做了一天,本来用DFS回溯,很快写出了解法,但是发现超时啊。。提交了n次。。研究了半天。。
最后放弃,改用另外的做法来做。解题的思路也换了。40分钟搞定了。
import java.util.Scanner;
/*
* 思路转换,将题意理解为用6个数填充1-100这个数组,
* 如1 24 34 39 46 50
* 那么a[1]=1 a[24]=1 ... a[50]=1,这些为第一层
* 循环,a[25](24+1 =25) a[23](24-1=23) = 2...
* 依次类推,将1,2之类记为层数,n即位2数的层数之和,a[25]即为层数为2(n==2)的层数构成
* 第一次,先记录层数为1的数,即currency
* 第2次先产生层数为2的数,第3次产生层数为1+2 或2+1的数
* 第i次产生层数为 i + n-i的数
* 思路如此。。。继续编码
*/
public class Main {
public static void main(String[] args) {
int currency[] = {1, 24, 34, 39, 46, 50};
Scanner sc = new Scanner(System.in);
int num = Integer.parseInt(sc.next());
do{
getCurrency(sc, currency);
num --;
calculate(currency);
}while(num != 0);
}
public static void getCurrency(Scanner sc, int [] currency){
for(int i = 0; i < 6; i ++){
currency[i] = sc.nextInt();
}
}
public static void calculate(int currency[]){
int res[] = new int[101];
for(int i = 0 ; i < 6 ; i ++){//产生第一层
res[currency[i]] = 1;
}
int count = 6;
int n = 2;//层次之和
while(count < 100){//跳出循环的条件为count == 100
for(int i = 1 ; i < 101; i ++){
for(int j = 1 ; j < 101 ; j ++){
for(int k = 1 ; k < n; k ++){//控制层数
if(res[i] == k && res[j] == n-k){
if( i + j <= 100 &&res[i + j] == 0){
res[i+j] = n;
count ++;
}
if(i != j && res[Math.abs(i-j)] == 0){
res[Math.abs(i-j)] = n;
count ++;
}
}
}
}
}
n ++;
}
double sum = 0;
int max = 0;
for(int i = 1 ; i < 101 ; i ++){
sum += res[i];
if(max < res[i]){
max = res[i];
}
}
System.out.printf("%.2f %d\n", sum/100, max);
}
}
//另附上弄了好久的代码,虽然超时。。但是也搞了好久。。不留作纪念说不过去。。悲剧!
import java.util.Scanner;
/*
* 悲剧的超时了
* DFS暴力
*/
public class EuroEfficiency {
public static int []currency = new int[6];
public static int result[] = new int[201];//-100 -1 0(->100) 1 100 共201个
public static int res = 101;
public static void calculate(int money, int total){
if(money == 0){
if(res > total){
res = total;
}
return ;
}
if(total >= res){
return ;
}
if(money >0){
for(int i = 5; i >= 0; i --){
if(total + 1 >= res ){
break;
}
if(money - currency[i] >= (-1 * currency[5])){
if(result[money - currency[i] + 100] != 0){
calculate(0, total + result[money - currency[i] + 100]+1);
}else{
calculate(money - currency[i], total + 1 );
}
}
}
}else{
for(int i = 0; i <6; i ++){
if(total + 1 >= res ){
break;
}
if(money + currency[i] <= currency[5]){
if(result[money + currency[i] + 100] != 0){
calculate(0, total + result[money + currency[i] + 100]+1);
}else{
calculate(money + currency[i], total + 1 );
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
do{
getCurrency(sc);
n --;
double sum = 0;
int max = 1;
for(int i = 1; i <= 100; i ++){
res = 101;
calculate(i, 0);
result[i+100] = res;
sum += res;
if(res > max){
max = res;
}
}
for(int i = 100 ; i < 201; i ++){
System.out.println(result[i]);
}
System.out.printf("%.2f %d\n",sum/100, max);
}while(n != 0);
}
public static void getCurrency(Scanner sc){
result = new int[201];
for(int i = 0; i < 6; i ++){
currency[i] = sc.nextInt();
}
}
}
分享到:
相关推荐
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot-vue交通管理在线服务系统的开发源码(完整前后端+mysql+说明文档+LunW).zip