TopCoder 350 points 7-SRM 147 DIV 1 105/350 30%

Problem Statement

 

There are numMales males and numFemales females arranged in a circle. Starting from a given point, you count clockwise and remove theK'th person from the circle (where K=1 is the person at the current point, K=2 is the next person in the clockwise direction, etc...). After removing that person, the next person in the clockwise direction becomes the new starting point. After repeating this procedure numFemales times, there are no females left in the circle.

Given numMales, numFemales and K, your task is to return what the initial arrangement of people in the circle must have been, starting from the starting point and in clockwise order.

For example, if there are 5 males and 3 females and you remove every second person, your return String will be "MFMFMFMM".

Definition

 
Class:PeopleCircle
Method:order
Parameters:int, int, int
Returns:String
Method signature:String order(int numMales, int numFemales, int K)
(be sure your method is public)
 
 

Constraints

-numMales is between 0 and 25 inclusive
-numFemales is between 0 and 25 inclusive
-K is between 1 and 1000 inclusive

Examples

0) 
 
5
3
2
Returns: "MFMFMFMM"
Return "MFMFMFMM". On the first round you remove the second person - "M_MFMFMM". Your new circle looks like "MFMFMMM" from your new starting point. Then you remove the second person again etc.
1) 
 
7
3
1
Returns: "FFFMMMMMMM"
Starting from the starting point you remove the first person, then you continue and remove the next first person etc. Clearly, all the females are located at the beginning. Hence return "FFFMMMMMMM"
2) 
 
25
25
1000
Returns: "MMMMMFFFFFFMFMFMMMFFMFFFFFFFFFMMMMMMMFFMFMMMFMFMMF"
 
3) 
 
5
5
3
Returns: "MFFMMFFMFM"
Here we mark the removed people with '_', and the starting position with lower-case:
Number of      | People Remaining
Rounds         | (in initial order)
---------------+-----------------
 0             | mFFMMFFMFM
 1             | MF_mMFFMFM
 2             | MF_MM_fMFM
 3             | MF_MM_FM_m
 4             | M__mM_FM_M
 5             | M__MM__m_M
4) 
 
1
0
245
Returns: "M"
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

这道题又超过一个小时了,,题目不难,,中间调BUG调了很久,,,终于系统测试通过,之前做出来,有些边缘测试没做好。

public class PeopleCircle {



	public  String order(int numMales, int numFemales, int K) {
		int sum = numMales + numFemales;
		int step = K;
		int arrays[] = new int[sum];
		int i = 0;
		if (numMales + numFemales == 0) {
			return "";
		}
		while (true) {
			if (numFemales == 0) {
						break;
					}
			if (i == sum)
				i = 0;
			if (step == 1) {
				if (arrays[i] == 0) {
					if (numFemales == 0) {
						break;
					}
					arrays[i] = 1;
					step = K;
					numFemales--;
				}
			} else {
				if (arrays[i] == 0)
					step--;
			}
			i++;
		}
		StringBuilder sb = new StringBuilder();
		for (int k = 0; k < sum; k++)
			sb.append(arrays[k] == 0 ? "M" : "F");
		return sb.toString();

	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值