`

FizzBuzzWhizz

    博客分类:
  • java
 
阅读更多

FizzBuzzWhizz

你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏。此时有100名学生在上课。游戏的规则是:


1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3、5、7。
2. 让所有学生拍成一队,然后按顺序报数。
3. 学生报数时,如果所报数字是第一个特殊数(3)的倍数,那么不能说该数字,而要说Fizz;如果所报数字是第二个特殊数(5)的倍数,那么要说Buzz;如果所报数字是第三个特殊数(7)的倍数,那么要说Whizz。
4. 学生报数时,如果所报数字同时是两个特殊数的倍数情况下,也要特殊处理,比如第一个特殊数和第二个特殊数的倍数,那么不能说该数字,而是要说FizzBuzz, 以此类推。如果同时是三个特殊数的倍数,那么要说FizzBuzzWhizz。
5. 学生报数时,如果所报数字包含了第一个特殊数,那么也不能说该数字,而是要说相应的单词,比如本例中第一个特殊数是3,那么要报13的同学应该说Fizz。如果数字中包含了第一个特殊数,那么忽略规则3和规则4,比如要报35的同学只报Fizz,不报BuzzWhizz。
public class NumberTuil {

	public static String[] getTuil(int a,int b,int c,int n){
		String[] t = new String[n];
		for(int i=1;i<=n;i++){
			if (rule5(i,a)){
				t[i-1] = "Fizz";
			}else if(rule42(i,a,b,c)){
				t[i-1] = "FizzBuzzWhizz";
			}else{
				String bb = rule41(i,a,b,c);
				if (!(i+"").equals(bb)){
					t[i-1] = bb;
				}else{
					t[i-1] = rule3(i,a,b,c);
				}
			}
		}
		return t;
	}

	private static String rule3(int i, int a, int b, int c) {
		if (i%a==0){
			return "Fizz";
		}
		if (i%b==0){
			return "Buzz";
		}
		if (i%c==0){
			return "Whizz";
		}
		return i+"";
	}

	private static String rule41(int i, int a, int b, int c) {
		if  (i%a==0 && i%b==0 ) {
			return "FizzBuzz";
		}else if (i%b==0 && i%c==0) {
			return "BuzzWhizz";
		}else if(i%a==0 && i%c==0){
			return "FizzWhizz";
		}
		return i+"";
	}

	private static boolean rule42(int i, int a, int b, int c) {
		return i%a==0 && i%b==0 && i%c==0;
	}

	private static boolean rule5(int i, int a) {
		String str = String.valueOf(i);
		return str.indexOf(a+"") != -1;
	} 
	
	
	public static void main(String[] args) {
		String strs[] = getTuil(3, 5, 7, 200);
		for (String str : strs) {
			System.out.println(str);
		}
	}
}
1
2
Fizz
4
Buzz
Fizz
Whizz
8
Fizz
Buzz
11
Fizz
Fizz
Whizz
FizzBuzz
16
17
Fizz
19
Buzz
FizzWhizz
22
Fizz
Fizz
Buzz
26
Fizz
Whizz
29
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Buzz
41
FizzWhizz
Fizz
44
FizzBuzz
46
47
Fizz
Whizz
Buzz
Fizz
52
Fizz
Fizz
Buzz
Whizz
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
BuzzWhizz
71
Fizz
Fizz
74
FizzBuzz
76
Whizz
Fizz
79
Buzz
Fizz
82
Fizz
FizzWhizz
Buzz
86
Fizz
88
89
FizzBuzz
Whizz
92
Fizz
94
Buzz
Fizz
97
Whizz
Fizz
Buzz
101
Fizz
Fizz
104
FizzBuzzWhizz
106
107
Fizz
109
Buzz
Fizz
Whizz
Fizz
Fizz
Buzz
116
Fizz
118
Whizz
FizzBuzz
121
122
Fizz
124
Buzz
FizzWhizz
127
128
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
Fizz
BuzzWhizz
Fizz
142
Fizz
Fizz
Buzz
146
FizzWhizz
148
149
FizzBuzz
151
152
Fizz
Whizz
Buzz
Fizz
157
158
Fizz
Buzz
Whizz
Fizz
Fizz
164
FizzBuzz
166
167
FizzWhizz
169
Buzz
Fizz
172
Fizz
Fizz
BuzzWhizz
176
Fizz
178
179
FizzBuzz
181
Whizz
Fizz
184
Buzz
Fizz
187
188
FizzWhizz
Buzz
191
Fizz
Fizz
194
FizzBuzz
Whizz
197
Fizz
199
Buzz

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics