java实现 洛谷 P1464 Function


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private static Scanner cin;
private static HashMap<String, Integer> map = new HashMap<String, Integer>();
public static void main(String args[]) throws Exception {
cin = new Scanner(System.in);
boolean loop = true;
int a = 0;
int b = 0;
int c = 0;
//为了减少IO等待时间,先把数据读取进来存储到List
LinkedList<TripleInteger> list = new LinkedList<TripleInteger>();
while(loop) {
a = cin.nextInt();
b = cin.nextInt();
c = cin.nextInt();
if(-1==a && -1==b && -1==c) {
loop = false;
}else {
list.add(new TripleInteger(a,b,c));
}
}
Iterator<TripleInteger> it = list.iterator();
while(it.hasNext()) {
TripleInteger ti = it.next();
System.out.println(String.format("w(%d, %d, %d) = %d", ti.getA(),ti.getB(),ti.getC(),calc(ti.getA(),ti.getB(),ti.getC())));
}
}
public static int calc(int a, int b, int c) {
int ret = 1;
if( a<=0 || b<=0 || c<=0) {
return ret;
}else if(a>20 || b>20 || c>20){
ret = calc(20,20,20);
}else if(map.containsKey(String.format("%d-%d-%d", a,b,c))) {
return (Integer)map.get(String.format("%d-%d-%d", a,b,c));
}
//如果a<b并且b<c 就返回w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c)
else if(a<b && b<c) {
ret = calc(a,b,c-1)+calc(a,b-1,c-1)-calc(a,b-1,c);
map.put(String.format("%d-%d-%d", a,b,c), ret);
}
//w(a−1,b,c)+w(a−1,b−1,c)+w(a−1,b,c−1)−w(a−1,b−1,c−1)
else {
ret = calc(a-1,b,c)+calc(a-1,b-1,c)+calc(a-1,b,c-1)-calc(a-1,b-1,c-1);
map.put(String.format("%d-%d-%d", a,b,c), ret);
}
return ret;
}
}
final class TripleInteger{
int a;
int b;
int c;
public TripleInteger(int a,int b,int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
}
java实现 洛谷 P1464 Function的更多相关文章
- 洛谷P1464 Function HDU P1579 Function Run Fun
洛谷P1464 Function HDU P1579 Function Run Fun 题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值11. 如果a> ...
- (水题)洛谷 - P1464 - Function
https://www.luogu.org/problemnew/show/P1464 #include<bits/stdc++.h> using namespace std; #defi ...
- 洛谷 P1464 Function【记忆化搜索】
题目链接 题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w ...
- 洛谷 P1464 Function【动态规划(递推)/记忆化搜索(递归)】
题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,2 ...
- 洛谷 P1464 Function
题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,2 ...
- 洛谷P1464 Function
对于一个递归函数w(a,b,c)w(a,b,c) 如果a \le 0a≤0 or b \le 0b≤0 or c \le 0c≤0就返回值11. 如果a>20a>20 or b>20 ...
- 【做题笔记】洛谷P1464 Function
我先谔谔一波 /kk 我谔谔 看题第一眼:欸这不就是按题意递归嘛,,直接搞不就好了 3 min 后,重新看题 然后自己手玩了几个样例,噢,递归太多了,铁定会 T 啊...... 然后,作为一个从没写过 ...
- 洛谷 P1464 Function(简单记忆化)
嗯... 让一切从水开始吧... 水过初赛,但愿复赛能够接着水过... 这道题不记忆化会tle,所以用空间换时间,将每次的答案(只有20*20*20个)存下来,如果之前已经求过,就不需要重复求了... ...
- Java实现 洛谷 Car的旅行路线
输入输出样例 输入样例#1: 1 3 10 1 3 1 1 1 3 3 1 30 2 5 7 4 5 2 1 8 6 8 8 11 6 3 输出样例#1: 47.5 import java.util. ...
随机推荐
- mysql计算
select @csum := 0;select create_time,merchant_id,award as 奖励,total_count as 数量,(@csum := @csum + awa ...
- Android Loader使用时,屏幕解锁后,重复加载
在使用AsyncTaskLoader时,当手机解锁后,会重复加载数据,代码如下: static class CouponShopQueryLoader extends AsyncTaskLoader& ...
- 我的linux学习日记day5
一.vim 编辑器 有三种模式,命令模式,输入模式,末行模式 1.下面是命令模式常用的命令 2.末行模式常用命令 :w 保存 :q 退出 :q! 强制退出 :wq! 强制保存退出 :set nu 显示 ...
- python基本数据类型:字符串及其方法(二)
格式化类 方法join() #join()用指定字符每间隔拼接字符串 name='miku' name1=' '.join(name) print(name1) 方法center() #center( ...
- 基于Javaee的影视创作论坛的设计与实现
基于Javaee的影视创作论坛的设计与实现主要用功能包括: 首页推荐.用户管理.影片管理.评论管理. 预告片管理.海报管理.公告管理.数据检索.用户注册与登录等等功能.统结构如下 (1)后台管理: 管 ...
- 浅析String、StringBuilder、StringBuffer
谈谈我对 String.StringBuilder.StringBuffer 的理解 StringBuilder.StringBuffer 和 String 一样,都是用于存储字符串的. 1.那既然有 ...
- BZOJ 1028 BZOJ 1029 //贪心
1028: [JSOI2007]麻将 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2197 Solved: 989[Submit][Status][ ...
- Poj1328 用雷达覆盖所有的岛屿
(此配图来自http://blog.csdn.net/zhengnanlee/article/details/9613161) 图中ABCD为海岛的位置.题目中会给出几个海岛的坐标位置,雷达覆盖半径d ...
- 关于pytest使用allure生成报告时,报一堆警告和缺少XX模块
因为最新的pytest 支持*.josn的用例报告,卸载旧的模块使用新的即可: 需要移除旧模块:pip uninstall pytest-allure-adaptor, 并安装:pip install ...
- 基于Pytest豆瓣自动化测试【1】
-- Pytest基础使用教程[1] 引言 Pytest 是一个非常实用的自动化测试框架,目前来说资料也是非常多了.最近某友人在学习 Python的一些测试技术,帮其网上搜了下教程:发现大多数文章多是 ...