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 hashMap = new TreeMap();

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 keySet = hashMap.keySet();

102. boolean bool = false;

103. for (Iterator 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 hashMap = new TreeMap();

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 keySet = hashMap.keySet();

135. int buff = 0;

136. boolean bool = false;

137. for (Iterator 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. /**

166. *

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

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

你可能感兴趣的:(Fizz Buzz programming)