Max Number of A's given four keys
来源:互联网 发布:浪潮软件论坛 编辑:程序博客网 时间:2024/06/05 10:36
/* Imagine you have a special keyboard with following four keys: Key 1: Prints 'A' on screenKey 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to bufferKey 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. If you can only press the keyboard for N times (with the above four keys), write a program to produce maximum numbers of A's.*/// attention, Ctrl-A and Ctrl-C can copy the text into buffer but not print out.// Ctrl-V is to print out.// Ctrl-A + Ctrl-C + Ctrl-V can be sperate and coherant.#include <iostream>#include <vector>using namespace std;int findMax(int n, vector<int>& maxSolution) { if(n < 0) return -1; if(n <= 6) return 6; int max_so_far = 0; int max_with_this_i = 0; int multiplier = 2; // why start at n - 3? // Suppose we have input n./* Suppose we can the following numbes. n - 9 n - 8 n - 7 n - 6 f(n-6), ctrA, ctrlc, ctrlV, ctrlV, ctrlV, ctrV n - 5 f(n-5) ctrA, ctrlC, ctrlV, ctrlV, ctrV n - 4 f(n-4) ctrlA, ctrlC, ctrlV, ctrV n - 3 f(n-3), ctrlA, ctrlC, ctrV n - 2 f(n-2) n - 1 f(n-1) n f(n)*/ for(int i = n - 3; i >= 0; --i) { if(maxSolution[i] == -1) maxSolution[i] = findMax(i, maxSolution); max_as_with_this_i = multiplier * maxSolution[i]; if(max_as_with_this_i > max_so_far) { max_so_far = max_as_with_this_i; } multiplier += 1; } return max_so_far;}
0 0
- Max Number of A's given four keys
- Find distance between two given keys of a Binary Tree
- Binary representation of a given number
- All factors of a given number
- Write a method that counts the number of occurrences of a given character in a string
- P15 (**) Replicate the elements of a list a given number of times.
- P14 (*) -P15 (**)Duplicate the elements of a list,Duplicate the elements of a list a given number of
- max sub array of the given array
- Find whether a given number is a power of 4 or not
- P23 (**) Extract a given number of randomly selected elements from a list.
- Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa
- 《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
- LeetCode Online Judge:Given a set of distinct integers, S, return all possible subsets.
- Add 1 to a given number
- Swap bits in a given number
- Cannot format given Object as a Number
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Servlet & JSP 面试题
- Maximum Depth of Binary Tree
- Linux课上学习c语言文件操作及读写
- Compare Version Numbers
- 网络流入门
- Max Number of A's given four keys
- 促销系统
- (seleluim 七) xlrd模块处理execl文档
- [Java]《Java编程思想》知识点总结(坚持逐步更新)
- 浅谈java中的多线程及synchronized锁重入的含义
- Java千百问_08JDK详解(002)_jdk工具集都有什么
- Windows软件安装包制作工具汇总
- 创建repository上传到github以及基本操作
- Distinct binary strings of length n with no consecutive 1s