`
zhenping
  • 浏览: 85739 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Fizz Buzz programming

 
阅读更多
Fizz Buzz programming

Fizz Buzz挺有趣的题目,我现在是按照这个题目简单的实现了编码测试.

1.题目:

Inverse Fizz Buzz

Thisis a lot more complicated than the fizzbuzz problem, and takes some realthinking to get right.

Theaim of the test is to discover the shortest sequence of consecutive numbers,which when they are run through the fizzbuzz algorithm produce the requiredoutput.


Forexample, the shortest sequence that producesfizzis3


Whenlooking for the shortest sequence for

fizz buzz

onesequence that produces that output is

3, 4, 5

However,this isn't the shortest. The shortest sequence is9, 10


Inour case, we are only interested in the numbers between 1 and 100, so be sureyou limit your calculations to that range, otherwise you are likely to exceedtimeout limits.

2.Code

InverseFizzBuzz.java

3. package devtest;

4.

5. import java.util.Iterator;

6. import java.util.Map;

7. import java.util.Set;

8. import java.util.TreeMap;

9. import java.util.logging.Logger;

10.

11. public class InverseFizzBuzz {

12.

13.

14. private Logger logger = Logger.getLogger(InverseFizzBuzz.class.getName());

15.

16. private Integer[] finalRange = null;

17.

18. private final int FizzDivisor = 3;

19. private final int BuzzDivisor = 5;

20.

21. private String pattern = "";

22.

23. /**

24. * FIZZ i % 3 == 0

25. */

26. private static final String FIZZ = "fizz";

27. /**

28. * BUZZ i % 5 == 0;

29. */

30. private static final String BUZZ = "buzz";

31.

32.

33. private static final String FizzBuzz="fizzbuzz";

34.

35.

36. private static final String BuzzFizz="buzzfizz";

37.

38.

39. private static final String FizzBuzzFizz="fizzbuzzfizz";

40.

41.

42. private static final String FizzFizz="fizzfizz";

43.

44.

45. private static final String FizzFizzBuzz="fizzfizzbuzz";

46.

47. /**

48. *

49. */

50. public InverseFizzBuzz(String[] list) {

51. logger.info("Fizz Divisor = " + FizzDivisor);

52. logger.info("Buzz Divisor = " + BuzzDivisor);

53. for(String s : list){

54. this.pattern = this.pattern + s;

55. }

56. }

57. private void calculateFIZZ(){

58. for(int i = 1 ; i <= 100 ; i++){

59. if (i % FizzDivisor == 0 && i % BuzzDivisor != 0) {

60. this.finalRange = new Integer[] { i };

61. break;

62. }

63. }

64. }

65.

66. private void calculateBUZZ(){

67. for(int i = 1 ; i <= 100 ; i++){

68. if (i % FizzDivisor != 0 && i % BuzzDivisor == 0) {

69. this.finalRange = new Integer[] { i };

70. break;

71. }

72. }

73. }

74.

75. private void calculateFizzBuzz(){

76. for(int i = 1 ; i <= 100 ; i++){

77. if (i % FizzDivisor == 0 && (i+1) % BuzzDivisor == 0) {

78. this.finalRange = new Integer[] { i ,i+1};

79. break;

80. }

81. }

82. }

83. private void calculateBuzzFizz(){

84. for(int i = 1 ; i <= 100 ; i++){

85. if (i % BuzzDivisor == 0 && (i+1) % FizzDivisor == 0) {

86. this.finalRange = new Integer[] { i ,i+1};

87. break;

88. }

89. }

90. }

91. private void calculateFizzBuzzFizz(){

92. Map<Integer,String> hashMap = new TreeMap<Integer,String>();

93. int num1 = 1, num2 = 1, num3 = 1;

94. for (int i = 1; i <= 100; i++) {

95. if (i % FizzDivisor == 0 && i % BuzzDivisor != 0) {

96. hashMap.put(i, FIZZ);

97. } else if (i % FizzDivisor != 0 &&i % BuzzDivisor == 0) {

98. hashMap.put(i,BUZZ);

99. }

100. }

101. Set<Integer> keySet = hashMap.keySet();

102. boolean bool = false;

103. for (Iterator<Integer> it = keySet.iterator(); !bool && it.hasNext();) {

104. int key = it.next();

105. String keyValue = hashMap.get(key);

106. if(keyValue.equals(FIZZ)){

107. if(num1 == 1){

108. num1 = key;

109. }else{

110. num3 = key;

111. }

112. }else if(keyValue.equals(BUZZ)){

113. num2 = key;

114. }

115.

116. if(num1 != 1 && num2 != 1 && num3 !=1 && num1 < num2 && num2 < num3){

117. this.finalRange = new Integer[num3-num1 + 1];

118. for(int j=0,i = num1; i <= num3 ; i ++,j ++){

119. this.finalRange[j] = i;

120. }

121. bool = true;

122. }

123. }

124. }

125.

126. private void calculateFizzFizz(){

127. Map<Integer,String> hashMap = new TreeMap<Integer,String>();

128. int num1 = 1, num2 = 1;

129. for (int i = 1; i <= 100; i++) {

130. if (i % FizzDivisor == 0 && i % BuzzDivisor != 0) {

131. hashMap.put(i, FIZZ);

132. }

133. }

134. Set<Integer> keySet = hashMap.keySet();

135. int buff = 0;

136. boolean bool = false;

137. for (Iterator<Integer> it = keySet.iterator(); !bool && it.hasNext();) {

138. buff = it.next();

139. if(num1 == 1 && num2 == 1){

140. num1 = buff;

141. num2 = buff;

142. }else{

143. num2 = buff;

144. }

145. boolean flag = false;

146. if(num1 != 1 && num2 != 1 && num1 < num2){

147. for(int i = num1; i <= num2 ; i ++){

148. if( i % BuzzDivisor != 0){

149. continue;

150. }else{

151. num1 = num2 ;

152. flag = true;

153. break;

154. }

155. }

156. if(!flag){

157. setData(num2,num1);

158. bool = true;

159. }

160. }

161. }

162. }

163.

164.

165. /** <Description functions in a word>

166. * <Detail description>

167. * @author Peter.Qiu [Parameters description]

168. * @return void [Return type description]

169. * @exception throws [Exception] [Exception description]

170. * @see [Related classes#Related methods#Related properties]

171. */

172. private void calculateSequenceRange(){

173. if(this.pattern.equals(FIZZ)){

174. calculateFIZZ();

175. }else if(this.pattern.equals(BUZZ)){

176. calculateBUZZ();

177. }else if(this.pattern.equals(FizzBuzz)){

178. calculateFizzBuzz();

179. }else if(this.pattern.equals(BuzzFizz)){

180. calculateBuzzFizz();

181. }else if(this.pattern.equals(FizzBuzzFizz)){

182. calculateFizzBuzzFizz();

183. }else if(this.pattern.equals(FizzFizz)){

184. calculateFizzFizz();

185. }else if(this.pattern.equals(FizzFizzBuzz)){

186. calculateFizzFizzBuzz();

187. }

188. }

189. private void calculateFizzFizzBuzz(){

190. calculateFizzFizz();//shortest of FizzFizz

191. int maxRange = this.finalRange[finalRange.length - 1];//

192. int minRange = this.finalRange[0];//

193. for(int i = maxRange ; i <= 100 ; i ++){

194. if(i % BuzzDivisor == 0){

195. maxRange = i;

196. break;

197. }

198. }

199. setData(maxRange,minRange);

200. }

201.

202. private void setData(int max, int min){

203. this.finalRange = new Integer[max- min + 1];

204. for(int j =0, i = min ; i <= max ; i++,j++ ){

205. this.finalRange[j] = i;

206. }

207. }

208. private void displayInfo() {

209. logger.info("=======Dispaly Info Start===============");

210. logger.info(this.pattern);

211. String result = "";

212. for(int i = 0 ; finalRange !=null && i < finalRange.length ; i ++){

213. Integer num = finalRange[i];

214. if(i < finalRange.length - 1){

215. result += num+",";

216. }else{

217. result += num;

218. }

219. }

220. logger.info(result);

221. logger.info("=======Dispaly Info End==============");

222. }

223.

224. public Integer[] sequence() {

225. calculateSequenceRange();

226. displayInfo();

227. return finalRange;

228. }

229.

230.}

git clone http://git.devtest.net/525f3dd13886977b57372de12c9c0818e633b3e74eb694027a7ee40333f4d230.git devtest

完整的编码及测试:http://download.csdn.net/detail/qiu_11/7791551

写的很简陋,有兴趣的同学可以继续研究。

分享到:
评论

相关推荐

    nico:Nico编程语言

    尼科 只是为了fun :slightly_smiling_face: ... println_str ( "Fizz Buzz" ) when x if x % 3 == 0 println_str ( "Fizz" ) when x if x % 5 == 0 println_str ( "Buzz" ) else println_i32

    [2018] Elm for Web Development.(Ajdin Imsirovic).[1788299051]

    Firstly, you will get a taste for web development with Elm by developing a simple fizz-buzz app. Next you will get hands-on with advanced Elm concepts as you develop your own personal blogging ...

    introductory_programming

    **入门编程**三角练习有史以来最简单的运动在控制台打印一个星号。... n=3 的示例: 账单**FizzBu​​zz 练习FizzBu​​zz 是一个简单的数字游戏,您可以在其中数数,但要说“Fizz”和/或“Buzz”,而不是遵

    Coding Interview _ Solution Manual

    If the number is divisible by 3, output "Fizz." If the number is divisible by 5, output "Buzz." If the number is divisible by both 3 and 5, output "FizzBuzz." If the number is not divisible by any of...

    制定:(WIP)制定:一种简单,熟悉且快速的编译后编程语言

    case (true, false) =&gt; print("Fizz"); case (false, true) =&gt; print("Buzz"); case (true, true) =&gt; print("FizzBuzz"); default =&gt; print(n); } } } 特征 静态类型有助于而不是阻碍 高效的编译时内存管理 ...

    Katas:卡塔练习

    1. **FizzBuzz**:这是一个基础的编程挑战,要求编写代码打印出1到100的数字,但遇到3的倍数打印"Fizz",5的倍数打印"Buzz",既是3又是5的倍数则打印"FizzBuzz"。这个练习主要测试基本的循环和条件判断。 2. **...

    PHPFizzBuzzWoof

    FizzBuzz问题要求我们编写一个程序,对于从1到给定数(比如100)的数字序列,如果数字能被3整除,则输出"Fizz";如果能被5整除,则输出"Buzz";如果能同时被3和5整除,则输出"FizzBuzz"。对于其他数字,就直接输出...

    LeetCode最全代码

    * [Dynamic Programming](https://github.com/kamyu104/LeetCode#dynamic-programming) * [Greedy](https://github.com/kamyu104/LeetCode#greedy) * [Design](https://github.com/kamyu104/LeetCode#design) ## ...

Global site tag (gtag.js) - Google Analytics