Java实现 洛谷 P1028 数的计算

在这里插入图片描述

import java.util.Scanner;
import java.util.Arrays;
 
public class Main {
	private static Scanner cin;
	private static int[] rem = new int[501];
	
	
	public static void main(String args[]) throws Exception {
		cin = new Scanner(System.in);
		Arrays.fill(rem,-1);
		int n = cin.nextInt();
		System.out.println(calc(n/2)+1);
		
	}
	
	private static int calc(int step) {
		int ret = 0;
		if(-1 != rem[step]) {
			return rem[step];
		}
		if(step>=1) {
			for(int i=1;i<=step;i++) {
				if(-1 != rem[i]) {
					ret = ret + rem[i]+1;
				}else {
					ret = ret + calc(i/2)+1;
					rem[i] = ret;
				}
				
			}
		}
		
		return ret;
	}
}
posted @ 2019-07-30 18:35  南墙1  阅读(65)  评论(0编辑  收藏  举报