TCHS-9-250

 

Problem Statement

     One day, nClassmates classmates decided to play an exciting game. They formed a circle and assigned themselves player numbers from 0 to nClassmates - 1 in a clockwise direction. Then, they counted from 1 to nTimes, inclusive, starting with player 0 and going in a clockwise direction. Each player spoke out the current number, and then, his clockwise neighbor spoke out the next number, etc. If a player got a number that was divisible by 3, he would cry out the word "hello" instead of speaking out the number. Return the number of times that player who cried out "hello".

Definition

    
Class: ExcitingGame
Method: howMany
Parameters: int, int, int
Returns: int
Method signature: int howMany(int nClassmates, int nTimes, int who)
(be sure your method is public)
    
 

Constraints

- nClassmates will be between 1 and 1000, inclusive.
- nTimes will be between 1 and 10000, inclusive.
- who will be between 0 and nClassmates - 1, inclusive.

Examples

0)  
    
2
 
13
 
0
 
Returns: 2
 
Player 0 cried out "hello" for the numbers 3 and 9.
1)  
    
10
 
10
 
1
 
Returns: 0
 
Player 1 only had one turn, and he spoke out the number 2.
2)  
    
3
 
30
 
2
 
Returns: 10
 
He cried out "hello" on all of his turns.
3)  
    
895
 
8544
 
837
 
Returns: 3
 
 
public class ExcitingGame {

	public int howMany(int mates, int times, int who) {
		int sum = 0;
		for (int i = 1; i <= times; i++)
			if (i % 3 == 0 && (i - 1) % mates == who)
				sum++;
		return sum;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值