TCO 2016 R1C

SumFullSet

(1)问题描述:给定一个整数的集合,问该集合是否对加法封闭

Problem Statement
	
Let S be a sequence of (not necessarily distinct) integers. We say that S is closed under addition if it has the following property: For any pair of valid and distinct indices i and j, the number S[i]+S[j] does also occur in the sequence S (one or more times).
Note that the numbers S[i] and S[j] may be equal, only the indices i and j are required to be distinct. Also note that from the definition it follows that any 0-element or 1-element sequence is closed under addition, as there are no valid pairs of distinct indices into such a sequence.
You are given a sequence of integers in a int[] elements. Return "closed" (quotes for clarity) if the given sequence is closed under addition. Otherwise, return "not closed".

Definition

Class:	SumFullSet
Method:	isSumFullSet
Parameters:	int[]
Returns:	String
Method signature:	String isSumFullSet(int[] elements)
(be sure your method is public)

Constraints
-	Number of elements in elements will be between 1 and 50, both inclusive.
-	Each element of elements will be between -50 and 50, both inclusive.

Examples
0)	
    	
{-1,0,1}
Returns: "closed"
(-1) + 0 = (-1), which does appear in our sequence
(-1) + 1 = 0, which does appear in our sequence
0 + 1 = 1, which does appear in our sequence
hence, our sequence is closed under addition
1)	
    	
{-1,1}
Returns: "not closed"
2)	
    	
{0,1}
Returns: "closed"
3)	
    	
{0,1,1}
Returns: "not closed"
This sequence is not closed under addition because 1+1 = 2, which is not an element of our sequence.
4)	
    	
{16,0,43,43,-36,-49,-46,-16,40,34,-43,-24,13,-48,45,19,12,0,43,6,26,-23,50,28,-3,21,46,45,-32,-41,0,-27,42,19,47,-36,-21,-1,5,-21,-28,-43,23,-26,-5,21,-41,16,-37,38}
Returns: "not closed"
5)	
    	
{10}
Returns: "closed"
A 1-element sequence is closed under addition by definition.

(2)要点:

(3)代码:


ThreeProgrammers

(1)问题描述:

Problem Statement
    	
This problem is about three programmers who work together on a project. The programmers' names are Alice, Bob, and Charles. We will use 'A', 'B', and 'C' to denote them.

Each day exactly one of the three programmers works on the project. The code history is a string that specifies who worked on which day. For example, the string "AAC" means that Alice worked on the project for two days in a row and then Charles worked for a day.

Alice is always able to work on the project. Each time Bob spends a day working, he needs to take at least one day off before he is able to work again. Each time Charles spends a day working, he needs to take at least two days off.

The above information means that not all strings are valid code histories. For example, the string "BB" is not a valid code history because Bob cannot work two days in a row.

You are given a String code. This may or may not be a valid code history. Find any permutation of letters of code that produces a valid code history, and return that code history. If there are multiple solutions, you may return any of them. If there are no solutions, return "impossible" instead.

 
Definition
    	
Class:	ThreeProgrammers
Method:	validCodeHistory
Parameters:	String
Returns:	String
Method signature:	String validCodeHistory(String code)
(be sure your method is public)
    
 
Constraints
-	code will contain between 1 and 50 characters, inclusive.
-	Each character in code will be one of 'A', 'B', and 'C'.
 
Examples
0)	
    	
"CAB"
Returns: "BCA"
The input is a valid code history. In fact, any permutation of this input is a valid code history, and you may return any of them.
1)	
    	
"CBB"
Returns: "BCB"
Bob cannot work on two consecutive days. Hence, "BCB" is the only valid code history that is a permutation of the given input.
2)	
    	
"BB"
Returns: "impossible"
3)	
    	
"BBA"
Returns: "BAB"
4)	
    	
"CAC"
Returns: "impossible"
5)	
    	
"ACAC"
Returns: "CAAC"
6)	
    	
"ACABC"
Returns: "ACABC"
7)	
    	
"BAABCABBCCACBAACABAABABBCCAACABCCAACCABCACACCBABAB"
Returns: "BACBABCAACBACABCBACBACABCBACBACABCABCAACBACAACABCA"

(2)要点:贪心算法

(3)代码:


PrimeStrings

(1)问题描述:

Problem Statement
    	
In this problem we deal with binary representations of integers. For clarity, decimal numbers will be given as numbers and their binary representations as strings. For example, the binary representation of 47 is "101111".

Additionally, in the entire problem statement, k is a positive integer constant. (The value of k will be given to you as one of the inputs.)

Let A be a positive integer. Consider the following process:

Find the string S that is the binary representation of A.
Let L = max( 1, length(S)-k ).
Let T be any (not necessarily contiguous) subsequence of S such that the length of T is between 1 and L, inclusive.
Convert T to decimal and output the result.
Each integer B that can be the result of the above process is called a binary substring of A. Note that S never starts with a leading zero, but T might. Some examples are shown below.

Let A = 10 and k = 1.
We convert A to S = "1010".
Then we compute that L = 3, which means that we are interested in subsequences of length at most 3.
Hence, T is one of "0", "1", "00", "01", "10", "11", "010", "100", "101", or "110".
This means that the binary substrings of A are 0, 1, 2, 3, 4, 5, and 6.
Let A = 10 and k = 2. Now we have L = 2, which means that the possible values of T are "0", "1", "00", "01", "10", and "11". Thus, the binary substrings of A are 0, 1, 2, and 3.
For A = 10 and k >= 3 the only binary substrings of A are the integers 0 and 1.
For A = 15 and k = 1 the binary substrings of A are the integers 1, 3, and 7.
You are given the int k. You are also given longs x and y. Consider the integers between 1 and x, inclusive, such that their largest binary substring is smaller than or equal to y.

Compute and return the number of such integers.

 
Definition
    	
Class:	PrimeStrings
Method:	getCount
Parameters:	long, long, int
Returns:	long
Method signature:	long getCount(long x, long y, int k)
(be sure your method is public)
    
 
Constraints
-	x will be between 1 and 1012, both inclusive.
-	y will be between 1 and 1012, both inclusive.
-	k will be between 1 and 40, both inclusive.
 
Examples
0)   	
2
1
1
Returns: 2
Given that k=1, the only binary substring of 1 is 1, and the binary substrings of 2 are 0 and 1. In both cases, the largest binary substring is less than or equal to y=1. Thus, the answer is 2.

1)	 	
6
2
1
Returns: 4
As x=6, we are interested in the numbers between 1 and 6, inclusive. For k=1, the largest binary substrings of 1, 2, 3, 4, 5, and 6 are 1, 1, 1, 2, 3, and 3, respectively. As we only count those for which the largest binary substring does not exceed 2, the answer is 4.

2)		
6
1
3
Returns: 6

3) 	
31
6
2
Returns: 20

4)  	
413
34
2
Returns: 130

5) 	
1000000000
1000000000
5
Returns: 1000000000

6)		
549755813602
8369864093
5
Returns: 178429547459


(2)要点:

(3)代码:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值