zoukankan      html  css  js  c++  java
  • 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;
    	}
    	
    }
    
  • 相关阅读:
    启用了不安全的HTTP方法
    Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'
    python发邮件
    糗事百科爬虫
    链接爬虫
    简单图片爬虫
    常见的非贪婪匹配
    [Selenium] WebDriver 操作 HTML5 中的 drag/drop
    [Selenium] 操作 HTML5 中的 Canvas 绘制图形
    [Selenium] WebDriver 操作 HTML5 中的 video
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947135.html
Copyright © 2011-2022 走看看