【洛谷OJ】【JAVA】P1464 Function

36 篇文章 0 订阅

https://www.luogu.org/problemnew/show/P1464

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;
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值